<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/70375>70375</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang ICE when compiling missing forward declaration of template specialization
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
yuxuanchen1997
</td>
</tr>
</table>
<pre>
Source code `repro_ice.cc`:
```
#include <coroutine>
template <typename Output>
struct Task {
template <typename POutput>
struct PromiseTypeImpl;
// Oops, I forgot to forward declare the specialization, but that caused a Clang ICE!
// template <>
// struct PromiseTypeImpl<void>;
using promise_type = PromiseTypeImpl<Output>;
using handle_type = std::coroutine_handle<promise_type>;
template <typename POutput>
struct PromiseTypeImpl {
Task<POutput> get_return_object() { return {handle_type::from_promise(*this)}; }
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_value(POutput expr) {}
void unhandled_exception() {}
};
template <>
struct PromiseTypeImpl<void> {
Task<void> get_return_object() { return {handle_type::from_promise(*this)}; }
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
handle_type hdl_;
};
Task<void> foo() { co_return; }
```
invocation:
```
clang++ -std=c++20 -pthread -stdlib=libc++ repro_ice.cc
```
Compiler Explorer: https://godbolt.org/z/1G85TGhMf
This issue doesn't seem to be completely restricted to using coroutines. But I cannot repro it otherwise. Template instantiation of just `Task<void>` seemed fine.
Stack trace:
```
1. <eof> parser at end of file
2. ./task.h:45:12: instantiating function definition 'coco::Task<>::PromiseTypeImpl<void>::get_return_object'
#0 0x00005605991d935e llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x378d35e)
#1 0x00005605991d74e9 llvm::sys::RunSignalHandlers() (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x378b4e9)
#2 0x00005605991d9f8d SignalHandler(int) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x378df8d)
#3 0x00007f8174c44540 __restore_rt /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/signal/../sysdeps/unix/sysv/linux/libc_sigaction.c:13:0
#4 0x000056059ba733b2 clang::Sema::FindInstantiatedDecl(clang::SourceLocation, clang::NamedDecl*, clang::MultiLevelTemplateArgumentList const&, bool) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x60273b2)
#5 0x000056059ba60693 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformRecordType(clang::TypeLocBuilder&, clang::RecordTypeLoc) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x6014693)
#6 0x000056059ba6037a clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformPointerType(clang::TypeLocBuilder&, clang::PointerTypeLoc) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x601437a)
#7 0x000056059ba3be87 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformType(clang::TypeSourceInfo*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x5fefe87)
#8 0x000056059ba47540 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCXXDependentScopeMemberExpr(clang::CXXDependentScopeMemberExpr*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x5ffb540)
#9 0x000056059ba49b94 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCallExpr(clang::CallExpr*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x5ffdb94)
#10 0x000056059ba43b17 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExprs(clang::Expr* const*, unsigned int, bool, llvm::SmallVectorImpl<clang::Expr*>&, bool*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x5ff7b17)
#11 0x000056059ba4c0f6 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformInitListExpr(clang::InitListExpr*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x60000f6)
#12 0x000056059ba66c38 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformReturnStmt(clang::ReturnStmt*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x601ac38)
#13 0x000056059ba5b206 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x600f206)
#14 0x000056059ba42312 clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x5ff6312)
#15 0x000056059ba82a88 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x6036a88)
#16 0x000056059ba850c1 clang::Sema::PerformPendingInstantiations(bool) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x60390c1)
#17 0x000056059b29205b clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x584605b)
#18 0x000056059b292667 clang::Sema::ActOnEndOfTranslationUnit() (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x5846667)
#19 0x000056059b17dfbf clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x5731fbf)
#20 0x000056059b179e7e clang::ParseAST(clang::Sema&, bool, bool) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x572de7e)
#21 0x0000560599c0b78a clang::FrontendAction::Execute() (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x41bf78a)
#22 0x0000560599b81484 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x4135484)
#23 0x0000560599cd1200 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x4285200)
#24 0x0000560597eb7aa8 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x246baa8)
#25 0x0000560597ea27d9 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x24567d9)
#26 0x0000560597ea17e7 clang_main(int, char**, llvm::ToolContext const&) (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x24557e7)
#27 0x0000560597ec3644 main (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x2477644)
#28 0x00007f8174c2c657 __libc_start_call_main /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#29 0x00007f8174c2c718 call_init /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/csu/../csu/libc-start.c:128:20
#30 0x00007f8174c2c718 __libc_start_main@GLIBC_2.2.5 /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/csu/../csu/libc-start.c:379:5
#31 0x0000560597e9eb61 _start (/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/bin/clang-15+0x2452b61)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWl1v2zjW_jXKDVFDIvV5kQvHqfsGb7stJpnF3BkUeWRzRia1JNUm8-sXpGRbVDwddGcU7MUWRSyLX895zgfPIU2NEXsJcBtld1F2f0N7e1D69qV_7qlkB5BJVRU3teIvt4-q1wwQUxxQlMcaOq12gsGKsSiPI7KO4vsoPv3N4_H_8BUTIVnbu6Fkw5RWvRUSIvJ-OsjCsWup9X3sSweSHgF97m3X23NPY3XPLHqi5jcUFXfDS4SuDv0yG4vGwV-0OgoDTy8dPBy7NiJ3UxQIRXgb4S36rDoT4Q16QI3Se2WRVe7pG9UccWAt1YDsAZDpgAnait-pFUq6EXVvkT1QixjtDXBE0aalco8eNu8jnMyWmUK_QD21_hHkzVcluOs-B98bIfeoG_rvHBcoIvdXJriQQ-7CwQcqeTsZayx36iXrs-J2Q5eIbKYLXYPzVxQzVTDyKo_I5jIW7cHuNNhey52qfwVmI1xGuHLD0PDePU6kGaRotDruRtx-xNoehIlwFRX3EblD7uO86ll205sOJN_R9ht9MUhIYQVtd-Prqyv_yHyNkK9mkwqeGXT2T6a9TO5sYuy4-0rb3ok38oXgudMjxAsgP6CXA0V8Nyznjbh81RcN6_6xeifG-2dWe1Wzp7b_qfV7anUkXdMO-uvKnPr9gbe7S4dXnWcqa5SaUMXUqL9QnnBLEPKrYkPEJGt0tQtzQTPCdxG-Q-88w_ds-Ipj9K6zBw2U-5ZW1BG5b0U9tqNgd7q6I_m_G3XsRAsavX_uWqVBOywHazvjtOkj8F7xWrV2pfQ-wtvfI7xNPpTZ04fDpyYg5CAMEsb0gLgCIyNcWGQAjm7XqN22eexasNC-IA3GasEscNc2hNxzZDUrdNdb9IAYlVLZQRAkLFL2APqbMLBCTyenE9JYKq3wNCLVoF97Y93mHKonymMPBbizR1ihKfBHS9lvyGrK4LKHh1wlK-T_RWQDqnH67qg2oBG1CCR3CzeihXFaPPZeRX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