https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/91939
We did not set the correct evaluation context for the compound statement of an ``if consteval`` statement in a templated entity in TreeTransform. Fixes #91509 >From e84c8c5e0f1bc9e094dfef961763db825234f7aa Mon Sep 17 00:00:00 2001 From: Corentin Jabot <corentinja...@gmail.com> Date: Mon, 13 May 2024 11:26:40 +0200 Subject: [PATCH] [Clang] Ensure ``if consteval`` consititute an immediate function context. We did not set the correct evaluation context for the compound statement of an ``if consteval`` statement in a templated entity in TreeTransform. Fixes 91509 --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Sema/TreeTransform.h | 18 +++++++++++++ .../SemaCXX/cxx2b-consteval-propagate.cpp | 26 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 7c5dcc59c7016..4702b8c10cdbb 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -707,6 +707,7 @@ Bug Fixes to C++ Support initialized, rather than evaluating them as a part of the larger manifestly constant evaluated expression. - Fix a bug in access control checking due to dealyed checking of friend declaration. Fixes (#GH12361). +- Correctly treat the compound statement of an ``if consteval`` as an immediate context. Fixes (#GH91509). Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 0b3cf566e3a7b..391cfb0fa4032 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -7964,6 +7964,15 @@ TreeTransform<Derived>::TransformIfStmt(IfStmt *S) { // Transform the "then" branch. StmtResult Then; if (!ConstexprConditionValue || *ConstexprConditionValue) { + Sema::ExpressionEvaluationContext Context = + S->isNonNegatedConsteval() + ? Sema::ExpressionEvaluationContext::ImmediateFunctionContext + : Sema::ExpressionEvaluationContext::PotentiallyEvaluated; + + EnterExpressionEvaluationContext Ctx( + getSema(), Context, nullptr, + Sema::ExpressionEvaluationContextRecord::EK_Other); + Then = getDerived().TransformStmt(S->getThen()); if (Then.isInvalid()) return StmtError(); @@ -7978,6 +7987,15 @@ TreeTransform<Derived>::TransformIfStmt(IfStmt *S) { // Transform the "else" branch. StmtResult Else; if (!ConstexprConditionValue || !*ConstexprConditionValue) { + Sema::ExpressionEvaluationContext Context = + S->isNegatedConsteval() + ? Sema::ExpressionEvaluationContext::ImmediateFunctionContext + : Sema::ExpressionEvaluationContext::PotentiallyEvaluated; + + EnterExpressionEvaluationContext Ctx( + getSema(), Context, nullptr, + Sema::ExpressionEvaluationContextRecord::EK_Other); + Else = getDerived().TransformStmt(S->getElse()); if (Else.isInvalid()) return StmtError(); diff --git a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp index 37fa1f1bdf59d..07937deb66738 100644 --- a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp +++ b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp @@ -420,3 +420,29 @@ int f = *fn().value + fn2(); // expected-error {{call to consteval function 'lv // expected-note {{pointer to heap-allocated object}} } #endif + + +#if __cplusplus >= 202302L + +namespace GH91509 { + +consteval int f(int) { return 0; } + +template<typename T> +constexpr int g(int x) { + if consteval { + return f(x); + } + if !consteval {} + else { + return f(x); + } + return 1; +} + +int h(int x) { + return g<void>(x); +} +} + +#endif _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits