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

[C++20][Modules] Unable to use ranges::find/sort when imported from a libc++ header unit

Open kaimfrai opened this issue 3 years ago • 3 comments

The following code works fine in Clang 16 (and 15 with -fexperimental-library) when using an include

// main.cpp
// clang++-16 -std=c++20 -stdlib=libc++ main.cpp
#include <algorithm>

int main()
{
	int arr[]{ 1, 2, 3, 4, 5 };
	(void)std::ranges::find(arr, 3);

	std::ranges::sort(arr);
}

When changing the #include of the header with an import of the corresponding header unit however it no longer compiles.

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

int main()
{
	int arr[]{ 1, 2, 3, 4, 5 };
	(void)std::ranges::find(arr, 3);

	std::ranges::sort(arr);
}
In module '/usr/lib/llvm-16/bin/../include/c++/v1/algorithm':
/usr/lib/llvm-16/bin/../include/c++/v1/__algorithm/ranges_find.h:49:12: error: no matching function for call to '__find_if_impl'
    return ranges::__find_if_impl(ranges::begin(__r), ranges::end(__r), __pred, __proj);
           ^~~~~~~~~~~~~~~~~~~~~~
main.cpp:6:25: note: in instantiation of function template specialization 'std::ranges::__find::__fn::operator()<int (&)[5], int, std::identity>' requested here
        (void)std::ranges::find(arr, 3);
                               ^
In module '/usr/lib/llvm-16/bin/../include/c++/v1/algorithm':
/usr/lib/llvm-16/bin/../include/c++/v1/__algorithm/ranges_sort.h:47:31: error: no matching function for call to '__make_projected'
    auto&& __projected_comp = std::__make_projected(__comp, __proj);
                              ^~~~~~~~~~~~~~~~~~~~~
/usr/lib/llvm-16/bin/../include/c++/v1/__algorithm/ranges_sort.h:64:12: note: in instantiation of function template specialization 'std::ranges::__sort::__fn::__sort_fn_impl<int *, int *, std::ranges::less, std::identity>' requested here
    return __sort_fn_impl(ranges::begin(__r), ranges::end(__r), __comp, __proj);
           ^
main.cpp:8:19: note: in instantiation of function template specialization 'std::ranges::__sort::__fn::operator()<int (&)[5], std::ranges::less, std::identity>' requested here
        std::ranges::sort(arr);
                         ^
/usr/lib/llvm-16/bin/../include/c++/v1/__algorithm/make_projected.h:77:1: note: candidate template ignored: requirement '!__can_use_pristine_comp<std::ranges::less, std::identity, void>::value' was not satisfied [with _Pred = std::ranges::less, _Proj = std::identity]
__make_projected(_Pred& __pred, _Proj& __proj) {
^
2 errors generated.

This only applies to libc++ however, and is not the case when using libstdc++12. As far as I could tell, this only affects find[_if/_if_not] and sort. Other ranges algorithms I used work as intended.

kaimfrai avatar Feb 10 '23 19:02 kaimfrai

@llvm/issue-subscribers-clang-modules

llvmbot avatar Feb 10 '23 22:02 llvmbot

Just checked up on this again. It appears to be resolved in Clang 17, but remains an issue in Clang 15 and 16.

kaimfrai avatar Apr 23 '23 07:04 kaimfrai

Just checked up on this again. It appears to be resolved in Clang 17, but remains an issue in Clang 15 and 16.

This was most likely a compiler bug, so it won't work in Clang 15 or 16. C++ header units in Clang are still very experimental. Also note that we are not claiming support for header units because of that.

philnik777 avatar Apr 23 '23 13:04 philnik777