Open
Description
Description
I encountered a crash in Clang-20 when compiling the following code snippet:
#include <iostream>
template <bool compare>
struct outer
{
template <bool compare_with,bool second>
struct inner // unspecialized compare != compare_with
{
static inline void test()
{
std::cout << "invert" << std::endl;
// call version with inverted template value
inner<! compare_with, ! second>::test();
}
};
template <bool second> // specialization compare == compare_with
struct inner<compare,second>
{
static inline void test()
{
std::cout << "second: " << second << std::endl;
}
};
};
int main(int argc,char* argv[],char** envp)
{
// expected output:
// invert
// second: true
outer<false>::inner<true,false>::test();
// expected output:
// second: false
outer<true>::inner<true,false>::test();
return 0;
}
template <bool B1> struct outer {
template <bool B2, bool B3>
struct inner {
static int f() { return inner<!B2,!B3>::N; };
};
template <bool B3>
struct inner<B1,B3> {
static const int N = 1;
};
};
int i = outer<false>::inner<true,false>::f();
template <bool B1> struct outer {
template <bool B2>
struct inner {
static int f() { return inner<!B2>::N; };
};
template <>
struct inner<B1> {
static const int N = 1;
};
};
int i = outer<false>::inner<true>::f();
template <typename T> struct outer {
template <typename T2, typename U>
struct inner {
static int f() { return inner<T,int>::N; };
};
template <typename U>
struct inner<T,U> {
static const int N = 1;
};
};
int i = outer<int>::inner<double,int>::f();
Command
The compilation command used was:
clang++ -fno-rtlib-add-rpath -fno-ident -std=c++23 -Wall -Wextra -fno-strict-aliasing -fwrapv -g -fsanitize=address test.cpp
Error Message
<source>:26:14: warning: unused parameter 'argc' [-Wunused-parameter]
26 | int main(int argc,char* argv[],char** envp)
| ^
<source>:26:25: warning: unused parameter 'argv' [-Wunused-parameter]
26 | int main(int argc,char* argv[],char** envp)
| ^
<source>:26:39: warning: unused parameter 'envp' [-Wunused-parameter]
26 | int main(int argc,char* argv[],char** envp)
| ^
<source>:40:27: error: redefinition of 'outer'
40 | template <bool B1> struct outer {
| ^
<source>:4:8: note: previous definition is here
4 | struct outer
| ^
<source>:52:42: error: no member named 'f' in 'outer<false>::inner<true, false>'
52 | int i = outer<false>::inner<true,false>::f();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
<source>:54:27: error: redefinition of 'outer'
54 | template <bool B1> struct outer {
| ^
<source>:4:8: note: previous definition is here
4 | struct outer
| ^
<source>:66:5: error: redefinition of 'i'
66 | int i = outer<false>::inner<true>::f();
| ^
<source>:52:5: note: previous definition is here
52 | int i = outer<false>::inner<true,false>::f();
| ^
<source>:66:23: error: too few template arguments for class template 'inner'
66 | int i = outer<false>::inner<true>::f();
| ^
<source>:7:10: note: template is declared here
6 | template <bool compare_with,bool second>
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 | struct inner // unspecialized compare != compare_with
| ^
<source>:68:20: error: template parameter has a different kind in template redeclaration
68 | template <typename T> struct outer {
| ^
<source>:3:16: note: previous template declaration is here
3 | template <bool compare>
| ^
<source>:80:5: error: redefinition of 'i'
80 | int i = outer<int>::inner<double,int>::f();
| ^
<source>:52:5: note: previous definition is here
52 | int i = outer<false>::inner<true,false>::f();
| ^
not a non-type template argument
UNREACHABLE executed at /root/llvm-project/clang/lib/Sema/SemaTemplate.cpp:7688!
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 -fno-rtlib-add-rpath -fno-ident -std=c++23 -Wall -Wextra -fno-strict-aliasing -fwrapv -g -fsanitize=address <source>
1. <source>:80:21: current parser token 'inner'
2. <source>:4:8: instantiating class definition 'outer<int>'
#0 0x0000000003be87b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3be87b8)
#1 0x0000000003be64bc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3be64bc)
#2 0x0000000003b33df8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007f77ff642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007f77ff6969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x00007f77ff642476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x00007f77ff6287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x0000000003b3f71a (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b3f71a)
#8 0x0000000007244d04 clang::Sema::BuildExpressionFromNonTypeTemplateArgument(clang::TemplateArgument const&, clang::SourceLocation) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7244d04)
#9 0x000000000747c1c3 (anonymous namespace)::TemplateInstantiator::transformNonTypeTemplateParmRef(clang::Decl*, clang::NonTypeTemplateParmDecl const*, clang::SourceLocation, clang::TemplateArgument, std::optional<unsigned int>) SemaTemplateInstantiate.cpp:0:0
#10 0x000000000748df0d (anonymous namespace)::TemplateInstantiator::TransformDeclRefExpr(clang::DeclRefExpr*) SemaTemplateInstantiate.cpp:0:0
#11 0x000000000746252d clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#12 0x0000000007481983 (anonymous namespace)::TemplateInstantiator::TransformTemplateArgument(clang::TemplateArgumentLoc const&, clang::TemplateArgumentLoc&, bool) SemaTemplateInstantiate.cpp:0:0
#13 0x0000000007485065 bool clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformTemplateArguments<clang::TemplateArgumentLoc const*>(clang::TemplateArgumentLoc const*, clang::TemplateArgumentLoc const*, clang::TemplateArgumentListInfo&, bool) (.constprop.0) SemaTemplateInstantiate.cpp:0:0
#14 0x0000000007485507 clang::Sema::SubstTemplateArguments(llvm::ArrayRef<clang::TemplateArgumentLoc>, clang::MultiLevelTemplateArgumentList const&, clang::TemplateArgumentListInfo&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7485507)
#15 0x000000000751cabb clang::TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(clang::ClassTemplateDecl*, clang::ClassTemplatePartialSpecializationDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x751cabb)
#16 0x000000000748717b clang::Sema::InstantiateClass(clang::SourceLocation, clang::CXXRecordDecl*, clang::CXXRecordDecl*, clang::MultiLevelTemplateArgumentList const&, clang::TemplateSpecializationKind, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x748717b)
#17 0x0000000007488e6e clang::Sema::InstantiateClassTemplateSpecialization(clang::SourceLocation, clang::ClassTemplateSpecializationDecl*, clang::TemplateSpecializationKind, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7488e6e)
#18 0x000000000754af0f void llvm::function_ref<void ()>::callback_fn<clang::Sema::RequireCompleteTypeImpl(clang::SourceLocation, clang::QualType, clang::Sema::CompleteTypeKind, clang::Sema::TypeDiagnoser*)::'lambda'()>(long) SemaType.cpp:0:0
#19 0x00000000081be4d1 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x81be4d1)
#20 0x00000000075543b7 clang::Sema::RequireCompleteTypeImpl(clang::SourceLocation, clang::QualType, clang::Sema::CompleteTypeKind, clang::Sema::TypeDiagnoser*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x75543b7)
#21 0x0000000007554705 clang::Sema::RequireCompleteType(clang::SourceLocation, clang::QualType, clang::Sema::CompleteTypeKind, clang::Sema::TypeDiagnoser&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7554705)
#22 0x00000000068e9f6c clang::Sema::RequireCompleteDeclContext(clang::CXXScopeSpec&, clang::DeclContext*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x68e9f6c)
#23 0x0000000007251146 clang::Sema::LookupTemplateName(clang::LookupResult&, clang::Scope*, clang::CXXScopeSpec&, clang::QualType, bool, clang::Sema::RequiredTemplateKind, clang::Sema::AssumedTemplateKind*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7251146)
#24 0x0000000007252709 clang::Sema::isTemplateName(clang::Scope*, clang::CXXScopeSpec&, bool, clang::UnqualifiedId const&, clang::OpaquePtr<clang::QualType>, bool, clang::OpaquePtr<clang::TemplateName>&, bool&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7252709)
#25 0x0000000006776d3e clang::Parser::ParseOptionalCXXScopeSpecifier(clang::CXXScopeSpec&, clang::OpaquePtr<clang::QualType>, bool, bool, bool*, bool, clang::IdentifierInfo const**, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6776d3e)
#26 0x00000000066e8603 clang::Parser::TryAnnotateTypeOrScopeToken(clang::ImplicitTypenameContext) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66e8603)
#27 0x000000000675c63e clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x675c63e)
#28 0x000000000675e567 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x675e567)
#29 0x000000000675e5f9 clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x675e5f9)
#30 0x0000000006717f28 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6717f28)
#31 0x0000000006726f89 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6726f89)
#32 0x00000000066e6fae clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66e6fae)
#33 0x00000000066e776e clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66e776e)
#34 0x00000000066eef03 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66eef03)
#35 0x00000000066efded clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66efded)
#36 0x00000000066e230a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66e230a)
#37 0x0000000004560df8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4560df8)
#38 0x000000000481b949 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x481b949)
#39 0x000000000479abbe clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x479abbe)
#40 0x00000000049016de clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x49016de)
#41 0x0000000000ce5b8f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xce5b8f)
#42 0x0000000000cdd84a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#43 0x00000000045a3f79 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
#44 0x0000000003b342a4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b342a4)
#45 0x00000000045a456f 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
#46 0x000000000456a8dd clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x456a8dd)
#47 0x000000000456b9cd 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+++0x456b9cd)
#48 0x0000000004572d85 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4572d85)
#49 0x0000000000ce2a29 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xce2a29)
#50 0x0000000000bafbc4 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xbafbc4)
#51 0x00007f77ff629d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#52 0x00007f77ff629e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#53 0x0000000000cdd2fe _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcdd2fe)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
The issue can also be reproduced on Compiler Explorer:https://godbolt.org/z/91cMa4abK
Please let me know if you need any more details.