llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.33k stars 11.7k forks source link

Clang++ crashes when instanciating a static data member declared as decltype(...) #110041

Open cassioneri opened 1 week ago

cassioneri commented 1 week ago

This program crashes clang++ (since version 12.0.0):

template <typename>
struct a {
    static char const b{};
    static decltype(&b) constexpr c{&b};
};

auto x = a<int>::c;

Either of the following changes make it to compile:

  1. replace decltype(&b) with auto;
  2. declare x as const.

See https://godbolt.org/z/1oxWGfE3T

Here is the crash report: --->8---

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: clang++ -c main.cpp
1.      <eof> parser at end of file
2.      main.cpp:7:6: LLVM IR generation of declaration 'x'
3.      main.cpp:7:6: Generating code for declaration 'x'
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVM-17.so      0x00007e1271581cb0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 64
1  libLLVM-17.so      0x00007e127157f18d llvm::sys::CleanupOnSignal(unsigned long) + 141
2  libLLVM-17.so      0x00007e127146f479
3  libc.so.6          0x00007e12709d1ae0
4  libLLVM-17.so      0x00007e127179d0f6 llvm::Type::isIntegerTy(unsigned int) const + 6
5  libclang-cpp.so.17 0x00007e127b4a09ce
6  libclang-cpp.so.17 0x00007e127a54a60d clang::CodeGen::ConstantEmitter::tryEmitPrivate(clang::APValue const&, clang::QualType) + 1469
7  libclang-cpp.so.17 0x00007e127a54af44 clang::CodeGen::ConstantEmitter::tryEmitPrivateForMemory(clang::APValue const&, clang::QualType) + 132
8  libclang-cpp.so.17 0x00007e127a54d024 clang::CodeGen::ConstantEmitter::tryEmitForInitializer(clang::VarDecl const&) + 52
9  libclang-cpp.so.17 0x00007e127a6e7f87 clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) + 503
10 libclang-cpp.so.17 0x00007e127a6f8d45 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) + 437
11 libclang-cpp.so.17 0x00007e127a6f94e3 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) + 1747
12 libclang-cpp.so.17 0x00007e127a700daf
13 libclang-cpp.so.17 0x00007e127a76aaf7
14 libclang-cpp.so.17 0x00007e127a671188
15 libclang-cpp.so.17 0x00007e12794a2d83 clang::ParseAST(clang::Sema&, bool, bool) + 579
16 libclang-cpp.so.17 0x00007e127ad6b4f9 clang::FrontendAction::Execute() + 393
17 libclang-cpp.so.17 0x00007e127ad57df7 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 391
18 libclang-cpp.so.17 0x00007e127ae3ac92 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 450
19 clang++            0x00005d6aac2a1c18 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 3928
20 clang++            0x00005d6aac2a710f
21 libclang-cpp.so.17 0x00007e127aa7cfdd
22 libLLVM-17.so      0x00007e127146f8ca llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 42
23 libclang-cpp.so.17 0x00007e127aa84174
24 libclang-cpp.so.17 0x00007e127aa454f1 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const + 161
25 libclang-cpp.so.17 0x00007e127aa458ad clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const + 253
26 libclang-cpp.so.17 0x00007e127aaa407c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) + 412
27 clang++            0x00005d6aac2a977b clang_main(int, char**, llvm::ToolContext const&) + 8939
28 clang++            0x00005d6aac29dd54 main + 52
29 libc.so.6          0x00007e12709bac88
30 libc.so.6          0x00007e12709bad4c __libc_start_main + 140
31 clang++            0x00005d6aac29dd95 _start + 37
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
clang version 17.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang++: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: /tmp/main-473bc6.cpp
clang++: note: diagnostic msg: /tmp/main-473bc6.sh
clang++: note: diagnostic msg: 

********************

