[clang] bfdeb58 - [Clang] use constant evaluation context for constexpr if conditions (#123667)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 9 16:53:23 PDT 2025
Author: Oleksandr T.
Date: 2025-03-10T01:53:20+02:00
New Revision: bfdeb5873073ed94b63b1ef1f7e91845c1be88a1
URL: https://github.com/llvm/llvm-project/commit/bfdeb5873073ed94b63b1ef1f7e91845c1be88a1
DIFF: https://github.com/llvm/llvm-project/commit/bfdeb5873073ed94b63b1ef1f7e91845c1be88a1.diff
LOG: [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.
Added:
clang/test/SemaCXX/constexpr-if.cpp
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Parse/ParseExprCXX.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7859871b0493a..372a95c80717c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -294,6 +294,7 @@ Bug Fixes to C++ Support
direct-list-initialized from an array is corrected to direct-initialization.
- Clang no longer crashes when a coroutine is declared ``[[noreturn]]``. (#GH127327)
- Clang now uses the parameter location for abbreviated function templates in ``extern "C"``. (#GH46386)
+- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
Improvements to C++ diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index 33a90e0cb8a42..26be78ee8ca15 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -2203,8 +2203,16 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc,
return ParseCXXCondition(nullptr, Loc, CK, MissingOK);
}
- // Parse the expression.
- ExprResult Expr = ParseExpression(); // expression
+ ExprResult Expr = [&] {
+ EnterExpressionEvaluationContext Eval(
+ Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
+ /*LambdaContextDecl=*/nullptr,
+ /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
+ /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
+ // Parse the expression.
+ return ParseExpression(); // expression
+ }();
+
if (Expr.isInvalid())
return Sema::ConditionError();
diff --git a/clang/test/SemaCXX/constexpr-if.cpp b/clang/test/SemaCXX/constexpr-if.cpp
new file mode 100644
index 0000000000000..494fc45c55c4e
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-if.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++20 -verify=cxx20,expected %s
+// RUN: %clang_cc1 -std=c++23 -verify=cxx23,expected %s
+// RUN: %clang_cc1 -std=c++26 -verify=cxx26,expected %s
+
+// expected-no-diagnostics
+
+namespace GH123524 {
+consteval void fn1() {}
+void fn2() {
+ if constexpr (&fn1 != nullptr) { }
+}
+}
More information about the cfe-commits
mailing list