llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang17] Ranges adaptor reverse with | no longer compiles when importing libc++ <ranges> header unit

Open kaimfrai opened this issue 3 years ago • 2 comments

A recent update in Clang 17 made the following code no longer compile:

// clang++-17 -std=c++20 -stdlib=libc++ --precompile -xc++-system-header ranges -o ranges.pcm
// clang++-17 -std=c++20 -stdlib=libc++ main.cpp -fmodule-file=ranges.pcm
import <ranges>;

int main()
{
	int arr[]{ 0, 1, 2, 3, 4, 5 };
	int product1 = 0;
	for (auto i : arr | std::views::reverse)
	{
		product1 *= i;
	}
	int product2 = 0;
	for (auto i : std::views::reverse(arr))
	{
		product2 *= i;
	}
	return product1 + product2;
}

It errors out with the following message:

main.cpp:9:20: error: invalid operands to binary expression ('int[6]' and 'const __reverse::__fn')
        for (auto i : arr | std::views::reverse)
                      ~~~ ^ ~~~~~~~~~~~~~~~~~~~
/usr/lib/llvm-17/bin/../include/c++/v1/cstddef:74:39: note: candidate function not viable: no known conversion from 'int[6]' to 'byte' for 1st argument
_LIBCPP_HIDE_FROM_ABI constexpr byte  operator| (byte  __lhs, byte __rhs) noexcept
                                      ^
/usr/lib/llvm-17/bin/../include/c++/v1/__ranges/range_adaptor.h:58:37: note: candidate template ignored: constraints not satisfied [with _View = int (&)[6], _Closure = const __reverse::__fn &]
    friend constexpr decltype(auto) operator|(_View&& __view, _Closure&& __closure)
                                    ^
/usr/lib/llvm-17/bin/../include/c++/v1/__ranges/range_adaptor.h:55:18: note: because 'same_as<std::ranges::views::__split_view::__fn, remove_cvref_t<const __fn &> >' evaluated to false
        requires same_as<_Tp, remove_cvref_t<_Closure>> &&
                 ^
/usr/lib/llvm-17/bin/../include/c++/v1/__concepts/same_as.h:29:19: note: because '__same_as_impl<std::ranges::views::__split_view::__fn, std::ranges::views::__reverse::__fn>' evaluated to false
concept same_as = __same_as_impl<_Tp, _Up> && __same_as_impl<_Up, _Tp>;
                  ^
/usr/lib/llvm-17/bin/../include/c++/v1/__concepts/same_as.h:26:26: note: because '_IsSame<__fn, __fn>::value' evaluated to false
concept __same_as_impl = _IsSame<_Tp, _Up>::value;
                         ^
/usr/lib/llvm-17/bin/../include/c++/v1/__ranges/range_adaptor.h:67:27: note: candidate template ignored: constraints not satisfied [with _Closure = int (&)[6], _OtherClosure = const __reverse::__fn &]
    friend constexpr auto operator|(_Closure&& __c1, _OtherClosure&& __c2)
                          ^
/usr/lib/llvm-17/bin/../include/c++/v1/__ranges/range_adaptor.h:62:15: note: because 'int (&)[6]' does not satisfy '_RangeAdaptorClosure'
    template <_RangeAdaptorClosure _Closure, _RangeAdaptorClosure _OtherClosure>
              ^
/usr/lib/llvm-17/bin/../include/c++/v1/__ranges/range_adaptor.h:50:32: note: because 'derived_from<remove_cvref_t<int (&)[6]>, __range_adaptor_closure<remove_cvref_t<int (&)[6]> > >' evaluated to false
concept _RangeAdaptorClosure = derived_from<remove_cvref_t<_Tp>, __range_adaptor_closure<remove_cvref_t<_Tp>>>;
                               ^
/usr/lib/llvm-17/bin/../include/c++/v1/__concepts/derived_from.h:28:3: note: because 'is_base_of_v<std::__range_adaptor_closure<int[6]>, int[6]>' evaluated to false
  is_base_of_v<_Bp, _Dp> &&
  ^
1 error generated.

Note that only the | syntax causes a problem. The call syntax does not. The issue is not present when using an #include <ranges> instead of the import <ranges>;. It is also not present in Clang 16 or when using libstdc++. It was also not present in Clang 17 a few days ago.

kaimfrai avatar Feb 24 '23 09:02 kaimfrai

This issue appears to be resolved for now. I suspect it's due to commit https://github.com/llvm/llvm-project/commit/db1b827ecfed6d311c315484f8a0bd78d4e9f99a. I'm unsure whether to close this or keep it open in case the same issue pops up again.

kaimfrai avatar Mar 02 '23 09:03 kaimfrai

I'm closing this. The original patch didn't follow the typical review process. If it had this issue would have been detected in the Clang pre-commit CI.

FYI I missed this bug report before, but I bi-sected the issue to https://github.com/llvm/llvm-project/commit/74565c3add6d683559618973863e78a5e6836e48. And indeed that revert fixes it.

mordante avatar Mar 02 '23 20:03 mordante