Skip to content

Commit bfdeb58

Browse files
authored
[Clang] use constant evaluation context for constexpr if conditions (#123667)
Fixes #123524 --- This PR addresses the issue of immediate function expressions not properly evaluated in `constexpr` if conditions. Adding the `ConstantEvaluated` context for expressions in `constexpr` if statements ensures that these expressions are treated as manifestly constant-evaluated and parsed correctly.
1 parent 5c3b059 commit bfdeb58

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Bug Fixes to C++ Support
294294
direct-list-initialized from an array is corrected to direct-initialization.
295295
- Clang no longer crashes when a coroutine is declared ``[[noreturn]]``. (#GH127327)
296296
- Clang now uses the parameter location for abbreviated function templates in ``extern "C"``. (#GH46386)
297+
- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
297298

298299
Improvements to C++ diagnostics
299300
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,8 +2203,16 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc,
22032203
return ParseCXXCondition(nullptr, Loc, CK, MissingOK);
22042204
}
22052205

2206-
// Parse the expression.
2207-
ExprResult Expr = ParseExpression(); // expression
2206+
ExprResult Expr = [&] {
2207+
EnterExpressionEvaluationContext Eval(
2208+
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
2209+
/*LambdaContextDecl=*/nullptr,
2210+
/*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
2211+
/*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
2212+
// Parse the expression.
2213+
return ParseExpression(); // expression
2214+
}();
2215+
22082216
if (Expr.isInvalid())
22092217
return Sema::ConditionError();
22102218

clang/test/SemaCXX/constexpr-if.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -std=c++20 -verify=cxx20,expected %s
2+
// RUN: %clang_cc1 -std=c++23 -verify=cxx23,expected %s
3+
// RUN: %clang_cc1 -std=c++26 -verify=cxx26,expected %s
4+
5+
// expected-no-diagnostics
6+
7+
namespace GH123524 {
8+
consteval void fn1() {}
9+
void fn2() {
10+
if constexpr (&fn1 != nullptr) { }
11+
}
12+
}

0 commit comments

Comments
 (0)