diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -159,6 +159,8 @@ template instantiation to be constexpr/consteval even though a call to such a function cannot appear in a constant expression. (C++14 [dcl.constexpr]p6 (CWG DR647/CWG DR1358)) +- Correctly defer dependent immediate invocations until template instantiation. + This fixes `Issue 55601 `_. diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -19676,7 +19676,8 @@ if (auto *FD = dyn_cast(E->getDecl())) if (!isUnevaluatedContext() && !isConstantEvaluated() && - FD->isConsteval() && !RebuildingImmediateInvocation) + FD->isConsteval() && !RebuildingImmediateInvocation && + !FD->isDependentContext()) ExprEvalContexts.back().ReferenceToConsteval.insert(E); MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E, OdrUse, RefsMinusAssignments); diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -648,9 +648,40 @@ static_assert(bar<15>() == 15); static_assert(baz() == sizeof(int)); - } // namespace value_dependent +// https://github.com/llvm/llvm-project/issues/55601 +namespace issue_55601 { +template +class Bar { + consteval static T x() { return 5; } // expected-note {{non-constexpr constructor 'derp' cannot be used in a constant expression}} + public: + Bar() : a(x()) {} // expected-error {{call to consteval function 'issue_55601::Bar::x' is not a constant expression}} + // expected-error@-1 {{call to consteval function 'issue_55601::derp::operator int' is not a constant expression}} + // expected-note@-2 {{in call to 'x()'}} + // expected-note@-3 {{non-literal type 'issue_55601::derp' cannot be used in a constant expression}} + private: + int a; +}; +Bar f; +Bar g; + +struct derp { + // Can't be used in a constant expression + derp(int); // expected-note {{declared here}} + consteval operator int() const { return 5; } +}; +Bar a; // expected-note {{in instantiation of member function 'issue_55601::Bar::Bar' requested here}} + +struct constantDerp { + // Can be used in a constant expression. + consteval constantDerp(int) {} + consteval operator int() const { return 5; } +}; +Bar b; + +} // namespace issue_55601 + namespace default_argument { // Previously calls of consteval functions in default arguments were rejected.