<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/101674>101674</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
template function with deduced return type specialization causes clang to crash
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Ming-3364
</td>
</tr>
</table>
<pre>
The following test now triggers a compiling failure in clang frontend:
```c++
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](https://godbolt.org/z/W8cWW6exn)
Stack Dump:
```c++
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)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWlGTmzjy_zTMiwoXCIzhwQ8eO5N_9p_c5jJTyaNLiAbrIhAliRnPfvorSdiDHHt2kvVeXdWlHI9RS61f_7qlbgFEKdZ0AMtgfhvMNzdk0Dshl59Y14RJkqU3paielw87QLXgXDyxrkEalEadeEJasqYBqRBBVLQ940ZaE8YHCYh1iHJiGqToNHRVkKyCaBNEh-8sch8a4Fvzsa0a2p4TDUGyppwohR4CvEbu5xfQg-wennsIkneu-0sTqgOcB7gIktvpLBN9x0EBvgvw3e__P71inUZ1kKxZp00_p2oip5KonWsggxaX-i7GyRFCSFpsKHpBtNicmO4uDbtCViCRqJE2tlRQDVQz0SG1EwOvUAmIs--A9I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