--->8--- Preprocessed source and associated script attached (with extensions changed to txt to bypass GitHub's refusal to attach .cpp and .sh) main-473bc6-sh.txt main-473bc6-cpp.txt

llvmbot commented 1 week ago

@llvm/issue-subscribers-clang-codegen

Author: Cassio Neri (cassioneri)

This program crashes clang++ (since version 12.0.0): ``` template <typename> struct a { static char const b{}; static decltype(&b) constexpr c{&b}; }; auto x = a<int>::c; ``` Either of the following changes make it to compile: 1. replace `decltype(&b)` with `auto`; 2. declare `x` as `const`. See https://godbolt.org/z/1oxWGfE3T Here is the crash report: --->8--- ``` 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: clang++ -c main.cpp 1. <eof> parser at end of file 2. main.cpp:7:6: LLVM IR generation of declaration 'x' 3. main.cpp:7:6: Generating code for declaration 'x' Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it): 0 libLLVM-17.so 0x00007e1271581cb0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 64 1 libLLVM-17.so 0x00007e127157f18d llvm::sys::CleanupOnSignal(unsigned long) + 141 2 libLLVM-17.so 0x00007e127146f479 3 libc.so.6 0x00007e12709d1ae0 4 libLLVM-17.so 0x00007e127179d0f6 llvm::Type::isIntegerTy(unsigned int) const + 6 5 libclang-cpp.so.17 0x00007e127b4a09ce 6 libclang-cpp.so.17 0x00007e127a54a60d clang::CodeGen::ConstantEmitter::tryEmitPrivate(clang::APValue const&, clang::QualType) + 1469 7 libclang-cpp.so.17 0x00007e127a54af44 clang::CodeGen::ConstantEmitter::tryEmitPrivateForMemory(clang::APValue const&, clang::QualType) + 132 8 libclang-cpp.so.17 0x00007e127a54d024 clang::CodeGen::ConstantEmitter::tryEmitForInitializer(clang::VarDecl const&) + 52 9 libclang-cpp.so.17 0x00007e127a6e7f87 clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) + 503 10 libclang-cpp.so.17 0x00007e127a6f8d45 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) + 437 11 libclang-cpp.so.17 0x00007e127a6f94e3 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) + 1747 12 libclang-cpp.so.17 0x00007e127a700daf 13 libclang-cpp.so.17 0x00007e127a76aaf7 14 libclang-cpp.so.17 0x00007e127a671188 15 libclang-cpp.so.17 0x00007e12794a2d83 clang::ParseAST(clang::Sema&, bool, bool) + 579 16 libclang-cpp.so.17 0x00007e127ad6b4f9 clang::FrontendAction::Execute() + 393 17 libclang-cpp.so.17 0x00007e127ad57df7 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 391 18 libclang-cpp.so.17 0x00007e127ae3ac92 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 450 19 clang++ 0x00005d6aac2a1c18 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 3928 20 clang++ 0x00005d6aac2a710f 21 libclang-cpp.so.17 0x00007e127aa7cfdd 22 libLLVM-17.so 0x00007e127146f8ca llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 42 23 libclang-cpp.so.17 0x00007e127aa84174 24 libclang-cpp.so.17 0x00007e127aa454f1 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const + 161 25 libclang-cpp.so.17 0x00007e127aa458ad clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const + 253 26 libclang-cpp.so.17 0x00007e127aaa407c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) + 412 27 clang++ 0x00005d6aac2a977b clang_main(int, char**, llvm::ToolContext const&) + 8939 28 clang++ 0x00005d6aac29dd54 main + 52 29 libc.so.6 0x00007e12709bac88 30 libc.so.6 0x00007e12709bad4c __libc_start_main + 140 31 clang++ 0x00005d6aac29dd95 _start + 37 clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation) clang version 17.0.6 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin clang++: note: diagnostic msg: ******************** PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang++: note: diagnostic msg: /tmp/main-473bc6.cpp clang++: note: diagnostic msg: /tmp/main-473bc6.sh clang++: note: diagnostic msg: ******************** ``` --->8--- Preprocessed source and associated script attached (with extensions changed to `txt` to bypass GitHub's refusal to attach `.cpp` and `.sh`) [main-473bc6-sh.txt](https://github.com/user-attachments/files/17138569/main-473bc6-sh.txt) [main-473bc6-cpp.txt](https://github.com/user-attachments/files/17138553/main-473bc6-cpp.txt)
shafik commented 1 week ago

It looks a lot like: https://github.com/llvm/llvm-project/issues/88104

CC @zyn0217

Assertion: https://godbolt.org/z/x8KMz5vK8

clang++: /root/llvm-project/clang/lib/AST/Decl.cpp:2550:
clang::APValue* clang::VarDecl::evaluateValueImpl(llvm::SmallVectorImpl<std::pair<clang::SourceLocation, clang::PartialDiagnostic> >&, bool) const:
Assertion `!Init->isValueDependent()' failed.