[clang]Clang assertion failure with lambda capturing local array in invalid template constructor
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
The following code causes crash on x86-64 clang(assertions)
**https://godbolt.org/z/qrezdrf8r**
```cpp
template
struct S {
S();
};
using X = S<0>;
template
X::S()
{
int e[1];
test([&e]() { return e[0]; });
}
```
Clang crashes with an assertion failure when compiling an invalid out-of-class constructor definition for `S<0>` that is still declared as a function template and contains a lambda capturing a local array by reference.
The compiler correctly diagnoses the template error, but then hits an assertion in the lambda capture handling instead of recovering gracefully.
**Compiler Output:**
```
:7:1: error: template parameter list matching the non-templated nested type 'S<0>' should be empty ('template<>')
7 | template
| ^ ~~~~~~~
8 | X::S()
| ~
clang++: /root/llvm-project/llvm/tools/clang/lib/Sema/SemaLambda.cpp:193: clang::UnsignedOrNone clang::getStackIndexOfNearestEnclosingCaptureCapableLambda(llvm::ArrayRef, clang::ValueDecl*, clang::Sema&): Assertion `((IndexOfCaptureReadyLambda != (FunctionScopes.size() - 1)) || S.getCurGenericLambda()) && "The capture ready lambda for a potential capture can only be the " "current lambda if it is a generic lambda"' 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/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
1. :11:28: current parser token ';'
2. :9:1: parsing function body 'S<0>::S'
3. :9:1: in compound statement ('{}')
4. :11:8: lambda expression parsing
5. :11:15: in compound statement ('{}')
#0 0x000000000444ea18 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x444ea18)
#1 0x000000000444b844 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x444b844)
#2 0x000000000444be64 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x444be64)
#3 0x000000000438b208 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x0000771909a42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#5 0x0000771909a969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#6 0x0000771909a42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#7 0x0000771909a287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#8 0x0000771909a2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#9 0x0000771909a39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#10 0x0000000007344e8f (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7344e8f)
#11 0x0000000007286c8a void llvm::function_ref::callback_fn(long, clang::ValueDecl*, clang::Expr*) SemaExprCXX.cpp:0:0
#12 0x000000000796ba6a clang::sema::LambdaScopeInfo::visitPotentialCaptures(llvm::function_ref) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x796ba6a)
#13 0x00000000072942de clang::Sema::ActOnFinishFullExpr(clang::Expr*, clang::SourceLocation, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x72942de)
#14 0x00000000075d6785 clang::Sema::ActOnCapScopeReturnStmt(clang::SourceLocation, clang::Expr*, clang::Sema::NamedReturnInfo&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x75d6785)
#15 0x00000000075d71ba clang::Sema::BuildReturnStmt(clang::SourceLocation, clang::Expr*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x75d71ba)
#16 0x00000000075d8677 clang::Sema::ActOnReturnStmt(clang::SourceLocation, clang::Expr*, clang::Scope*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x75d8677)
#17 0x0000000006d5879a clang::Parser::ParseReturnStatement() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d5879a)
#18 0x0000000006d5cc03 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d5cc03)
#19 0x0000000006d5db46 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d5db46)
#20 0x0000000006d664a6 clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d664a6)
#21 0x0000000006cf8c12 clang::Parser::ParseLambdaExpressionAfterIntroducer(clang::LambdaIntroducer&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cf8c12)
#22 0x0000000006cf9cfb clang::Parser::ParseLambdaExpression() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cf9cfb)
#23 0x0000000006cda4bd clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cda4bd)
#24 0x0000000006cdae97 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, clang::TypoCorrectionTypeBehavior, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cdae97)
#25 0x0000000006cdaf29 clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cdaf29)
#26 0x0000000006cdb28e clang::Parser::ParseExpressionList(llvm::SmallVectorImpl&, llvm::function_ref, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cdb28e)
#27 0x0000000006ce045d clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ce045d)
#28 0x0000000006cd8bfa clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cd8bfa)
#29 0x0000000006cdae97 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, clang::TypoCorrectionTypeBehavior, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cdae97)
#30 0x0000000006cdaf29 clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cdaf29)
#31 0x0000000006cdf819 clang::Parser::ParseExpression(clang::TypoCorrectionTypeBehavior) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cdf819)
#32 0x0000000006d65cc9 clang::Parser::ParseExprStatement(clang::Parser::ParsedStmtContext) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d65cc9)
#33 0x0000000006d5d164 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d5d164)
#34 0x0000000006d5db46 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d5db46)
#35 0x0000000006d664a6 clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d664a6)
#36 0x0000000006d66cea clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d66cea)
#37 0x0000000006c6c3f3 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c6c3f3)
#38 0x0000000006ca2a8d 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+++0x6ca2a8d)
#39 0x0000000006d72c50 clang::Parser::ParseDeclarationAfterTemplate(clang::DeclaratorContext, clang::Parser::ParsedTemplateInfo&, clang::ParsingDeclRAIIObject&, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d72c50)
#40 0x0000000006d80169 clang::Parser::ParseTemplateDeclarationOrSpecialization(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (.part.0) ParseTemplate.cpp:0:0
#41 0x0000000006d8057a clang::Parser::ParseDeclarationStartingWithTemplate(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d8057a)
#42 0x0000000006cab460 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6cab460)
#43 0x0000000006c67c16 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c67c16)
#44 0x0000000006c68b25 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c68b25)
#45 0x0000000006c4a1ea clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c4a1ea)
#46 0x0000000004f2a6b8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f2a6b8)
#47 0x0000000005236385 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5236385)
#48 0x00000000051b41ee clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x51b41ee)
#49 0x0000000005332d4d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5332d4d)
#50 0x0000000000decf52 cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdecf52)
#51 0x0000000000de388a ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#52 0x0000000000de3a0d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#53 0x0000000004fa09f9 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#54 0x000000000438b644 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x438b644)
#55 0x0000000004fa1026 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#56 0x0000000004f60102 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f60102)
#57 0x0000000004f610ae clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f610ae)
#58 0x0000000004f68c65 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f68c65)
#59 0x0000000000de92b4 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xde92b4)
#60 0x0000000000ca091a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xca091a)
#61 0x0000771909a29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#62 0x0000771909a29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#63 0x0000000000de3325 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xde3325)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
```
Contributor guide
Assessment
This issue has not been assessed yet.