-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] use constant evaluation context for constexpr if conditions #123667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) ChangesFixes #123524 Full diff: https://github.com/llvm/llvm-project/pull/123667.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f6d5c346021d60..cd16ce13a4e6b6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -963,7 +963,7 @@ Bug Fixes to C++ Support
- Fixed a crash caused by the incorrect construction of template arguments for CTAD alias guides when type
constraints are applied. (#GH122134)
- Fixed canonicalization of pack indexing types - Clang did not always recognized identical pack indexing. (#GH123033)
-
+- Clang now permits the use of immediate-escalating expressions in ``constexpr`` if conditions. (#GH123524)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index 33a90e0cb8a42a..e174d9a24e7440 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -2203,6 +2203,12 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc,
return ParseCXXCondition(nullptr, Loc, CK, MissingOK);
}
+ EnterExpressionEvaluationContext Eval(
+ Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
+ /*LambdaContextDecl=*/nullptr,
+ /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
+ /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
+
// Parse the expression.
ExprResult Expr = ParseExpression(); // expression
if (Expr.isInvalid())
diff --git a/clang/test/SemaCXX/constexpr-if.cpp b/clang/test/SemaCXX/constexpr-if.cpp
new file mode 100644
index 00000000000000..1832086fee42d4
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-if.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -std=c++26 -verify %s
+
+// expected-no-diagnostics
+
+namespace GH123524 {
+consteval void fn1() {}
+void fn2() {
+ if constexpr (&fn1 != nullptr) { }
+}
+}
|
shafik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a more detailed summary to this PR. I have asked this on several PRs and I believe the confusion reflected in the comments is very much attributed to not explaining why you believe your fix works. It may be incorrect but a solid summary will allow the reviewers to quickly understand the assumptions you are making in your fix and should reduce back and forth and allow for more targeted feedback quicker.
erichkeane
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes #123524
This PR addresses the issue of immediate function expressions not properly evaluated in
constexprif conditions. Adding theConstantEvaluatedcontext for expressions inconstexprif statements ensures that these expressions are treated as manifestly constant-evaluated and parsed correctly.