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

    <tr>
        <th>Summary</th>
        <td>
            [clang] Requires clauses instantiate unused function templates leading to errors
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    Given the following translation unit

```c++
void f(int, int);

auto do_f(auto lhs, auto rhs) {
        f(lhs, rhs);
}

template<typename T>
struct wrapper {
        explicit wrapper(T) {
        }

        template<typename U> requires requires(T lhs, U rhs) { do_f(lhs, rhs); }
        friend auto f(wrapper<T>, wrapper<U>) -> void {
        }
};

auto g(wrapper<wrapper<int>> x) -> void {
        f(x, x);
}
```

clang trunk gives an error of

```console
<source>:4:2: error: no matching function for call to 'f'
    4 |         f(lhs, rhs);
 |         ^
<source>:12:57: note: in instantiation of function template specialization 'do_f<int, wrapper<int>>' requested here
   12 | template<typename U> requires requires(T lhs, U rhs) { do_f(lhs, rhs); }
 | ^
<source>:12:57: note: in instantiation of requirement here
   12 | template<typename U> requires requires(T lhs, U rhs) { do_f(lhs, rhs); }
 | ^~~~~~~~~~~~~~
<source>:12:32: note: while substituting template arguments into constraint expression here
   12 |         template<typename U> requires requires(T lhs, U rhs) { do_f(lhs, rhs); }
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:18:2: note: while checking constraint satisfaction for template 'f<int>' required here
   18 | f(x, x);
      |         ^
<source>:18:2: note: while substituting deduced template arguments into function template 'f' [with U = wrapper<int>]
<source>:1:6: note: candidate function not viable: no known conversion from 'wrapper<int>' to 'int' for 2nd argument
    1 | void f(int, int);
      |      ^      ~~~
<source>:13:14: note: candidate function template not viable: no known conversion from 'int' to 'wrapper<int>' for 1st argument
   13 |         friend auto f(wrapper<T>, wrapper<U>) -> void {
 |                     ^ ~~~~~~~~~~
1 error generated.
Compiler returned: 1
```

Whereas clang 20 accepted.

See it live: https://godbolt.org/z/hxdM45nev

What I expect to happen, and what used to happen, is that the constrain on the `f` in `wrapper` would unwrap one layer on both arguments, calling `f(wrapper<int>, wrapper<int>)`, which in turn would unwrap one layer and call the base `f(int, int)`. I think what's happening now is that after the first function template is instantiated, it's being considered for a failed overload resolution, which leads to a hard error inside the body of `do_f`, but I am having trouble coming up with why that happens now.

It's possible this is another side-effect / breakage of the CWG change mentioned in https://github.com/llvm/llvm-project/issues/143918 . Like that issue, this one depends on the existence of a single-argument constructor.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEV01v67oR_TX0ZhBDomzLWnjhOPHDBdpN-4K3LChxZLGhSZek7OQu-tuLISV_JM5tgduLJxixIlIzc86cmaGF92pnEFds_sjmTxPRh866lRRHJX2wBie1le-r39QRDYQOobVa25MyOwhOGK9FUNZAb1Rg2Zo-iyx9GsYf6ZOtj1ZJaBlfKhMY30D8qljxmN4QfbAg7T9oR7zXnadt8d7RfQWsTJsr2jSsp6XBTPmUjAXcH7QIyIpNeD-gEXuE31nxzLK1D65vApycOBzQXUzi20GrRp1XGF_-fuPzbJxl1T37L6x4Bof_6pVDf74hMyOUlyscI9SPKGB0U7VOoZEJP-0c4yo2EQnfwOXJS3xSwQPFEIn-GHb5dMv07sbi5Y6yUjyTmbcvDFIsb-T-7SPvY86Tm0aLKI_evMJOHdGDMIDOWQe2_agSa7zVSI-Kjbe9azBGsZ6xYs1ZsU4v0o2xsBeh6Uh7bW-aKLzWOmiE1hAsMF62jJcsWwMAzICVGxivL3Rzs4fNnz_HkVMQ8zIFQHlfgzKgjA_CBJXUb9tLQKNAwB-wUUKr72kP42VMfCL6Jodn5hkvo3zQB5TQocOEJecxzl-uvejlZ1gY_O_RhD8x_H9fX19gKfg1llOnNILvax9U6EPsbmMehdv1BMhT37JAgg1OKBMA3w4OvSfsn8CO168HnaR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