llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang18][Concepts] Using static data member as concept argument in partially specialized template fails to compile

Open kaimfrai opened this issue 2 years ago • 8 comments

The following example compiles with Clang 17 but not in Clang 18 (since approximately 1 week):

template<typename T, typename U, int i>
concept True = true;

template<typename T, int I>
struct Template
{
    static constexpr int i = I;

    friend constexpr auto operator+(True<T, i> auto f)
    {
        return i;
    }
};

template<int I>
struct Template<float, I>
{
    static constexpr int i = I;

    friend constexpr auto operator+(True<float, i> auto f)
    {
        return i;
    }
};

Template<void, 4> f{};

static_assert(+Template<float, 5>{} == 5);

With -std=c++20 on clang 18 I get:

<source>:9:45: error: no member 'i' in 'Template<float, 5>'; it has not yet been instantiated

The error is caused in the static_assert. What's interesting is that it's refering to line 9, which is not the specialization Template<float, 5>. The error goes away if I remove the variable f, when I don't have a partial specialization, or I use I directly. This is a problem when I don't want to just use the value of I but the result of a complex expression dependent on I and that result is repeatedly used throughout the specialization, not just 1 function.

kaimfrai avatar Jan 14 '24 16:01 kaimfrai

@llvm/issue-subscribers-clang-frontend

Author: Kai Müller (kaimfrai)

The following example compiles with Clang 17 but not in Clang 18 (since approximately 1 week):
template&lt;typename T, typename U, int i&gt;
concept True = true;

template&lt;typename T, int I&gt;
struct Template
{
    static constexpr int i = I;

    friend constexpr auto operator+(True&lt;T, i&gt; auto f)
    {
        return i;
    }
};

template&lt;int I&gt;
struct Template&lt;float, I&gt;
{
    static constexpr int i = I;

    friend constexpr auto operator+(True&lt;float, i&gt; auto f)
    {
        return i;
    }
};

Template&lt;void, 4&gt; f{};

static_assert(+Template&lt;float, 5&gt;{} == 5);

With -std=c++20 on clang 18 I get:

&lt;source&gt;:9:45: error: no member 'i' in 'Template&lt;float, 5&gt;'; it has not yet been instantiated

The error is caused in the static_assert. What's interesting is that it's refering to line 9, which is not the specialization Template&lt;float, 5&gt;. The error goes away if I remove the variable f, when I don't have a partial specialization, or I use I directly. This is a problem when I don't want to just use the value of I but the result of a complex expression dependent on I and that result is repeatedly used throughout the specialization, not just 1 function.

llvmbot avatar Jan 14 '24 16:01 llvmbot

@llvm/issue-subscribers-c-20

Author: Kai Müller (kaimfrai)

The following example compiles with Clang 17 but not in Clang 18 (since approximately 1 week):
template&lt;typename T, typename U, int i&gt;
concept True = true;

template&lt;typename T, int I&gt;
struct Template
{
    static constexpr int i = I;

    friend constexpr auto operator+(True&lt;T, i&gt; auto f)
    {
        return i;
    }
};

template&lt;int I&gt;
struct Template&lt;float, I&gt;
{
    static constexpr int i = I;

    friend constexpr auto operator+(True&lt;float, i&gt; auto f)
    {
        return i;
    }
};

Template&lt;void, 4&gt; f{};

static_assert(+Template&lt;float, 5&gt;{} == 5);

With -std=c++20 on clang 18 I get:

&lt;source&gt;:9:45: error: no member 'i' in 'Template&lt;float, 5&gt;'; it has not yet been instantiated

The error is caused in the static_assert. What's interesting is that it's refering to line 9, which is not the specialization Template&lt;float, 5&gt;. The error goes away if I remove the variable f, when I don't have a partial specialization, or I use I directly. This is a problem when I don't want to just use the value of I but the result of a complex expression dependent on I and that result is repeatedly used throughout the specialization, not just 1 function.

llvmbot avatar Jan 14 '24 16:01 llvmbot

This currently crashes on HEAD https://godbolt.org/z/jY5Yrsvoa:

