Skip to content

Conversation

@a-tarasyuk
Copy link
Member

@a-tarasyuk a-tarasyuk commented Jan 20, 2025

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 20, 2025

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #123524


Full diff: https://github.com/llvm/llvm-project/pull/123667.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1-1)
  • (modified) clang/lib/Parse/ParseExprCXX.cpp (+6)
  • (added) clang/test/SemaCXX/constexpr-if.cpp (+10)
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 shafik self-requested a review January 23, 2025 06:28
Copy link
Collaborator

@shafik shafik left a 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.

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now lacks my concerns, as this is only applying to the singular expression. But parsing isn't my expertise, so @cor3ntin or @Endilll should probably think about this for a minute or two.

@a-tarasyuk a-tarasyuk requested a review from zyn0217 February 27, 2025 12:43
@a-tarasyuk a-tarasyuk requested a review from zyn0217 March 9, 2025 14:06
@a-tarasyuk a-tarasyuk merged commit bfdeb58 into llvm:main Mar 9, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang++] if-contexpr expression aren't parsed in an immediate function context

7 participants