Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang crash with return statement in statement-expression in default argument to lambda #53488

Closed
creator1creeper1 opened this issue Jan 30, 2022 · 5 comments
Labels
bug Indicates an unexpected problem or unintended behavior clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@creator1creeper1
Copy link

creator1creeper1 commented Jan 30, 2022

GodBolt Link: https://godbolt.org/z/8rjxs4ab3

Program Source:

int g()
{
	//char f(char c = ({ return 42; 'A'; })) { return c; }
	auto f = [](char c = ({ return 42; 'A'; })) -> char { return c; };
	char r = f();
}

Crash Backtrace:

Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1.	<source>:4:35: current parser token ';'
2.	<source>:2:1: parsing function body 'g'
3.	<source>:2:1: in compound statement ('{}')
4.	<source>:4:11: lambda expression parsing
5.	<source>:4:24: in compound statement ('{}')
 #0 0x000055a514f6230f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055a514f601e0 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35601e0)
 #2 0x000055a514e9b178 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f8206b8a3c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0)
 #4 0x000055a5174408f7 clang::Sema::ActOnCapScopeReturnStmt(clang::SourceLocation, clang::Expr*, clang::Sema::NamedReturnInfo&, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a408f7)
 #5 0x000055a517441259 clang::Sema::BuildReturnStmt(clang::SourceLocation, clang::Expr*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a41259)
 #6 0x000055a5174421e6 clang::Sema::ActOnReturnStmt(clang::SourceLocation, clang::Expr*, clang::Scope*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5a421e6)
 #7 0x000055a516e28315 clang::Parser::ParseReturnStatement() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5428315)
 #8 0x000055a516e2e65f clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributesWithRange&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x542e65f)
 #9 0x000055a516e2f87d clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x542f87d)
#10 0x000055a516e2a562 clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x542a562)
#11 0x000055a516e36311 clang::Parser::ParseCompoundStatement(bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5436311)
#12 0x000055a516dd310e clang::Parser::ParseParenExpression(clang::Parser::ParenParseOption&, bool, bool, clang::OpaquePtr<clang::QualType>&, clang::SourceLocation&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53d310e)
#13 0x000055a516dcb006 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53cb006)
#14 0x000055a516dccfb6 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53ccfb6)
#15 0x000055a516dcd048 clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53cd048)
#16 0x000055a516dac736 clang::Parser::ParseParameterDeclarationClause(clang::DeclaratorContext, clang::ParsedAttributes&, llvm::SmallVectorImpl<clang::DeclaratorChunk::ParamInfo>&, clang::SourceLocation&) (.part.429) ParseDecl.cpp:0:0
#17 0x000055a516daceb5 clang::Parser::ParseParameterDeclarationClause(clang::DeclaratorContext, clang::ParsedAttributes&, llvm::SmallVectorImpl<clang::DeclaratorChunk::ParamInfo>&, clang::SourceLocation&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53aceb5)
#18 0x000055a516de39ed clang::Parser::ParseLambdaExpressionAfterIntroducer(clang::LambdaIntroducer&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53e39ed)
#19 0x000055a516de3dc3 clang::Parser::ParseLambdaExpression() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53e3dc3)
#20 0x000055a516dcbb72 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53cbb72)
#21 0x000055a516dccfb6 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53ccfb6)
#22 0x000055a516dcd048 clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53cd048)
#23 0x000055a516d98a00 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5398a00)
#24 0x000055a516da97a5 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53a97a5)
#25 0x000055a516daa637 clang::Parser::ParseSimpleDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributesWithRange&, bool, clang::Parser::ForRangeInit*, clang::SourceLocation*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53aa637)
#26 0x000055a516daa9b6 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributesWithRange&, clang::SourceLocation*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53aa9b6)
#27 0x000055a516e2eb59 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributesWithRange&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x542eb59)
#28 0x000055a516e2f87d clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x542f87d)
#29 0x000055a516e2a562 clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x542a562)
#30 0x000055a516e2b393 clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x542b393)
#31 0x000055a516d80454 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5380454)
#32 0x000055a516da96dc clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x53a96dc)
#33 0x000055a516d7b4e9 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributesWithRange&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x537b4e9)
#34 0x000055a516d7bbf1 clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributesWithRange&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.280) Parser.cpp:0:0
#35 0x000055a516d81b19 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributesWithRange&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5381b19)
#36 0x000055a516d82f39 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5382f39)
#37 0x000055a516d83435 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5383435)
#38 0x000055a516d76763 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5376763)
#39 0x000055a515ee1612 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x44e1612)
#40 0x000055a5158dd1f1 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3edd1f1)
#41 0x000055a515878622 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3e78622)
#42 0x000055a5159a70d3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3fa70d3)
#43 0x000055a512b8f1f4 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x118f1f4)
#44 0x000055a512b8b1ad ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#45 0x000055a5157164f5 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#46 0x000055a514e9b763 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x349b763)
#47 0x000055a5157185c8 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3d185c8)
#48 0x000055a5156ec83a clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cec83a)
#49 0x000055a5156ed37f clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ced37f)
#50 0x000055a5156f5d25 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cf5d25)
#51 0x000055a512a9c872 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0x109c872)
#52 0x00007f820663a0b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b3)
#53 0x000055a512b8ad2a _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0x118ad2a)
clang-14: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Compiler returned: 139

