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

    <tr>
        <th>Summary</th>
        <td>
            ICE on fold expression in nested lambda with parameter pack in a templated entity
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          Dasaav-dsv
      </td>
    </tr>
</table>

<pre>
    Problematic code (should be valid):
```cpp
template<int = 0>
void f() {
    []<int... Is>() {
        ([i = Is]{}, ...);
    }.template operator()<1,2,3>();
}

void f2() {
 f();
}
```
Flags: `-std=c++20`

Crash log:
```
clang++: /root/llvm-project/llvm/include/llvm/Support/Casting.h:109: static bool llvm::isa_impl_cl<To, const From*>::doit(const From*) [with To = clang::PackExpansionExpr; From = clang::Expr]: Assertion `Val && "isa<> used on a null pointer"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-18.1.0/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-13.2.0 -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 <source>
1.      <eof> parser at end of file
2.      <source>:2:6: instantiating function definition 'f<0>'
3.      <source>:3:5: instantiating function definition 'f()::(anonymous class)::operator()<1, 2, 3>'
 #0 0x00000000038d53d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x38d53d8)
 #1 0x00000000038d30bc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x38d30bc)
 #2 0x000000000381baa8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f6145242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f61452969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007f6145242476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007f61452287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007f614522871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007f6145239e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000000007546433 (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x7546433)
#10 0x0000000007549c84 clang::TemplateArgument::isPackExpansion() const (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x7549c84)
#11 0x0000000006cda1a4 clang::Sema::collectUnexpandedParameterPacks(clang::TemplateArgument, llvm::SmallVectorImpl<std::pair<llvm::PointerUnion<clang::TemplateTypeParmType const*, clang::NamedDecl*>, clang::SourceLocation>>&) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6cda1a4)
#12 0x0000000006c351c1 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCXXFoldExpr(clang::CXXFoldExpr*) SemaTemplateInstantiate.cpp:0:0
#13 0x0000000006c291d9 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#14 0x0000000006c5eedf clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformStmt(clang::Stmt*, clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::StmtDiscardKind) SemaTemplateInstantiate.cpp:0:0
#15 0x0000000006c5fac4 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#16 0x0000000006c65f4a clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6c65f4a)
#17 0x0000000006cb6486 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6cb6486)
#18 0x0000000006251ba1 clang::Sema::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6251ba1)
#19 0x0000000006b9ac42 clang::Sema::DeduceReturnType(clang::FunctionDecl*, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6b9ac42)
#20 0x0000000006641d3a clang::Sema::DiagnoseUseOfDecl(clang::NamedDecl*, llvm::ArrayRef<clang::SourceLocation>, clang::ObjCInterfaceDecl const*, bool, bool, clang::ObjCInterfaceDecl*, bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6641d3a)
#21 0x0000000006a1694f clang::Sema::BuildCallToMemberFunction(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6a1694f)
#22 0x00000000066b42ae clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x66b42ae)
#23 0x00000000066b6f3c clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x66b6f3c)
#24 0x0000000006c34770 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCallExpr(clang::CallExpr*) SemaTemplateInstantiate.cpp:0:0
#25 0x0000000006c2929a clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#26 0x0000000006c5eedf clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformStmt(clang::Stmt*, clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::StmtDiscardKind) SemaTemplateInstantiate.cpp:0:0
#27 0x0000000006c5fac4 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#28 0x0000000006c65f4a clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6c65f4a)
#29 0x0000000006cb6486 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6cb6486)
#30 0x0000000006cb48bf clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6cb48bf)
#31 0x0000000006274989 clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.0) Sema.cpp:0:0
#32 0x0000000006275162 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x6275162)
#33 0x00000000060fd531 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x60fd531)
#34 0x00000000060f02ea clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x60f02ea)
#35 0x000000000415d928 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x415d928)
#36 0x00000000043cbfe9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x43cbfe9)
#37 0x000000000434ba9e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x434ba9e)
#38 0x00000000044ab01e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x44ab01e)
#39 0x0000000000c10cd6 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0xc10cd6)
#40 0x0000000000c084fa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#41 0x000000000419ee49 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#42 0x000000000381bf54 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x381bf54)
#43 0x000000000419f43f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#44 0x00000000041671c5 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x41671c5)
#45 0x0000000004167c2d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x4167c2d)
#46 0x000000000416fb65 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0x416fb65)
#47 0x0000000000c0e06c clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0xc0e06c)
#48 0x0000000000b05134 main (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0xb05134)
#49 0x00007f6145229d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#50 0x00007f6145229e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#51 0x0000000000c07fde _start (/opt/compiler-explorer/clang-assertions-18.1.0/bin/clang+++0xc07fde)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
```
Reproduction on godbolt: https://godbolt.org/z/EosG7Y6Ms
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsW99z2yi7_mvIDWOPhH5f5MKx4z3d055m6nTPnisPAuSwRaADKE2-v_4bkOwYxck2O_HO7LfbaRtbwMvzPry8PAiCjeE7ydglyK5AtrrAvb1T-nKFDcb3M2ruL2pFHy9vtKoFa7HlBBJFGQSoNHeqFxTWDN5jwSlAFUgWIFqBaAHyaPhLum54YlnbCWwZSJZcWgiSFYxAcj0U3itOYQNQCVAFQXE1PIUQwgHV0Gg-n8MPxjU6UdFXRiXIrrg3_sG4dsUVKFYALeF8Pvf4jk0Xq_keFVQd09gqPZgGyTIGaIkAWiaH_g6NncnhwxF49AxU82KzPTnD17XAOwOSBQR5NDOWgmRFALoC6Ao9VRr-X2ps7qBQu-dED1-JwHI3NPYW0VorZQFaC3HfzjqtfmNk_xWgNZdE9JQ9Pdj0Xae0q7HExnK5m9-BZBFHbmShsX74a6UE9PWTBUgW3OAtbzuxJQIky1vl2CZKGgvXWrUALRyBviZV3AJUhoWOsuzqO7d38Fb5kRt88C1uMPl2_dBhabiS1w-dBsmVbzmt6MtcnCzgwhimLVfS8fkLFhCgHKAcAoS4wSBZguQa9oZRqCTEUPZCwE5xaZkGCAFUwAZzweh8YPTm4_Vicw1NX7fcQgzrf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