[clang] [Clang] Implement CWG2517 Useless restriction on use of parameter in constraint-expression (PR #132919)
Imad Aldij via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 25 10:07:21 PDT 2025
https://github.com/imdj updated https://github.com/llvm/llvm-project/pull/132919
>From 7905577616743f5158560a4b337148ef9cd25f1e Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Tue, 25 Mar 2025 14:50:55 +0200
Subject: [PATCH 1/7] [Clang] Implement CWG2517 Useless restriction on use of
parameter in constraint-expression
---
clang/lib/Sema/SemaExpr.cpp | 11 -----------
1 file changed, 11 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 3af6d6c23438f..fa8eeb644c179 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -396,17 +396,6 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
targetDiag(*Locs.begin(), diag::err_thread_unsupported);
}
- if (isa<ParmVarDecl>(D) && isa<RequiresExprBodyDecl>(D->getDeclContext()) &&
- !isUnevaluatedContext()) {
- // C++ [expr.prim.req.nested] p3
- // A local parameter shall only appear as an unevaluated operand
- // (Clause 8) within the constraint-expression.
- Diag(Loc, diag::err_requires_expr_parameter_referenced_in_evaluated_context)
- << D;
- Diag(D->getLocation(), diag::note_entity_declared_at) << D;
- return true;
- }
-
return false;
}
>From a3b573c1d1f7120c9a1caa7e6fad42a5309679f0 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Tue, 25 Mar 2025 15:04:15 +0200
Subject: [PATCH 2/7] Remove old tests
---
.../expr/expr.prim/expr.prim.req/nested-requirement.cpp | 7 -------
.../expr/expr.prim/expr.prim.req/simple-requirement.cpp | 7 -------
2 files changed, 14 deletions(-)
diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp
index 763d983d20f61..eaef4441c10f8 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp
@@ -37,13 +37,6 @@ namespace std_example {
static_assert(D<T1>);
template<D T> struct D_check {}; // expected-note{{because 'short' does not satisfy 'D'}}
using dc1 = D_check<short>; // expected-error{{constraints not satisfied for class template 'D_check' [with T = short]}}
-
- template<typename T>
- concept C2 = requires (T a) {
- requires sizeof(a) == 4; // OK
- requires a == 0; // expected-note{{because 'a == 0' would be invalid: constraint variable 'a' cannot be used in an evaluated context}}
- };
- static_assert(C2<int>); // expected-note{{because 'int' does not satisfy 'C2'}} expected-error{{static assertion failed}}
}
template<typename T>
diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp
index 7515f5c62d5ea..0b6d9e147b3d1 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.req/simple-requirement.cpp
@@ -98,13 +98,6 @@ namespace std_example {
using c1c2 = C_check<int *>; // expected-error{{constraints not satisfied for class template 'C_check' [with T = int *]}}
}
-// typeid() of an expression becomes potentially evaluated if the expression is
-// of a polymorphic type.
-class X { virtual ~X(); };
-constexpr bool b = requires (X &x) { static_cast<int(*)[(typeid(x), 0)]>(nullptr); };
-// expected-error at -1{{constraint variable 'x' cannot be used in an evaluated context}}
-// expected-note at -2{{'x' declared here}}
-
namespace access_checks {
namespace in_requires_expression {
template<auto>
>From bbd033d24b3a625d448fb9e7267af54a202e41d7 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Tue, 25 Mar 2025 15:05:17 +0200
Subject: [PATCH 3/7] Add test associated with CWG2517
---
clang/test/CXX/drs/cwg25xx.cpp | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/clang/test/CXX/drs/cwg25xx.cpp b/clang/test/CXX/drs/cwg25xx.cpp
index d9a7d2bbb2671..bda2cc6e5e965 100644
--- a/clang/test/CXX/drs/cwg25xx.cpp
+++ b/clang/test/CXX/drs/cwg25xx.cpp
@@ -32,6 +32,26 @@ enum E2 : S<E2>::I { e };
#endif
} // namespace cwg2516
+namespace cwg2517 { // cwg2517: 23
+#if __cplusplus >= 202302L
+template<typename ArrayType>
+concept LargeArray = requires (ArrayType my_array) {
+ requires my_array.size() > 5;
+};
+
+struct Big {
+ constexpr int size() const { return 100; }
+};
+
+struct Small {
+ constexpr int size() const { return 3; }
+};
+
+static_assert(LargeArray<Big>);
+static_assert(!LargeArray<Small>);
+#endif
+} // namespace cwg2517
+
namespace cwg2518 { // cwg2518: 17
#if __cplusplus >= 201103L
>From 159505e515da00e6006cba1a68766a5522981d8b Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Tue, 25 Mar 2025 15:07:46 +0200
Subject: [PATCH 4/7] Update cxx_dr_status.html
---
clang/www/cxx_dr_status.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index 16a9b26052f87..21fd1c8c965b7 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -14937,7 +14937,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/2517.html">2517</a></td>
<td>C++23</td>
<td>Useless restriction on use of parameter in <I>constraint-expression</I></td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="full" align="center">Clang 23</td>
</tr>
<tr id="2518">
<td><a href="https://cplusplus.github.io/CWG/issues/2518.html">2518</a></td>
>From f5681cdae7efa3b80860b7e142906e0fe7f3c7ba Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Tue, 25 Mar 2025 15:36:33 +0200
Subject: [PATCH 5/7] Add release notes
---
clang/docs/ReleaseNotes.rst | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2f15c90ab1583..243bebba397cc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -116,6 +116,9 @@ Resolutions to C++ Defect Reports
- Bumped the ``__cpp_constexpr`` feature-test macro to ``202002L`` in C++20 mode as indicated in
`P2493R0 <https://wg21.link/P2493R0>`_.
+- Implemented `CWG2517 Useless restriction on use of parameter in `
+ `constraint-expression <https://cplusplus.github.io/CWG/issues/2517.html>`_.
+
C Language Changes
------------------
>From 5a939a79c7cbdbfc0d8d41e8b8ea45fc41441b62 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Tue, 25 Mar 2025 16:01:17 +0200
Subject: [PATCH 6/7] Fix clang version
---
clang/test/CXX/drs/cwg25xx.cpp | 2 +-
clang/www/cxx_dr_status.html | 44 ++++++++++++++++++++++++++++++----
2 files changed, 41 insertions(+), 5 deletions(-)
diff --git a/clang/test/CXX/drs/cwg25xx.cpp b/clang/test/CXX/drs/cwg25xx.cpp
index bda2cc6e5e965..e79b62772f08b 100644
--- a/clang/test/CXX/drs/cwg25xx.cpp
+++ b/clang/test/CXX/drs/cwg25xx.cpp
@@ -32,7 +32,7 @@ enum E2 : S<E2>::I { e };
#endif
} // namespace cwg2516
-namespace cwg2517 { // cwg2517: 23
+namespace cwg2517 { // cwg2517: 21
#if __cplusplus >= 202302L
template<typename ArrayType>
concept LargeArray = requires (ArrayType my_array) {
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index 21fd1c8c965b7..cbc39e670cc50 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -12529,11 +12529,11 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>Direct or copy initialization for omitted aggregate initializers</td>
<td class="unknown" align="center">Unknown</td>
</tr>
- <tr class="open" id="2117">
+ <tr id="2117">
<td><a href="https://cplusplus.github.io/CWG/issues/2117.html">2117</a></td>
- <td>open</td>
+ <td>NAD</td>
<td>Explicit specializations and <TT>constexpr</TT> function templates</td>
- <td align="center">Not resolved</td>
+ <td class="unknown" align="center">Unknown</td>
</tr>
<tr class="open" id="2118">
<td><a href="https://cplusplus.github.io/CWG/issues/2118.html">2118</a></td>
@@ -14937,7 +14937,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/2517.html">2517</a></td>
<td>C++23</td>
<td>Useless restriction on use of parameter in <I>constraint-expression</I></td>
- <td class="full" align="center">Clang 23</td>
+ <td class="unreleased" align="center">Clang 21</td>
</tr>
<tr id="2518">
<td><a href="https://cplusplus.github.io/CWG/issues/2518.html">2518</a></td>
@@ -17909,6 +17909,42 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td>open</td>
<td>Missing Annex C entry for <TT>void</TT> object declarations</td>
<td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="3009">
+ <td><a href="https://cplusplus.github.io/CWG/issues/3009.html">3009</a></td>
+ <td>open</td>
+ <td>Unclear rules for constant initialization</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="3010">
+ <td><a href="https://cplusplus.github.io/CWG/issues/3010.html">3010</a></td>
+ <td>open</td>
+ <td>constexpr placement-new should require transparent replaceability</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="3011">
+ <td><a href="https://cplusplus.github.io/CWG/issues/3011.html">3011</a></td>
+ <td>open</td>
+ <td>Parenthesized aggregate initialization for <I>new-expression</I>s</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="3012">
+ <td><a href="https://cplusplus.github.io/CWG/issues/3012.html">3012</a></td>
+ <td>open</td>
+ <td>Deviating <TT>constexpr</TT> or <TT>consteval</TT> across translation units</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="3013">
+ <td><a href="https://cplusplus.github.io/CWG/issues/3013.html">3013</a></td>
+ <td>open</td>
+ <td>Disallowing macros for <TT>#embed</TT> parameters</td>
+ <td align="center">Not resolved</td>
+ </tr>
+ <tr class="open" id="3014">
+ <td><a href="https://cplusplus.github.io/CWG/issues/3014.html">3014</a></td>
+ <td>open</td>
+ <td>Comma-delimited vs. comma-separated output for <TT>#embed</TT></td>
+ <td align="center">Not resolved</td>
</tr></table>
</div>
>From e108f4c7309bfd003fa002cc3905c89c6b2ff1cf Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Tue, 25 Mar 2025 19:04:46 +0200
Subject: [PATCH 7/7] Remove
err_requires_expr_parameter_referenced_in_evaluated_context
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 --
1 file changed, 2 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index c77cde297dc32..b05c06a3ddbe7 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3038,8 +3038,6 @@ def err_reference_to_function_with_unsatisfied_constraints : Error<
"invalid reference to function %0: constraints not satisfied">;
def err_requires_expr_local_parameter_default_argument : Error<
"default arguments not allowed for parameters of a requires expression">;
-def err_requires_expr_parameter_referenced_in_evaluated_context : Error<
- "constraint variable %0 cannot be used in an evaluated context">;
def note_expr_requirement_expr_substitution_error : Note<
"%select{and|because}0 '%1' would be invalid: %2">;
def note_expr_requirement_expr_unknown_substitution_error : Note<
More information about the cfe-commits
mailing list