[clang] Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed
https://godbolt.org/z/7Y5ad7Prd
Clang crashes when attempting to parse a malformed decomposition declaration (structured binding) followed by a function body.
This seems to be a regression starting from Clang 19.1.0.
Assertion Builds (Trunk) fails early with an assertion: Assertion isa<To>(Val) && "cast<Ty>() argument of incompatible type!" failed.
Reproducer: b;([c,ac])(){} with clang++ example.cpp
Stack Dump (Assertion Trunk)
clang++: /root/llvm-project/llvm/include/llvm/Support/Casting.h:572: decltype(auto) llvm::cast(From*) [with To = clang::FunctionDecl; From = clang::Decl]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -v <source>
1. <source>:1:13: current parser token '{'
#0 0x00000000044143e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44143e8)
#1 0x0000000004411294 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4411294)
#2 0x00000000044118cc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44118cc)
#3 0x000000000434ff58 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x00007959d0a45330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x00007959d0a9ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
#6 0x00007959d0a4527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x00007959d0a288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x00007959d0a2881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x00007959d0a3b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x00000000071831d4 clang::Sema::ActOnStartOfFunctionDef(clang::Scope*, clang::Decl*, clang::SkipBodyInfo*, clang::Sema::FnBodyKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71831d4)
#11 0x00000000071a3ec1 clang::Sema::ActOnStartOfFunctionDef(clang::Scope*, clang::Declarator&, llvm::MutableArrayRef<clang::TemplateParameterList*>, clang::SkipBodyInfo*, clang::Sema::FnBodyKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71a3ec1)
#12 0x0000000006d781bf clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::LateParsedAttrList*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d781bf)
#13 0x0000000006dc2426 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6dc2426)
#14 0x0000000006d6caab clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d6caab)
#15 0x0000000006d6d29f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d6d29f)
#16 0x0000000006d79db8 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d79db8)
#17 0x0000000006d7acf5 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d7acf5)
#18 0x0000000006d578ba clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d578ba)
#19 0x0000000004f20548 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f20548)
#20 0x000000000524d138 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x524d138)
#21 0x00000000051dae8d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x51dae8d)
#22 0x0000000005322a7d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5322a7d)
#23 0x0000000000db3c0c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdb3c0c)
#24 0x0000000000daa81a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#25 0x0000000000daa99d int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)>(long, llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#26 0x0000000004f99049 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
#27 0x0000000004350394 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4350394)
#28 0x0000000004f99bb5 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
#29 0x0000000004f51a32 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f51a32)
#30 0x0000000004f56397 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f56397)
#31 0x0000000004f60985 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f60985)
#32 0x0000000000db00fa clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdb00fa)
#33 0x0000000000ccbc8a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xccbc8a)
#34 0x00007959d0a2a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#35 0x00007959d0a2a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#36 0x0000000000daa1c5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdaa1c5)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
Compiler version:
clang version 24.0.0git (https://github.com/llvm/llvm-project.git dafb6eebc1627805a9f697b40dff604f4875e7ec)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /cefs/31/319eb4ac9fc8eb278450ca8a_clang-assertions-trunk-20260822/bin
Build config: +assertions
Found candidate GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0
Selected GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
@llvm/issue-subscribers-clang-frontend
Author: thapgua
Clang crashes when attempting to parse a malformed decomposition declaration (structured binding) followed by a function body.
This seems to be a regression starting from Clang 19.1.0.
Assertion Builds (Trunk) fails early with an assertion: Assertion isa<To>(Val) && "cast<Ty>() argument of incompatible type!" failed.
Reproducer: b;([c,ac])(){} with clang++ example.cpp
<details>
Stack Dump (Assertion Trunk)
clang++: /root/llvm-project/llvm/include/llvm/Support/Casting.h:572: decltype(auto) llvm::cast(From*) [with To = clang::FunctionDecl; From = clang::Decl]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -v <source>
1. <source>:1:13: current parser token '{'
#<!-- -->0 0x00000000044143e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44143e8)
#<!-- -->1 0x0000000004411294 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4411294)
#<!-- -->2 0x00000000044118cc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44118cc)
#<!-- -->3 0x000000000434ff58 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#<!-- -->4 0x00007959d0a45330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#<!-- -->5 0x00007959d0a9ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
#<!-- -->6 0x00007959d0a4527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#<!-- -->7 0x00007959d0a288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#<!-- -->8 0x00007959d0a2881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#<!-- -->9 0x00007959d0a3b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#<!-- -->10 0x00000000071831d4 clang::Sema::ActOnStartOfFunctionDef(clang::Scope*, clang::Decl*, clang::SkipBodyInfo*, clang::Sema::FnBodyKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71831d4)
#<!-- -->11 0x00000000071a3ec1 clang::Sema::ActOnStartOfFunctionDef(clang::Scope*, clang::Declarator&, llvm::MutableArrayRef<clang::TemplateParameterList*>, clang::SkipBodyInfo*, clang::Sema::FnBodyKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x71a3ec1)
#<!-- -->12 0x0000000006d781bf clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::LateParsedAttrList*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d781bf)
#<!-- -->13 0x0000000006dc2426 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6dc2426)
#<!-- -->14 0x0000000006d6caab clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d6caab)
#<!-- -->15 0x0000000006d6d29f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d6d29f)
#<!-- -->16 0x0000000006d79db8 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d79db8)
#<!-- -->17 0x0000000006d7acf5 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d7acf5)
#<!-- -->18 0x0000000006d578ba clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d578ba)
#<!-- -->19 0x0000000004f20548 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f20548)
#<!-- -->20 0x000000000524d138 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x524d138)
#<!-- -->21 0x00000000051dae8d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x51dae8d)
#<!-- -->22 0x0000000005322a7d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5322a7d)
#<!-- -->23 0x0000000000db3c0c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdb3c0c)
#<!-- -->24 0x0000000000daa81a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#<!-- -->25 0x0000000000daa99d int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)>(long, llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#<!-- -->26 0x0000000004f99049 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
#<!-- -->27 0x0000000004350394 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4350394)
#<!-- -->28 0x0000000004f99bb5 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
#<!-- -->29 0x0000000004f51a32 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f51a32)
#<!-- -->30 0x0000000004f56397 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f56397)
#<!-- -->31 0x0000000004f60985 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f60985)
#<!-- -->32 0x0000000000db00fa clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdb00fa)
#<!-- -->33 0x0000000000ccbc8a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xccbc8a)
#<!-- -->34 0x00007959d0a2a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#<!-- -->35 0x00007959d0a2a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#<!-- -->36 0x0000000000daa1c5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdaa1c5)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
Compiler version:
clang version 24.0.0git (https://github.com/llvm/llvm-project.git dafb6eebc1627805a9f697b40dff604f4875e7ec)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /cefs/31/319eb4ac9fc8eb278450ca8a_clang-assertions-trunk-20260822/bin
Build config: +assertions
Found candidate GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0
Selected GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0
Candidate multilib: .;@<!-- -->m64
Candidate multilib: 32;@<!-- -->m32
Candidate multilib: x32;@<!-- -->mx32
Selected multilib: .;@<!-- -->m64
</details>
Goes back to Clang 4.0 https://godbolt.org/z/f9oKfrjGe
I think this is the same cause as this but this one: https://github.com/llvm/llvm-project/issues/214395