template function with deduced return type specialization causes clang to crash
The following test now triggers a compiling failure in clang frontend:
template<class T, class ReturnType>
ReturnType f();
template<>
//OK
//int f<int>()
//crash
auto f<int>()
{
return 0;
}
The order of type deduction should be like that auto is first deduced to int according to the return 0 statement; then the template argument ReturnType is deduced to int from auto through CTAD. But if CTAD is performed first instead, there will probably be some problems. The bug seems to be related to that.
Godbolt:
https://godbolt.org/z/W8cWW6exn
Stack Dump:
0. Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -gdwarf-4 -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 <source>
1. <eof> parser at end of file
2. <source>:11:6: LLVM IR generation of declaration 'f'
3. <source>:11:6: Generating code for declaration 'f'
#0 0x0000000003828558 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3828558)
#1 0x00000000038266c4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x38266c4)
#2 0x0000000003770d68 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x000074c761c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x0000000003afa30c clang::CodeGen::CGDebugInfo::getOrCreateType(clang::QualType, llvm::DIFile*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3afa30c)
#5 0x0000000003affb89 clang::CodeGen::CGDebugInfo::CollectTemplateParams(std::optional<clang::CodeGen::CGDebugInfo::TemplateArgs>, llvm::DIFile*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3affb89)
#6 0x0000000003b0c44a clang::CodeGen::CGDebugInfo::collectFunctionDeclProps(clang::GlobalDecl, llvm::DIFile*, llvm::StringRef&, llvm::StringRef&, llvm::DIScope*&, llvm::MDTupleTypedArrayWrapper<llvm::DINode>&, llvm::DINode::DIFlags&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b0c44a)
#7 0x0000000003b0cde7 clang::CodeGen::CGDebugInfo::emitFunctionStart(clang::GlobalDecl, clang::SourceLocation, clang::SourceLocation, clang::QualType, llvm::Function*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b0cde7)
#8 0x0000000003bdfa68 clang::CodeGen::CodeGenFunction::StartFunction(clang::GlobalDecl, clang::QualType, llvm::Function*, clang::CodeGen::CGFunctionInfo const&, clang::CodeGen::FunctionArgList const&, clang::SourceLocation, clang::SourceLocation) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3bdfa68)
#9 0x0000000003be3609 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3be3609)
#10 0x0000000003c3d83b clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c3d83b)
#11 0x0000000003c39095 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c39095)
#12 0x0000000003c39a20 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c39a20)
#13 0x0000000003c43127 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#14 0x000000000410139e (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#15 0x00000000040f2ee9 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x40f2ee9)
#16 0x0000000006096f64 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x6096f64)
#17 0x00000000040ff1b5 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x40ff1b5)
#18 0x00000000043a4cf1 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43a4cf1)
#19 0x0000000004327aeb clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4327aeb)
#20 0x000000000448bcc3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x448bcc3)
#21 0x0000000000c90dac cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc90dac)
#22 0x0000000000c8aced ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#23 0x00000000041484c9 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
#24 0x0000000003771193 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3771193)
#25 0x00000000041486e9 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
#26 0x000000000410e237 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x410e237)
#27 0x000000000410ebed clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x410ebed)
#28 0x000000000411863c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x411863c)
#29 0x0000000000c8e091 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc8e091)
#30 0x0000000000b64294 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xb64294)
#31 0x000074c761c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#32 0x000074c761c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#33 0x0000000000c8a78e _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc8a78e)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
@llvm/issue-subscribers-clang-codegen
Author: Raki (Ming-3364)
template<class T, class ReturnType>
ReturnType f();
template<>
//OK
//int f<int>()
//crash
auto f<int>()
{
return 0;
}
The order of type deduction should be like that auto is first deduced to int according to the return 0 statement; then the template argument ReturnType is deduced to int from auto through CTAD. But if CTAD is performed first instead, there will probably be some problems. The bug seems to be related to that.
Godbolt:
https://godbolt.org/z/W8cWW6exn
Stack Dump:
0. Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -gdwarf-4 -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 <source>
1. <eof> parser at end of file
2. <source>:11:6: LLVM IR generation of declaration 'f'
3. <source>:11:6: Generating code for declaration 'f'
#<!-- -->0 0x0000000003828558 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3828558)
#<!-- -->1 0x00000000038266c4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x38266c4)
#<!-- -->2 0x0000000003770d68 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#<!-- -->3 0x000074c761c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#<!-- -->4 0x0000000003afa30c clang::CodeGen::CGDebugInfo::getOrCreateType(clang::QualType, llvm::DIFile*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3afa30c)
#<!-- -->5 0x0000000003affb89 clang::CodeGen::CGDebugInfo::CollectTemplateParams(std::optional<clang::CodeGen::CGDebugInfo::TemplateArgs>, llvm::DIFile*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3affb89)
#<!-- -->6 0x0000000003b0c44a clang::CodeGen::CGDebugInfo::collectFunctionDeclProps(clang::GlobalDecl, llvm::DIFile*, llvm::StringRef&, llvm::StringRef&, llvm::DIScope*&, llvm::MDTupleTypedArrayWrapper<llvm::DINode>&, llvm::DINode::DIFlags&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b0c44a)
#<!-- -->7 0x0000000003b0cde7 clang::CodeGen::CGDebugInfo::emitFunctionStart(clang::GlobalDecl, clang::SourceLocation, clang::SourceLocation, clang::QualType, llvm::Function*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b0cde7)
#<!-- -->8 0x0000000003bdfa68 clang::CodeGen::CodeGenFunction::StartFunction(clang::GlobalDecl, clang::QualType, llvm::Function*, clang::CodeGen::CGFunctionInfo const&, clang::CodeGen::FunctionArgList const&, clang::SourceLocation, clang::SourceLocation) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3bdfa68)
#<!-- -->9 0x0000000003be3609 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3be3609)
#<!-- -->10 0x0000000003c3d83b clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c3d83b)
#<!-- -->11 0x0000000003c39095 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c39095)
#<!-- -->12 0x0000000003c39a20 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c39a20)
#<!-- -->13 0x0000000003c43127 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#<!-- -->14 0x000000000410139e (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#<!-- -->15 0x00000000040f2ee9 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x40f2ee9)
#<!-- -->16 0x0000000006096f64 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x6096f64)
#<!-- -->17 0x00000000040ff1b5 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x40ff1b5)
#<!-- -->18 0x00000000043a4cf1 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43a4cf1)
#<!-- -->19 0x0000000004327aeb clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4327aeb)
#<!-- -->20 0x000000000448bcc3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x448bcc3)
#<!-- -->21 0x0000000000c90dac cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc90dac)
#<!-- -->22 0x0000000000c8aced ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#<!-- -->23 0x00000000041484c9 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
#<!-- -->24 0x0000000003771193 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3771193)
#<!-- -->25 0x00000000041486e9 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
#<!-- -->26 0x000000000410e237 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x410e237)
#<!-- -->27 0x000000000410ebed clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x410ebed)
#<!-- -->28 0x000000000411863c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x411863c)
#<!-- -->29 0x0000000000c8e091 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc8e091)
#<!-- -->30 0x0000000000b64294 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xb64294)
#<!-- -->31 0x000074c761c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#<!-- -->32 0x000074c761c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#<!-- -->33 0x0000000000c8a78e _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc8a78e)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
This still exists on the trunk