Passing templated function-local class as template parameter causes infinite recursion in instantiation
| Bugzilla Link | 48839 |
| Version | trunk |
| OS | All |
| CC | @DougGregor,@futog,@zygoloid |
Extended Description
The following compiles with gcc but does not with clang:
template<typename T>
void construct() {
T(0);
}
template<typename T>
void tester() {
struct d {
void test() {
construct<d>();
}
constexpr d(T b) : a(b) {}
T a;
};
}
int main() {
tester<int>();
}
link: https://godbolt.org/z/jd41rr
It fails with "fatal error: recursive template instantiation exceeded maximum depth of 1024"
Different issue, but this should also work right?
template<typename T>
void construct() {
constexpr T t(0);
}
template<typename T>
void tester() {
struct d {
void test() {
construct<d>();
}
constexpr d(T b) : a(b) {}
T a;
};
}
int main() {
tester<int>();
}
clang fails with following error:
<source>:3:17: error: constexpr variable 't' must be initialized by a constant expression
constexpr T t(0);
^~~~
link: https://godbolt.org/z/93ecvjE5h
https://reviews.llvm.org/D103595
Hi, I'm new to clang, but do you mind if I start working on this? Thanks, Gergely
This appears to have regressed between Clang 3.3 and 3.4.
From the diagnostics:
<source>:8:12: note: in instantiation of member function 'tester()::d::d(int)::d::test' requested here
... it looks like we're creating a duplicate local struct d within the d::d(int) constructor (and so on, recursively).
@llvm/issue-subscribers-clang-frontend
Author: None (ec7eeb7f-58bd-4c66-a038-16c3e01dbcce)
Extended Description
The following compiles with gcc but does not with clang:
template<typename T>
void construct() {
T(0);
}
template<typename T>
void tester() {
struct d {
void test() {
construct<d>();
}
constexpr d(T b) : a(b) {}
T a;
};
}
int main() {
tester<int>();
}
link: https://godbolt.org/z/jd41rr
It fails with "fatal error: recursive template instantiation exceeded maximum depth of 1024"
Still crashes: https://godbolt.org/z/c9xMx9o8W
Assertion:
clang++: /root/llvm-project/clang/lib/Sema/SemaTemplateInstantiate.cpp:4664:
void clang::LocalInstantiationScope::InstantiatedLocal(const clang::Decl*, clang::Decl*):
Assertion `!Current->LocalDecls.contains(D) && "Instantiated local in inner and outer scopes"' failed.
Backtrace:
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 -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 <source>
1. <eof> parser at end of file
2. <source>:7:10: instantiating function definition 'tester<int>'
3. <source>:9:18: instantiating function definition 'tester()::d::test'
4. <source>:2:10: instantiating function definition 'construct<d>'
5. <source>:12:23: instantiating function definition 'tester()::d::d'
6. <source>:8:16: instantiating class definition 'd'
#0 0x0000000003e7dc08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3e7dc08)
#1 0x0000000003e7b8c4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3e7b8c4)
#2 0x0000000003dc0618 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007391d8e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007391d8e969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x00007391d8e42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x00007391d8e287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x00007391d8e2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#8 0x00007391d8e39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#9 0x0000000006fa5c3a clang::LocalInstantiationScope::InstantiatedLocal(clang::Decl const*, clang::Decl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6fa5c3a)
#10 0x0000000006fcc998 clang::Sema::SubstParmVarDecl(clang::ParmVarDecl*, clang::MultiLevelTemplateArgumentList const&, int, std::optional<unsigned int>, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6fcc998)
#11 0x0000000006fcd720 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformFunctionTypeParams(clang::SourceLocation, llvm::ArrayRef<clang::ParmVarDecl*>, clang::QualType const*, clang::FunctionType::ExtParameterInfo const*, llvm::SmallVectorImpl<clang::QualType>&, llvm::SmallVectorImpl<clang::ParmVarDecl*>*, clang::Sema::ExtParameterInfoBuilder&, unsigned int*) SemaTemplateInstantiate.cpp:0:0
#12 0x000000000701270d clang::Sema::SubstFunctionDeclType(clang::TypeSourceInfo*, clang::MultiLevelTemplateArgumentList const&, clang::SourceLocation, clang::DeclarationName, clang::CXXRecordDecl*, clang::Qualifiers, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x701270d)
#13 0x0000000007035c9e clang::TemplateDeclInstantiator::SubstFunctionType(clang::FunctionDecl*, llvm::SmallVectorImpl<clang::ParmVarDecl*>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7035c9e)
#14 0x0000000007094d0a clang::TemplateDeclInstantiator::VisitCXXMethodDecl(clang::CXXMethodDecl*, clang::TemplateParameterList*, clang::TemplateDeclInstantiator::RewriteKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7094d0a)
#15 0x0000000006ff72cb clang::Sema::InstantiateClass(clang::SourceLocation, clang::CXXRecordDecl*, clang::CXXRecordDecl*, clang::MultiLevelTemplateArgumentList const&, clang::TemplateSpecializationKind, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ff72cb)
#16 0x000000000708998f clang::TemplateDeclInstantiator::VisitCXXRecordDecl(clang::CXXRecordDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x708998f)
#17 0x0000000007096c74 void llvm::function_ref<void ()>::callback_fn<clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&)::'lambda'()>(long) SemaTemplateInstantiateDecl.cpp:0:0
#18 0x0000000007d0a621 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7d0a621)
#19 0x0000000007035a5a clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7035a5a)
#20 0x0000000007083991 clang::Sema::FindInstantiatedDecl(clang::SourceLocation, clang::NamedDecl*, clang::MultiLevelTemplateArgumentList const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7083991)
#21 0x0000000007084412 clang::Sema::FindInstantiatedContext(clang::SourceLocation, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7084412)
#22 0x00000000070835db clang::Sema::FindInstantiatedDecl(clang::SourceLocation, clang::NamedDecl*, clang::MultiLevelTemplateArgumentList const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x70835db)
#23 0x0000000007084ae6 clang::Sema::InstantiateMemInitializers(clang::CXXConstructorDecl*, clang::CXXConstructorDecl const*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7084ae6)
#24 0x000000000708aaf5 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x708aaf5)
#25 0x0000000007088aa6 clang::Sema::PerformPendingInstantiations(bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7088aa6)
#26 0x000000000708a3f9 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x708a3f9)
#27 0x0000000007088aa6 clang::Sema::PerformPendingInstantiations(bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7088aa6)
#28 0x000000000708ade3 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x708ade3)
#29 0x0000000007088aa6 clang::Sema::PerformPendingInstantiations(bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7088aa6)
#30 0x00000000070899e9 clang::TemplateDeclInstantiator::VisitCXXRecordDecl(clang::CXXRecordDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x70899e9)
#31 0x0000000007096c74 void llvm::function_ref<void ()>::callback_fn<clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&)::'lambda'()>(long) SemaTemplateInstantiateDecl.cpp:0:0
#32 0x0000000007d0a621 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7d0a621)
#33 0x0000000007035a5a clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7035a5a)
#34 0x0000000006fa611d clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformDeclStmt(clang::DeclStmt*) SemaTemplateInstantiate.cpp:0:0
#35 0x000000000700713e clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#36 0x0000000007010174 clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7010174)
#37 0x000000000708ab25 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x708ab25)
#38 0x0000000007088aa6 clang::Sema::PerformPendingInstantiations(bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7088aa6)
#39 0x00000000065dd712 clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.0) Sema.cpp:0:0
#40 0x00000000065ddf62 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x65ddf62)
#41 0x00000000064498ab clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x64498ab)
#42 0x000000000643bc5a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x643bc5a)
#43 0x0000000004837528 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4837528)
#44 0x0000000004b00005 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b00005)
#45 0x0000000004a8398e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4a8398e)
#46 0x0000000004bedf9e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bedf9e)
#47 0x0000000000d5391f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd5391f)
#48 0x0000000000d4b2da ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#49 0x000000000487fa49 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
#50 0x0000000003dc0ac4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3dc0ac4)
#51 0x000000000488003f 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
#52 0x00000000048426fd clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48426fd)
#53 0x000000000484377e 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+++0x484377e)
#54 0x000000000484b695 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x484b695)
#55 0x0000000000d5076c clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd5076c)
#56 0x0000000000c17ff4 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc17ff4)
#57 0x00007391d8e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#58 0x00007391d8e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#59 0x0000000000d4ad85 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd4ad85)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
@futog Would you be willing to make a github PR for this phabricator change? https://reviews.llvm.org/D103595 I apologize no one reviewed it before
@futog Would you be willing to make a github PR for this phabricator change? https://reviews.llvm.org/D103595 I apologize no one reviewed it before
Yes, I will make a PR. Regards, Gergely