clang++: /root/llvm-project/clang/include/clang/AST/Decl.h:276: llvm::StringRef clang::NamedDecl::getName() const: Assertion `Name.isIdentifier() && "Name is not a simple identifier"' failed.

69066ab31959968ebcbca71f3872bdedef8fb8cd is the first commit where this crashes (the previous one compiles this without error or crash)

@cor3ntin

MitalAshok avatar Jan 14 '24 23:01 MitalAshok

Thanks for the quick fix! Unfortunately, after updating clang the issue I had still persists. The reproducer I provided does not appear to have reflected my problem properly. I changed the example a bit and now it ceased to work on Clang 18 and 17, but still works on Clang 16 and GCC.

template<typename T, int i>
concept True = true;

template<typename T, int I>
struct Template
{
    static constexpr int i = I;

    friend constexpr auto operator+(True<i> auto f)
    {
        return i;
    }
};

template<int I>
struct Template<float, I>
{
    static constexpr int i = I;

    friend constexpr auto operator+(True<i> auto f)
    {
        return i;
    }
};

Template<void, 4> f{};

static_assert(+Template<float, 5>{} == 5);

Note that all I did was remove the type parameter from the concept.

https://godbolt.org/z/156133ETT

kaimfrai avatar Jan 20 '24 11:01 kaimfrai

Trying out your reproducer w/ asserts trunk shows that is crashes clang and apparently edg as well: https://godbolt.org/z/3f7bvETfK

Assert:

clang++: /root/llvm-project/clang/include/clang/AST/Decl.h:277:
llvm::StringRef clang::NamedDecl::getName() const:
Assertion `Name.isIdentifier() && "Name is not a simple identifier"' 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 -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 <source>
1.	<source>:28:37: current parser token '=='
2.	<source>:5:8: instantiating class definition 'Template<float, 5>'
 #0 0x000000000380b888 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x380b888)
 #1 0x000000000380956c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x380956c)
 #2 0x0000000003751fd8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f2c0ce42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f2c0ce969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007f2c0ce42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007f2c0ce287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007f2c0ce2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007f2c0ce39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000000006b46fcd clang::Sema::PrintInstantiationStack() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b46fcd)