Note that gcc is able to compile this code correctly (GodBolt Link: https://godbolt.org/z/sGG3ddPrz). In invoking the lambda, it evaluates the default argument, runs the statement-expression, hits the return statement and correctly returns from the enclosing function.

This should be equivalent to the following:

int g()
{
	//char f(char c) { return c; }
	auto f = [](char c) -> char { return c; };
	char r = f(({ return 42; 'A'; }));
}

Since having a default argument on a function should cause the same behavior as manually writing out that argument on every caller.

clang is able to compile this written-out version correctly.

@EugeneZelenko EugeneZelenko added bug Indicates an unexpected problem or unintended behavior clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Jan 30, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 30, 2022

@llvm/issue-subscribers-clang-frontend

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 30, 2022

@llvm/issue-subscribers-bug

@junaire
Copy link
Member

junaire commented Feb 11, 2022

I think the minimal reproduce case should be:

int main(){
    auto f = [](char c = ({ return 42;})) {};
}

g++ gives errors like:

crash.cpp: In function ‘int main()’:
crash.cpp:2:30: error: could not convert ‘<statement>’ from ‘void’ to ‘char’
    2 |         auto f = [](char c = ({ return 42;})) {};
      |                              ^~~~~~~~~~~~~~~
      |                              |
      |                              void

@junaire
Copy link
Member

junaire commented Feb 12, 2022

A potential fix: https://reviews.llvm.org/D119609

@creator1creeper1
Copy link
Author

g++ gives errors like:

crash.cpp: In function ‘int main()’:
crash.cpp:2:30: error: could not convert ‘<statement>’ from ‘void’ to ‘char’
    2 |         auto f = [](char c = ({ return 42;})) {};
      |                              ^~~~~~~~~~~~~~~
      |                              |
      |                              void

Well yeah, because that statement-expression has the wrong type. Which is why I attached an example that actually does compile on gcc.

@junaire junaire closed this as completed in 8a4d388 Apr 6, 2022
@creator1creeper1 creator1creeper1 changed the title clang crash with return statement in statement-expression in default argument to lambda (on apparently valid code) clang crash with return statement in statement-expression in default argument to lambda Apr 6, 2022
mem-frob pushed a commit to draperlaboratory/hope-llvm-project that referenced this issue Oct 7, 2022
As statement expression makes no sense in the default argument,
this patch tries to disable it in the all cases.

Please note that the statement expression is a GNU extension, which
means that Clang should be consistent with GCC. However, there's no
response from GCC devs since we have raised the issue for several weeks.
In this case, I think we can disallow statement expressions as a default
parameter in general for now, and relax the restriction if GCC folks
decide to retain the feature for functions but not lambdas in the
future.

Related discussion: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104765

Fixes llvm/llvm-project#53488

Differential Revision: https://reviews.llvm.org/D119609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

4 participants