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

    <tr>
        <th>Summary</th>
        <td>
            Clang argument deduction failure within nested generic lambda
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          sneed-deens
      </td>
    </tr>
</table>

<pre>
    Latest GCC and MSVC able to compile, latest Clang don't ([Godbolt](https://godbolt.org/z/9YbMbez67)).
Compiled with: `-std=c++20 -Wall -Wextra -Wpedantic`.

`x86-64 Clang 14.0.0` also able to compile (any version above don't).

Factoring out part with problematic deduction also makes it compile ([Godbolt](https://godbolt.org/z/Wv5zEncT3)).

```
using Index = int;
using Alignment = int;
struct Key {
  Index old;
  Alignment a;
};

template<Key k, typename T> struct Elem {};
template<typename ...Elems> struct Map : Elems... {};

template<typename ...Ts> struct Tuple {};

// removing 't' parameter makes it compile,
// so sort_tuple should be template to trigger error
constexpr auto sort_tuple(auto t)
{
 (void)t;
  using M = ::Map<
    ::Elem<::Key{0, 1}, char>
  >;

  // this lambda should be generic to trigger error
  using Sorted = decltype([]<auto ...Is>(
  ){
    return ::Tuple<
      decltype([]<::Key k, typename T>(
        [[maybe_unused]] ::Elem<k, T>&& deduced
      ){
 return T{};
      }.template operator()<::Key{0, 1}>(M{}))
    >{};
 }.template operator()<1,2,3>());

  return Sorted{};
};

using T1 = Tuple<char, short, int, double>;
using Sorted = decltype(sort_tuple(T1{}));

int main() {}
```
Error from Compiler Explorer:
```
<source>:30:18: error: no matching member function for call to 'operator()'
   26 |       decltype([]<::Key k, typename T>(
      | ~~~~~~~~~~~~~~~~~~~~~~~~
   27 |         [[maybe_unused]] ::Elem<k, T>&& deduced
      | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   28 |       ){
      |       ~~
 29 |         return T{};
      |         ~~~~~~~~~~~
   30 | }.template operator()<::Key{0, 1}>(M{}))
      | ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
<source>:24:4: note: while substituting into a lambda expression here
   24 |   ){
      |    ^
<source>:38:25: note: in instantiation of function template specialization 'sort_tuple<Tuple<char, short, int, double>>' requested here
   38 | using Sorted = decltype(sort_tuple(T1{}));
      | ^
<source>:26:16: note: candidate template ignored: deduced type '::Elem<k, T>' of 1st parameter does not match adjusted type 'M' (aka 'Map< ::Elem< ::Key{0, 1}, char>>') of argument [with k = Key{0, 1}, T = char]
   26 | decltype([]<::Key k, typename T>(
      |                ^
1 error generated.
Compiler returned: 1
```
This minimized example made from code that sorts tuple's types, where
GCC also unable to compile ([Godbolt](https://godbolt.org/z/YPh1aMnco), if interested).

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysV19v4zgO_zTKC1HDkf8lD3lI3XSx2CtwwAY32KeFbDGJtraUk-RO24f77AdKTuJ00lkMZo0ASSyR_JH8kaKEc2qvEVesuGfFw0wM_mDsymlEeScRtZs1Rr6t_iU8Og-_1DUILeHp9__UIJoOwRtoTX9UHTJeQxe31Z3Qe5BGM155YHzBivtfjGxM51nxwPji4P3RsWzN-CPjj_u4lBi7Z_zxnfHH5R_NU4PvZcX4kvFlwtJ1Ha1I-Kr8gWVrYGV657xk2UPL-D3j9zyFuy-i6-DuC756K-DuyxGl0F61rExJB33K9HVR3pX5CHKeJ2mSsjIF0Tnz0SfCLvQbvKB1ymgQjXnBk2MjMJauH0XrjVV6D2bwcBTWB5RwtKbpsBdetSBRDq0PSshQL57RgfJTSz8apS8vxftGt9vsEqXo4fhJ14MjUL9qia_AsgdQ2rPs_ryw7tRe96j99aLzdmg9_IZvwCp6AaMK08m4AyaiIr5i1cP4I1177I9EBZbVpOSZqOHfjqhFj7Bl2QZGE5sO-2DjJDyRPAskSUL73ETuSRyBOBDeJ0lypeNzNdupju1wpLBfC8ZQg8XevFCEYp4ryqno0aP9J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