#10 0x000000000618bab0 clang::Sema::EmitCurrentDiagnostic(unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x618bab0)
#11 0x000000000618bdf1 clang::Sema::ImmediateDiagBuilder::~ImmediateDiagBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x618bdf1)
#12 0x000000000618c640 clang::Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x618c640)
#13 0x0000000006bd5b21 clang::Sema::FindInstantiatedDecl(clang::SourceLocation, clang::NamedDecl*, clang::MultiLevelTemplateArgumentList const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6bd5b21)
#14 0x0000000006b3c181 (anonymous namespace)::TemplateInstantiator::TransformDecl(clang::SourceLocation, clang::Decl*) SemaTemplateInstantiate.cpp:0:0
#15 0x0000000006b729b0 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformDeclRefExpr(clang::DeclRefExpr*) SemaTemplateInstantiate.cpp:0:0
#16 0x0000000006b72e8b (anonymous namespace)::TemplateInstantiator::TransformDeclRefExpr(clang::DeclRefExpr*) SemaTemplateInstantiate.cpp:0:0
#17 0x0000000006b4fc1f clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#18 0x0000000006b6898b clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformTemplateArgument(clang::TemplateArgumentLoc const&, clang::TemplateArgumentLoc&, bool) SemaTemplateInstantiate.cpp:0:0
#19 0x0000000006b6be65 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
#20 0x0000000006b6c35e clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformConceptSpecializationExpr(clang::ConceptSpecializationExpr*) SemaTemplateInstantiate.cpp:0:0
#21 0x0000000006b4fbf7 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#22 0x0000000006b82f48 clang::Sema::SubstConstraintExprWithoutSatisfaction(clang::Expr*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b82f48)
#23 0x00000000062c598c SubstituteConstraintExpressionWithoutSatisfaction(clang::Sema&, clang::Sema::TemplateCompareNewDeclInfo const&, clang::Expr const*) SemaConcept.cpp:0:0
#24 0x00000000062d18a2 clang::Sema::AreConstraintExpressionsEqual(clang::NamedDecl const*, clang::Expr const*, clang::Sema::TemplateCompareNewDeclInfo const&, clang::Expr const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x62d18a2)
#25 0x00000000069f4fb4 MatchTemplateParameterKind(clang::Sema&, clang::NamedDecl*, clang::Sema::TemplateCompareNewDeclInfo const&, clang::NamedDecl*, clang::NamedDecl const*, bool, clang::Sema::TemplateParameterListEqualKind, clang::SourceLocation) SemaTemplate.cpp:0:0
#26 0x00000000069f482a clang::Sema::TemplateParameterListsAreEqual(clang::Sema::TemplateCompareNewDeclInfo const&, clang::TemplateParameterList*, clang::NamedDecl const*, clang::TemplateParameterList*, bool, clang::Sema::TemplateParameterListEqualKind, clang::SourceLocation) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x69f482a)
#27 0x0000000006912874 IsOverloadOrOverrideImpl(clang::Sema&, clang::FunctionDecl*, clang::FunctionDecl*, bool, bool, bool) SemaOverload.cpp:0:0
#28 0x0000000006913166 clang::Sema::CheckOverload(clang::Scope*, clang::FunctionDecl*, clang::LookupResult const&, clang::NamedDecl*&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6913166)
#29 0x000000000639e863 clang::Sema::CheckFunctionDeclaration(clang::Scope*, clang::FunctionDecl*, clang::LookupResult&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x639e863)
#30 0x0000000006be3662 clang::TemplateDeclInstantiator::VisitFunctionDecl(clang::FunctionDecl*, clang::TemplateParameterList*, clang::TemplateDeclInstantiator::RewriteKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6be3662)
#31 0x0000000006be937d clang::TemplateDeclInstantiator::VisitFunctionTemplateDecl(clang::FunctionTemplateDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6be937d)
#32 0x0000000006be6bdb clang::TemplateDeclInstantiator::VisitFriendDecl(clang::FriendDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6be6bdb)
#33 0x0000000006b6df6b clang::Sema::InstantiateClass(clang::SourceLocation, clang::CXXRecordDecl*, clang::CXXRecordDecl*, clang::MultiLevelTemplateArgumentList const&, clang::TemplateSpecializationKind, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b6df6b)
#34 0x0000000006b8c0ab clang::Sema::InstantiateClassTemplateSpecialization(clang::SourceLocation, clang::ClassTemplateSpecializationDecl*, clang::TemplateSpecializationKind, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b8c0ab)
#35 0x0000000006c1745f 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
#36 0x000000000617a911 clang::Sema::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x617a911)
#37 0x0000000006c208a7 clang::Sema::RequireCompleteTypeImpl(clang::SourceLocation, clang::QualType, clang::Sema::CompleteTypeKind, clang::Sema::TypeDiagnoser*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c208a7)
#38 0x0000000006c20bf5 clang::Sema::RequireCompleteType(clang::SourceLocation, clang::QualType, clang::Sema::CompleteTypeKind, clang::Sema::TypeDiagnoser&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6c20bf5)
#39 0x00000000066a5767 clang::Sema::BuildCXXTypeConstructExpr(clang::TypeSourceInfo*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x66a5767)
#40 0x000000000673212d clang::Sema::ActOnCXXTypeConstructExpr(clang::OpaquePtr<clang::QualType>, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x673212d)
#41 0x00000000060ab9d6 clang::Parser::ParseCXXTypeConstructExpression(clang::DeclSpec const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x60ab9d6)
#42 0x000000000608e565 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x608e565)
#43 0x000000000608e3ad clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x608e3ad)
#44 0x00000000060900da clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x60900da)
#45 0x000000000608d50b clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x608d50b)
#46 0x000000000609be96 clang::Parser::ParseConstantExpressionInExprEvalContext(clang::Parser::TypeCastState) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x609be96)
#47 0x0000000006074e84 clang::Parser::ParseStaticAssertDeclaration(clang::SourceLocation&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6074e84)
#48 0x00000000060663ff clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x60663ff)
#49 0x000000000602a76e clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x602a76e)
#50 0x000000000602b60d clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x602b60d)
#51 0x000000000601e50a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x601e50a)
#52 0x000000000408f448 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x408f448)
#53 0x00000000042fb0e9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42fb0e9)
#54 0x000000000427acee clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x427acee)
#55 0x00000000043d98ce clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43d98ce)
#56 0x0000000000c0fa06 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc0fa06)
#57 0x0000000000c0727a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#58 0x00000000040d0739 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
#59 0x0000000003752484 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3752484)
#60 0x00000000040d0d2f 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
#61 0x0000000004098ce5 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4098ce5)
#62 0x000000000409974d 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+++0x409974d)
#63 0x00000000040a1645 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x40a1645)
#64 0x0000000000c0cdec clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc0cdec)
#65 0x0000000000b04774 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xb04774)
#66 0x00007f2c0ce29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#67 0x00007f2c0ce29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#68 0x0000000000c06d5e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc06d5e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

shafik avatar Jan 23 '24 05:01 shafik

CC @cor3ntin

shafik avatar Jan 23 '24 05:01 shafik

@erichkeane might have an idea too. The bug is present identically in Clang 17 so my recent deducing this changes are unrelated. We are still comparing function to establish whether they are overloads, even though they do not have the same context at all, which is suspicious. (The partial specialization ought to be unrelated to the primary template afaik)

cor3ntin avatar Jan 23 '24 07:01 cor3ntin

@zyn0217

cor3ntin avatar Aug 09 '24 14:08 cor3ntin