[clang] [Clang] use constant evaluation context for constexpr if conditions (PR #123667)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 9 07:06:21 PDT 2025
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/123667
>From 00b8b64879ad3ae35a0a671da563ac876ffaf228 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 20 Jan 2025 22:51:00 +0200
Subject: [PATCH 1/6] [Clang] use constant evaluation context for constexpr if
conditions
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/lib/Parse/ParseExprCXX.cpp | 6 ++++++
clang/test/SemaCXX/constexpr-if.cpp | 10 ++++++++++
3 files changed, 17 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/constexpr-if.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f6d5c346021d6..cd16ce13a4e6b 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 33a90e0cb8a42..e174d9a24e744 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 0000000000000..1832086fee42d
--- /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) { }
+}
+}
>From df6fd0c3f762d5fb76bdf98c6425a79ef01f4d7e Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 21 Jan 2025 10:27:57 +0200
Subject: [PATCH 2/6] update tests to verify changes from c++20
---
clang/test/SemaCXX/constexpr-if.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/test/SemaCXX/constexpr-if.cpp b/clang/test/SemaCXX/constexpr-if.cpp
index 1832086fee42d..494fc45c55c4e 100644
--- a/clang/test/SemaCXX/constexpr-if.cpp
+++ b/clang/test/SemaCXX/constexpr-if.cpp
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -std=c++26 -verify %s
+// 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
>From bba89e7dc79d4d78a794ccf0e7e5196a22b72820 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Thu, 20 Feb 2025 00:35:54 +0200
Subject: [PATCH 3/6] ensure context is applied only during expression parsing
---
clang/lib/Parse/ParseExprCXX.cpp | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index e174d9a24e744..5d5e015d548f5 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -2203,14 +2203,18 @@ 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
+ ExprResult Expr;
+ {
+ EnterExpressionEvaluationContext Eval(
+ Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
+ /*LambdaContextDecl=*/nullptr,
+ /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
+ /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
+
+ // Parse the expression.
+ Expr = ParseExpression(); // expression
+ }
+
if (Expr.isInvalid())
return Sema::ConditionError();
>From 666d5bf9574fb583f9da27331545c861eed66488 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Thu, 20 Feb 2025 00:36:11 +0200
Subject: [PATCH 4/6] update release notes
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6f54341a1f45b..df67e7351d4f8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -966,7 +966,6 @@ Bug Fixes to C++ Support
constraints are applied. (#GH122134)
- Fixed canonicalization of pack indexing types - Clang did not always recognized identical pack indexing. (#GH123033)
- Fixed a nested lambda substitution issue for constraint evaluation. (#GH123441)
-- Clang now permits the use of immediate-escalating expressions in ``constexpr`` if conditions. (#GH123524)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -979,6 +978,7 @@ Bug Fixes to AST Handling
- Clang now uses the location of the begin of the member expression for ``CallExpr``
involving deduced ``this``. (#GH116928)
- Fixed printout of AST that uses pack indexing expression. (#GH116486)
+- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
>From 5104a333ea9dfe84670b930d06b10907bd41832a Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Thu, 27 Feb 2025 14:41:38 +0200
Subject: [PATCH 5/6] use immediately-invoked lambda expression
---
clang/lib/Parse/ParseExprCXX.cpp | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index 5d5e015d548f5..26be78ee8ca15 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -2203,17 +2203,15 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc,
return ParseCXXCondition(nullptr, Loc, CK, MissingOK);
}
- ExprResult Expr;
- {
+ ExprResult Expr = [&] {
EnterExpressionEvaluationContext Eval(
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
/*LambdaContextDecl=*/nullptr,
/*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
/*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
-
// Parse the expression.
- Expr = ParseExpression(); // expression
- }
+ return ParseExpression(); // expression
+ }();
if (Expr.isInvalid())
return Sema::ConditionError();
>From 1397ae40153a617198058df67678f17b2e2c9a1b Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Sun, 9 Mar 2025 16:05:04 +0200
Subject: [PATCH 6/6] move release notes to Bug Fixes to C++ Support section
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea5d30e80a395..8a222ba563a1d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -235,11 +235,11 @@ Bug Fixes to C++ Support
- The initialization kind of elements of structured bindings
direct-list-initialized from an array is corrected to direct-initialization.
- Clang no longer crashes when a coroutine is declared ``[[noreturn]]``. (#GH127327)
+- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed type checking when a statement expression ends in an l-value of atomic type. (#GH106576)
-- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list