[clang] crash when compiling a lambda with template for
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Reproducer:
https://godbolt.org/z/31hroq9a9
```cpp
void foo() {
static constexpr int arr[]{1, 2, 3};
int sum = 0;
[n = 5, &sum]() {
template for (constexpr auto x : __FUNCTION__) { sum += n + x; }
}();
}
```
Backtrace:
```console
clang++: /root/llvm-project/llvm/tools/clang/lib/Sema/SemaExpr.cpp:19848: bool clang::Sema::tryCaptureVariable(clang::ValueDecl*, clang::SourceLocation, clang::TryCaptureKind, clang::SourceLocation, bool, clang::QualType&, clang::QualType&, const unsigned int*): Assertion `MaxFunctionScopesIndex <= FSIndex && "FunctionScopeIndexToStopAt should be no greater than FSIndex into " "FunctionScopes."' 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 -std=c++2c
1. :6:5: current parser token ')'
2. :1:12: parsing function body 'foo'
3. :1:12: in compound statement ('{}')
4. :4:3: instantiating function definition 'foo()::(lambda)::operator()'
#0 0x0000000004413058 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4413058)
#1 0x000000000440ff04 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x440ff04)
#2 0x000000000441053c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x441053c)
#3 0x000000000434f518 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x0000733203045330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x000073320309eb2c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9eb2c)
#6 0x000073320304527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x00007332030288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x000073320302881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x000073320303b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x0000000007298859 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7298859)
#11 0x000000000743093e clang::getStackIndexOfNearestEnclosingCaptureCapableLambda(llvm::ArrayRef, clang::ValueDecl*, clang::Sema&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x743093e)
#12 0x000000000736db8a void llvm::function_ref::callback_fn(long, clang::ValueDecl*, clang::Expr*) SemaExprCXX.cpp:0:0
#13 0x0000000007a5185a clang::sema::LambdaScopeInfo::visitPotentialCaptures(llvm::function_ref) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7a5185a)
#14 0x0000000007381526 clang::Sema::ActOnFinishFullExpr(clang::Expr*, clang::SourceLocation, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7381526)
#15 0x000000000712c2ad clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x712c2ad)
#16 0x000000000792b8c8 clang::Sema::InstantiateVariableInitializer(clang::VarDecl*, clang::VarDecl*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x792b8c8)
#17 0x00000000079824f3 clang::Sema::BuildVariableInstantiation(clang::VarDecl*, clang::VarDecl*, clang::MultiLevelTemplateArgumentList const&, llvm::SmallVector*, clang::DeclContext*, clang::LocalInstantiationScope*, bool, clang::VarTemplateSpecializationDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x79824f3)
#18 0x000000000798d41d clang::TemplateDeclInstantiator::VisitVarDecl(clang::VarDecl*, bool, llvm::ArrayRef*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x798d41d)
#19 0x0000000007993414 void llvm::function_ref::callback_fn(long) SemaTemplateInstantiateDecl.cpp:0:0
#20 0x0000000008830811 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8830811)
#21 0x000000000792b05a clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x792b05a)
#22 0x00000000078917b5 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformDeclStmt(clang::DeclStmt*) SemaTemplateInstantiate.cpp:0:0
#23 0x00000000078de815 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#24 0x00000000078e835e clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x78e835e)
#25 0x000000000722ee0f clang::Sema::FinishCXXExpansionStmt(clang::Stmt*, clang::Stmt*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x722ee0f)
#26 0x00000000078e7abc clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCXXExpansionStmtPattern(clang::CXXExpansionStmtPattern*) SemaTemplateInstantiate.cpp:0:0
#27 0x00000000078e835e clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x78e835e)
#28 0x00000000079325b0 clang::TemplateDeclInstantiator::VisitCXXExpansionStmtDecl(clang::CXXExpansionStmtDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x79325b0)
#29 0x0000000007993414 void llvm::function_ref::callback_fn(long) SemaTemplateInstantiateDecl.cpp:0:0
#30 0x0000000008830811 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8830811)
#31 0x000000000792b05a clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x792b05a)
#32 0x00000000078917b5 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformDeclStmt(clang::DeclStmt*) SemaTemplateInstantiate.cpp:0:0
#33 0x00000000078de815 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#34 0x00000000078e835e clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x78e835e)
#35 0x0000000007986495 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7986495)
#36 0x0000000008830811 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8830811)
#37 0x00000000077d7446 clang::Sema::DeduceReturnType(clang::FunctionDecl*, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x77d7446)
#38 0x000000000727e304 clang::Sema::DiagnoseUseOfDecl(clang::NamedDecl*, llvm::ArrayRef, clang::ObjCInterfaceDecl const*, bool, bool, clang::ObjCInterfaceDecl*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x727e304)
#39 0x0000000007634467 CreateFunctionRefExpr(clang::Sema&, clang::FunctionDecl*, clang::NamedDecl*, clang::Expr const*, bool, clang::SourceLocation, clang::DeclarationNameLoc const&) SemaOverload.cpp:0:0
#40 0x00000000076865ef clang::Sema::BuildCallToObjectOfClassType(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef, clang::SourceLocation) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x76865ef)
#41 0x00000000072ca310 clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef, clang::SourceLocation, clang::Expr*, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x72ca310)
#42 0x00000000072cf51f clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef, clang::SourceLocation, clang::Expr*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x72cf51f)
#43 0x0000000006daeb83 clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6daeb83)
#44 0x0000000006da755a clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6da755a)
#45 0x0000000006da9807 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, clang::TypoCorrectionTypeBehavior, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6da9807)
#46 0x0000000006da9899 clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6da9899)
#47 0x0000000006dae189 clang::Parser::ParseExpression(clang::TypoCorrectionTypeBehavior) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6dae189)
#48 0x0000000006e394f9 clang::Parser::ParseExprStatement(clang::Parser::ParsedStmtContext) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e394f9)
#49 0x0000000006e30e6f clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e30e6f)
#50 0x0000000006e317c9 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e317c9)
#51 0x0000000006e39cc5 clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e39cc5)
#52 0x0000000006e3a50a clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e3a50a)
#53 0x0000000006d389dd clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::LateParsedAttrList*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d389dd)
#54 0x0000000006d82136 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+++0x6d82136)
#55 0x0000000006d2ce1b clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d2ce1b)
#56 0x0000000006d2d60f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d2d60f)
#57 0x0000000006d39db8 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d39db8)
#58 0x0000000006d3ad15 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d3ad15)
#59 0x0000000006d3b100 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d3b100)
#60 0x0000000006d17c4c clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d17c4c)
#61 0x0000000004f16e18 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f16e18)
#62 0x0000000005243b18 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5243b18)
#63 0x00000000051d063d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x51d063d)
#64 0x000000000531807d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x531807d)
#65 0x0000000000dae7fc cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdae7fc)
#66 0x0000000000da540a ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#67 0x0000000000da558d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#68 0x0000000004f8e109 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#69 0x000000000434f954 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x434f954)
#70 0x0000000004f8ec75 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#71 0x0000000004f47622 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f47622)
#72 0x0000000004f4be57 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f4be57)
#73 0x0000000004f563b5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f563b5)
#74 0x0000000000daacea clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdaacea)
#75 0x0000000000cc5afa main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcc5afa)
#76 0x000073320302a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#77 0x000073320302a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#78 0x0000000000da4db5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda4db5)
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.