llvm / llvm-project

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

decltype in template lambdas crashes clang++ #57193

Open OgnianM opened 2 years ago

OgnianM commented 2 years ago
#include <vector>
template<typename T>
decltype(T::z) doesnt_crash(T& v) {
    decltype(v.z) i = 0;

    i += v.z;
    return i;
}

int main() {
    struct test {
        int z;
    };

    auto crashes = []<typename T>(test& t) {
        decltype(t.z) v;
        return v;
    };

    auto also_crashes = []<typename T>(T& t) {
        std::vector<decltype(t.z)> v;
        return v;
    };

    auto no_crash = [](test& t) {
        std::vector<decltype(t.z)> v;
        return v;
    };

    test t{5};
    //also_crashes(t);
    doesnt_crash(t);
    no_crash(t);

    crashes.operator()<void>(t);
    also_crashes(t);
}

Only happens with C++11

$ clang++ --version 
clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
shafik commented 2 years ago

See godbolt: https://gcc.godbolt.org/z/jq8jhfEK9

and backtrace:

Unexpected placeholder builtin type!
UNREACHABLE executed at /root/llvm-project/clang/lib/CodeGen/CodeGenTypes.cpp:641!
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++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++11 <source>
1.  <eof> parser at end of file
2.  <source>:10:5: LLVM IR generation of declaration 'main'
3.  <source>:10:5: Generating code for declaration 'main'
 #0 0x000055b4c49005a4 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055b4c48fe34c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3e1c34c)
 #2 0x000055b4c4835738 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007efc243013c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x143c0)
 #4 0x00007efc23dce03b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4303b)
 #5 0x00007efc23dad859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #6 0x000055b4c483ed7e (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3d5cd7e)
 #7 0x000055b4c4e88c5c clang::CodeGen::CodeGenTypes::ConvertType(clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43a6c5c)
 #8 0x000055b4c50677fc clang::CodeGen::CodeGenTypes::arrangeLLVMFunctionInfo(clang::CanQual<clang::Type>, bool, bool, llvm::ArrayRef<clang::CanQual<clang::Type>>, clang::FunctionType::ExtInfo, llvm::ArrayRef<clang::FunctionType::ExtParameterInfo>, clang::CodeGen::RequiredArgs) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45857fc)
 #9 0x000055b4c506af96 arrangeLLVMFunctionInfo(clang::CodeGen::CodeGenTypes&, bool, llvm::SmallVectorImpl<clang::CanQual<clang::Type>>&, clang::CanQual<clang::FunctionProtoType>) CGCall.cpp:0:0
#10 0x000055b4c506e2e4 clang::CodeGen::CodeGenTypes::arrangeCXXMethodType(clang::CXXRecordDecl const*, clang::FunctionProtoType const*, clang::CXXMethodDecl const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x458c2e4)
#11 0x000055b4c506e73d clang::CodeGen::CodeGenTypes::arrangeCXXMethodDeclaration(clang::CXXMethodDecl const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x458c73d)
#12 0x000055b4c511bb28 clang::CodeGen::CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr(clang::CallExpr const*, clang::CXXMethodDecl const*, clang::CodeGen::ReturnValueSlot, bool, clang::NestedNameSpecifier*, bool, clang::Expr const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4639b28)
#13 0x000055b4c511cbb1 clang::CodeGen::CodeGenFunction::EmitCXXMemberCallExpr(clang::CXXMemberCallExpr const*, clang::CodeGen::ReturnValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x463abb1)
#14 0x000055b4c5104723 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4622723)
#15 0x000055b4c5149d28 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
#16 0x000055b4c513f830 clang::StmtVisitorBase<std::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
#17 0x000055b4c5148db4 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4666db4)
#18 0x000055b4c50e262e clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x460062e)
#19 0x000055b4c5102591 clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4620591)
#20 0x000055b4c4d69ceb clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4287ceb)
#21 0x000055b4c4d70854 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x428e854)
#22 0x000055b4c4dcea0e clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42eca0e)
#23 0x000055b4c4de0c48 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42fec48)
#24 0x000055b4c4e3e804 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x435c804)
#25 0x000055b4c4e39ac5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4357ac5)
#26 0x000055b4c4e39f7b clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4357f7b)
#27 0x000055b4c4e430d4 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#28 0x000055b4c5c06369 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#29 0x000055b4c5bf8490 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5116490)
#30 0x000055b4c6ea3f94 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63c1f94)
#31 0x000055b4c5c03288 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5121288)
#32 0x000055b4c54a1409 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x49bf409)
#33 0x000055b4c542b03e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x494903e)
#34 0x000055b4c55811b3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4a9f1b3)
#35 0x000055b4c1ea9f94 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x13c7f94)
#36 0x000055b4c1ea30c8 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#37 0x000055b4c5299ff9 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
#38 0x000055b4c4835eda llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3d53eda)
#39 0x000055b4c529a70f clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#40 0x000055b4c5263e39 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4781e39)
#41 0x000055b4c52648bd 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+++0x47828bd)
#42 0x000055b4c526e68c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x478c68c)
#43 0x000055b4c1ea8373 clang_main(int, char**) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x13c6373)
#44 0x00007efc23daf0b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b3)
#45 0x000055b4c1ea2cbe _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x13c0cbe)
clang-16: error: clang frontend command failed with exit code 134 (use -v to see invocation)
llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-codegen