llvm / llvm/llvm-project

[Clang] Assertion failure in DiagnoseImmediateEscalatingReason: `FD->hasBody() && "expected the function to have a body"`

Open
#176,710 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

clang:frontend crash-on-invalid generated by fuzzer regression:20
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Generated by fuzzer.
Clang trunk crashed and goes back to 20.1 with -std=c++23 flag.
Compiler Explorer: https://godbolt.org/z/4T65c5x1c

Program

consteval void trigger_defect();
template < int > struct C {
public:
  static void f() {
    auto lambda = [](auto &&obj) -> decltype(0) {
      trigger_defect();
    };
    lambda(0);
  }
};
int main() {
  C<1>::f();
}

Stack Dump

<source>:8:5: error: call to immediate function 'C<1>::f()::(lambda)::operator()<int>' is not a constant expression
    8 |     lambda(0);
      |     ^
<source>:12:9: note: in instantiation of member function 'C<1>::f' requested here
   12 |   C<1>::f();
      |         ^
clang++: /root/llvm-project/llvm/tools/clang/lib/Sema/SemaDeclCXX.cpp:2604: void clang::Sema::DiagnoseImmediateEscalatingReason(clang::FunctionDecl*): Assertion `FD->hasBody() && "expected the function to have a body"' 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 -std=c++23 <source>
1.	<eof> parser at end of file
2.	<source>:4:15: instantiating function definition 'C<1>::f'
 #0 0x0000000004312838 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4312838)
 #1 0x000000000430fc94 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x430fc94)
 #2 0x00000000042528e8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007b4caba42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007b4caba969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007b4caba42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007b4caba287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007b4caba2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007b4caba39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000000006ebd455 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ebd455)
#10 0x0000000006fc94f7 clang::Sema::PopExpressionEvaluationContext() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6fc94f7)
#11 0x0000000006e5a3ca clang::Sema::ActOnFinishFunctionBody(clang::Decl*, clang::Stmt*, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e5a3ca)
#12 0x00000000076938d6 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x76938d6)
#13 0x0000000007691476 clang::Sema::PerformPendingInstantiations(bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7691476)
#14 0x0000000006c10772 clang::Sema::ActOnEndOfTranslationUnitFragment(clang::TUFragmentKind) (.part.0) Sema.cpp:0:0
#15 0x0000000006c10af2 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c10af2)
#16 0x0000000006a7b845 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a7b845)
#17 0x0000000006a5c7fa clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a5c7fa)
#18 0x0000000004c7ab78 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c7ab78)
#19 0x0000000004f74f85 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f74f85)
#20 0x0000000004ef495e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ef495e)
#21 0x000000000506fbfd clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x506fbfd)
#22 0x0000000000ddd1ce cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xddd1ce)
#23 0x0000000000dd3b9a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#24 0x0000000000dd3d1d 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
#25 0x0000000004cee0b9 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
#26 0x0000000004252d84 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4252d84)
#27 0x0000000004cee6cf 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
#28 0x0000000004caf0a2 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4caf0a2)
#29 0x0000000004cb004e 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+++0x4cb004e)
#30 0x0000000004cb7495 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4cb7495)
#31 0x0000000000dd9541 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdd9541)
#32 0x0000000000c96be4 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc96be4)
#33 0x00007b4caba29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#34 0x00007b4caba29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#35 0x0000000000dd3635 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdd3635)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash with the C++23 program and clang++ command shown in the issue. Start in llvm/tools/clang/lib/Sema/SemaDeclCXX.cpp around DiagnoseImmediateEscalatingReason and trace the function-instantiation path from the stack dump. Add regression coverage for this program and verify Clang reports the diagnostic without hitting the assertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.