Fznamznon created this revision. Herald added a project: All. Fznamznon requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
This results in expressions that appear in default function argument not being checked for being actual constant expressions. This aligns clang's behavior with the standard and fixes one of the examples from https://wg21.link/P1073R3. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D145251 Files: clang/lib/Sema/SemaExpr.cpp clang/test/CXX/expr/expr.const/p6-2a.cpp Index: clang/test/CXX/expr/expr.const/p6-2a.cpp =================================================================== --- clang/test/CXX/expr/expr.const/p6-2a.cpp +++ clang/test/CXX/expr/expr.const/p6-2a.cpp @@ -43,14 +43,12 @@ constexpr Temporary t = {3}; // expected-error {{must have constant destruction}} expected-note {{created here}} expected-note {{in call}} namespace P1073R3 { -consteval int f() { return 42; } // expected-note 3 {{declared here}} +consteval int f() { return 42; } // expected-note 2 {{declared here}} consteval auto g() { return f; } -// FIXME: there should be no diagnostics associated with either h() or r. -consteval int h(int (*p)() = g()) { return p(); } // expected-error {{call to consteval function 'P1073R3::g' is not a constant expression}} \ - expected-note {{declared here}} \ - expected-note {{pointer to a consteval declaration is not a constant expression}} -constexpr int r = h(); // expected-note {{in the default initalizer of 'p'}} +consteval int h(int (*p)() = g()) { return p(); } +constexpr int r = h(); constexpr auto e = g(); // expected-error {{call to consteval function 'P1073R3::g' is not a constant expression}} \ expected-error {{constexpr variable 'e' must be initialized by a constant expression}} \ expected-note 2 {{pointer to a consteval declaration is not a constant expression}} +static_assert(r == 42); } // namespace P1073R3 Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -5918,7 +5918,10 @@ "default argument expression has capturing blocks?"); } EnterExpressionEvaluationContext EvalContext( - *this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); + *this, + FD->isConsteval() ? ExpressionEvaluationContext::ImmediateFunctionContext + : ExpressionEvaluationContext::PotentiallyEvaluated, + Param); ExprEvalContexts.back().IsCurrentlyCheckingDefaultArgumentOrInitializer = SkipImmediateInvocations; MarkDeclarationsReferencedInExpr(Init, /*SkipLocalVariables*/ true); @@ -6006,7 +6009,11 @@ // If we are instantiating a template we won't have to // retransform immediate calls. EnterExpressionEvaluationContext EvalContext( - *this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); + *this, + FD->isConsteval() + ? ExpressionEvaluationContext::ImmediateFunctionContext + : ExpressionEvaluationContext::PotentiallyEvaluated, + Param); if (Param->hasUninstantiatedDefaultArg()) { if (InstantiateDefaultArgument(CallLoc, FD, Param))
Index: clang/test/CXX/expr/expr.const/p6-2a.cpp =================================================================== --- clang/test/CXX/expr/expr.const/p6-2a.cpp +++ clang/test/CXX/expr/expr.const/p6-2a.cpp @@ -43,14 +43,12 @@ constexpr Temporary t = {3}; // expected-error {{must have constant destruction}} expected-note {{created here}} expected-note {{in call}} namespace P1073R3 { -consteval int f() { return 42; } // expected-note 3 {{declared here}} +consteval int f() { return 42; } // expected-note 2 {{declared here}} consteval auto g() { return f; } -// FIXME: there should be no diagnostics associated with either h() or r. -consteval int h(int (*p)() = g()) { return p(); } // expected-error {{call to consteval function 'P1073R3::g' is not a constant expression}} \ - expected-note {{declared here}} \ - expected-note {{pointer to a consteval declaration is not a constant expression}} -constexpr int r = h(); // expected-note {{in the default initalizer of 'p'}} +consteval int h(int (*p)() = g()) { return p(); } +constexpr int r = h(); constexpr auto e = g(); // expected-error {{call to consteval function 'P1073R3::g' is not a constant expression}} \ expected-error {{constexpr variable 'e' must be initialized by a constant expression}} \ expected-note 2 {{pointer to a consteval declaration is not a constant expression}} +static_assert(r == 42); } // namespace P1073R3 Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -5918,7 +5918,10 @@ "default argument expression has capturing blocks?"); } EnterExpressionEvaluationContext EvalContext( - *this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); + *this, + FD->isConsteval() ? ExpressionEvaluationContext::ImmediateFunctionContext + : ExpressionEvaluationContext::PotentiallyEvaluated, + Param); ExprEvalContexts.back().IsCurrentlyCheckingDefaultArgumentOrInitializer = SkipImmediateInvocations; MarkDeclarationsReferencedInExpr(Init, /*SkipLocalVariables*/ true); @@ -6006,7 +6009,11 @@ // If we are instantiating a template we won't have to // retransform immediate calls. EnterExpressionEvaluationContext EvalContext( - *this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); + *this, + FD->isConsteval() + ? ExpressionEvaluationContext::ImmediateFunctionContext + : ExpressionEvaluationContext::PotentiallyEvaluated, + Param); if (Param->hasUninstantiatedDefaultArg()) { if (InstantiateDefaultArgument(CallLoc, FD, Param))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits