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

    <tr>
        <th>Summary</th>
        <td>
            Capturing structured binding in lambda in module causes Assertion `isa<X>(Val) && "cast_if_present<Ty>() argument of incompatible type!"`
        </td>
    </tr>

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

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

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

<pre>
    The following valid translation unit:

```cpp
export module m;

struct s {
        int m;
};

void f() {
        auto [x] = s();
        [=] { x; };
}
```

causes clang to crash with

```console
clang-18: /root/llvm-project/llvm/include/llvm/Support/Casting.h:706: auto llvm::cast_if_present(Y*) [with X = clang::VarDecl; Y = clang::Decl]: Assertion `isa<X>(Val) && "cast_if_present<Ty>() argument of incompatible type!"' 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-trunk-20231120/bin/clang-18 -cc1 -triple x86_64-unknown-linux-gnu -S -disable-free -clear-ast-before-backend -main-file-name example.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/app -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-assertions-trunk-20231120/lib/clang/18 -w -std=c++20 -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics -mllvm --x86-asm-syntax=intel -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /app/output.s -x pcm /tmp/example-bd7ff3.pcm
1.      <eof> parser at end of file
2.      <source>:7:6: LLVM IR generation of declaration 'f'
3.      <source>:7:6: Generating code for declaration 'f'
 #0 0x0000000003795218 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x3795218)
 #1 0x000000000379296c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f8759a6b420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f875952e00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f875950d859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007f875950d729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007f875951efd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x00000000044018d3 clang::ASTDeclReader::ReadCXXDefinitionData(clang::CXXRecordDecl::DefinitionData&, clang::CXXRecordDecl const*, clang::Decl*, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x44018d3)
 #8 0x000000000440e9b8 clang::ASTDeclReader::ReadCXXRecordDefinition(clang::CXXRecordDecl*, bool, clang::Decl*, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x440e9b8)
 #9 0x000000000442174b clang::ASTDeclReader::VisitCXXRecordDeclImpl(clang::CXXRecordDecl*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x442174b)
#10 0x0000000004427b65 clang::declvisitor::Base<std::add_pointer, clang::ASTDeclReader, void>::Visit(clang::Decl*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4427b65)
#11 0x0000000004428022 clang::ASTDeclReader::Visit(clang::Decl*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4428022)
#12 0x0000000004428679 clang::ASTReader::ReadDeclRecord(unsigned int) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4428679)
#13 0x000000000435dc1b clang::ASTReader::GetDecl(unsigned int) (.part.0) ASTReader.cpp:0:0
#14 0x00000000043c8bb7 clang::serialization::AbstractTypeReader<clang::ASTRecordReader>::read(clang::Type::TypeClass) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x43c8bb7)
#15 0x00000000043c9df9 clang::ASTReader::readTypeRecord(unsigned int) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x43c9df9)
#16 0x00000000043ca304 clang::ASTReader::GetType(unsigned int) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x43ca304)
#17 0x000000000442c3fb clang::ASTStmtReader::VisitExpr(clang::Expr*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x442c3fb)
#18 0x0000000004434971 clang::ASTStmtReader::VisitLambdaExpr(clang::LambdaExpr*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4434971)
#19 0x000000000443c0f2 clang::ASTReader::ReadStmtFromStream(clang::serialization::ModuleFile&) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x443c0f2)
#20 0x0000000004381c64 clang::ASTReader::GetExternalDeclStmt(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4381c64)
#21 0x0000000006f9b783 clang::FunctionDecl::getBody(clang::FunctionDecl const*&) const (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x6f9b783)
#22 0x0000000003b2b669 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x3b2b669)
#23 0x0000000003b8cc53 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x3b8cc53)
#24 0x0000000003b87ae5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x3b87ae5)
#25 0x0000000003b880d3 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x3b880d3)
#26 0x0000000003b91753 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#27 0x0000000004010d86 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#28 0x0000000004002e6c clang::BackendConsumer::HandleInterestingDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4002e6c)
#29 0x00000000043e42fd clang::ASTReader::PassInterestingDeclsToConsumer() (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x43e42fd)
#30 0x0000000004355fae non-virtual thunk to clang::ASTReader::StartTranslationUnit(clang::ASTConsumer*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4355fae)
#31 0x0000000005f1ed08 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x5f1ed08)
#32 0x000000000400e9a8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x400e9a8)
#33 0x0000000004275209 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x4275209)
#34 0x00000000041f56de clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x41f56de)
#35 0x000000000435454e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0x435454e)
#36 0x0000000000bf8346 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0xbf8346)
#37 0x0000000000befc0a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#38 0x0000000000bf47aa clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0xbf47aa)
#39 0x0000000000aef061 main (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0xaef061)
#40 0x00007f875950f083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#41 0x0000000000bef6ee _start (/opt/compiler-explorer/clang-assertions-trunk-20231120/bin/clang-18+0xbef6ee)
clang++: error: unable to execute command: Aborted (core dumped)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 254
```

when compiled with `-std=c++20 -w -x c++-module`

See it live: https://godbolt.org/z/Ysjr81PGG
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWl1v47iS_TXMC8FAoj79kAfHjnMb6AEanWxvz5NBiSWHtyVSIKnEmV-_ICU7pjpJZ2bXvXeQmbEoqnjOYbGqKJEZI3YS4Apl1yhbX7DBPih9xdmj4MYqCReV4s9X9w-AG9W26knIHX5kreDYaiZNy6xQEg9SWJQsUbRG0eG_eTT-1X0_tsC-V9riTvGhBdyh5Pq0v7F6qC02GBXH9oWQ9rRjsZ499KgExw2iJaKL4EE2WIVRdr1H2RqjZI3N2Onk-YWjnKx9h-Ia71FyjYMRivWMyenINRsMGFy3TO6wVbjWzDzgJ2EfXhdBSaNamJ51D5G4RMkSI7rRSllEN2372JFeq39DfbhEdCNk3Q4cXhruht7JiOhmxYwVcnf5gJJlEeXOmqftOyZLlCxrZuxWNNtegwFpES3_RHTptcquHVb83YvjAY2PfGN6DXXrxPhzfs_fyNZuoKUxoP3UozwShqFk9R0lN4iW31jrB6A5ojlGlM5BJKv757Gr68f0buhAWqwaLGStup5ZUbWA7XMPiMaIUkQL3DDRAr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