<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60665>60665</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [C++20][Modules] Unable to use ranges::find/sort when imported from a libc++ header unit
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kaimfrai
      </td>
    </tr>
</table>

<pre>
    The following code works fine in Clang16 (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.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcV01v47wR_jX0ZWBDoiLZPvjgj9fAHormsEWPAi2OLO5SpEpSyaaH_vZiKNlyEm_RTXcX6BsENMWPGfKZZ4Yzwnt1Nogblu9YfpiJPjTWbb4K1dZOqNnJypfN5wahtlrbZ2XOUFmJ8GzdVw-1MgjKwF4Lc04LYHwljIQ0h2cVGpjX-K1Dp1o0Qei5Vicn3Avja3hu0EDvSZwwoEyle4ksObBky4pk_B8--ZHxI7RCmUXVda8GK1LL-I7x3TwtYO6DZNmhGkZ4Ege0OrHsoNVpHH4nKRu1A8v2Qp-tU6FpWfbHOB9bZULcx_iK8fU4s9xdlqxpXjg3YMiWO0gZ3wOnJqPmgZoc2PLAsmkX46snqyTj63jwLcu2Tpgz-qFfKyMJUOdGOeubzaOIuxu9deGy8WbP8nAf4Nj-nQxSNcKcySahQaAFV3BYkYCt43iDQqIbDCwMLVNtRxqnJZV1Dn1njSRh44beqACNfcYndKACGAvamjM6qGzbKY1-8XsIMJ93DkedM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