Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -15377,9 +15377,16 @@ } } +// Check to see if this expression is part of a decltype specifier. +// We're not interested in evaluating this expression, immediately or otherwise. +static bool isInDeclType(Sema &SemaRef) { + return SemaRef.ExprEvalContexts.back().ExprContext == + Sema::ExpressionEvaluationContextRecord::EK_Decltype; +} + ExprResult Sema::CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl) { if (!E.isUsable() || !Decl || !Decl->isConsteval() || isConstantEvaluated() || - RebuildingImmediateInvocation) + isInDeclType(*this) || RebuildingImmediateInvocation) return E; /// Opportunistically remove the callee from ReferencesToConsteval if we can. Index: clang/test/SemaCXX/decltype-consteval.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/decltype-consteval.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s +// expected-no-diagnostics + +struct MaybeConsteval { + MaybeConsteval* ptr = nullptr; + + consteval MaybeConsteval(bool Valid) : ptr(this) { + if (Valid) { + ptr = nullptr; + } + } +}; + +consteval decltype(MaybeConsteval(false)) foo() { + return { true }; +}