Skip to content

Commit c4e9e41

Browse files
authored
[Clang] Ensure if consteval consititute an immediate function context (llvm#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 llvm#91509
1 parent 4445ed4 commit c4e9e41

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ Bug Fixes to C++ Support
707707
initialized, rather than evaluating them as a part of the larger manifestly constant evaluated
708708
expression.
709709
- Fix a bug in access control checking due to dealyed checking of friend declaration. Fixes (#GH12361).
710+
- Correctly treat the compound statement of an ``if consteval`` as an immediate context. Fixes (#GH91509).
710711

711712
Bug Fixes to AST Handling
712713
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/TreeTransform.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7964,6 +7964,11 @@ TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
79647964
// Transform the "then" branch.
79657965
StmtResult Then;
79667966
if (!ConstexprConditionValue || *ConstexprConditionValue) {
7967+
EnterExpressionEvaluationContext Ctx(
7968+
getSema(), Sema::ExpressionEvaluationContext::ImmediateFunctionContext,
7969+
nullptr, Sema::ExpressionEvaluationContextRecord::EK_Other,
7970+
S->isNonNegatedConsteval());
7971+
79677972
Then = getDerived().TransformStmt(S->getThen());
79687973
if (Then.isInvalid())
79697974
return StmtError();
@@ -7978,6 +7983,11 @@ TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
79787983
// Transform the "else" branch.
79797984
StmtResult Else;
79807985
if (!ConstexprConditionValue || !*ConstexprConditionValue) {
7986+
EnterExpressionEvaluationContext Ctx(
7987+
getSema(), Sema::ExpressionEvaluationContext::ImmediateFunctionContext,
7988+
nullptr, Sema::ExpressionEvaluationContextRecord::EK_Other,
7989+
S->isNegatedConsteval());
7990+
79817991
Else = getDerived().TransformStmt(S->getElse());
79827992
if (Else.isInvalid())
79837993
return StmtError();

clang/test/SemaCXX/cxx2b-consteval-propagate.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,29 @@ int f = *fn().value + fn2(); // expected-error {{call to consteval function 'lv
420420
// expected-note {{pointer to heap-allocated object}}
421421
}
422422
#endif
423+
424+
425+
#if __cplusplus >= 202302L
426+
427+
namespace GH91509 {
428+
429+
consteval int f(int) { return 0; }
430+
431+
template<typename T>
432+
constexpr int g(int x) {
433+
if consteval {
434+
return f(x);
435+
}
436+
if !consteval {}
437+
else {
438+
return f(x);
439+
}
440+
return 1;
441+
}
442+
443+
int h(int x) {
444+
return g<void>(x);
445+
}
446+
}
447+
448+
#endif

0 commit comments

Comments
 (0)