[clang] [clang] Fix the local parameter of void type inside the `Requires` expression. (PR #109831)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 29 22:51:18 PDT 2024


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/109831

>From 9c112e7f88be2fc71f33ce4332d2ea500e479963 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Tue, 24 Sep 2024 16:48:39 +0000
Subject: [PATCH 1/5] require void

---
 clang/lib/Sema/SemaExprCXX.cpp                    | 11 +++++++++++
 .../invalid-requirement-requires-parameter.cpp    | 15 +++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index ac3fe6ab8f9bd0..2dc7f46837f6d1 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -9509,6 +9509,17 @@ Sema::ActOnStartRequiresExpr(SourceLocation RequiresKWLoc,
   PushDeclContext(BodyScope, Body);
 
   for (ParmVarDecl *Param : LocalParameters) {
+    if (Param->getType()->isVoidType()) {
+      if (LocalParameters.size() > 1) {
+        Diag(Param->getBeginLoc(), diag::err_void_only_param);
+      } else if (Param->getIdentifier()) {
+        Diag(Param->getBeginLoc(), diag::err_param_with_void_type);
+      } else if (Param->getType().hasQualifiers()) {
+        Diag(Param->getBeginLoc(), diag::err_void_param_qualified);
+        Body->setInvalidDecl();
+      }
+    }
+
     if (Param->hasDefaultArg())
       // C++2a [expr.prim.req] p4
       //     [...] A local parameter of a requires-expression shall not have a
diff --git a/clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp b/clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp
new file mode 100644
index 00000000000000..01aa0ca4d229b5
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang -fsyntax-only -std=c++2a -Xclang -verify %s
+
+namespace GH109538 {
+static_assert(requires(void *t) { t; });
+static_assert(requires(void) { 42; });
+static_assert(requires(void t) { // expected-error {{argument may not have 'void' type}}
+  t;
+});
+static_assert(requires(void t, int a) {  // expected-error {{'void' must be the first and only parameter if specified}}
+  t;
+});
+static_assert(requires(const void) { // expected-error {{'void' as parameter must not have type qualifiers}}
+  42;
+});
+} // namespace GH109538

>From 8206940c634a3f32d273c75d499fdc4beb683e88 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Tue, 24 Sep 2024 16:52:05 +0000
Subject: [PATCH 2/5] fix

---
 clang/lib/Sema/SemaExprCXX.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 2dc7f46837f6d1..d0914d990be6a7 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -9512,8 +9512,10 @@ Sema::ActOnStartRequiresExpr(SourceLocation RequiresKWLoc,
     if (Param->getType()->isVoidType()) {
       if (LocalParameters.size() > 1) {
         Diag(Param->getBeginLoc(), diag::err_void_only_param);
+        Body->setInvalidDecl();
       } else if (Param->getIdentifier()) {
         Diag(Param->getBeginLoc(), diag::err_param_with_void_type);
+        Body->setInvalidDecl();
       } else if (Param->getType().hasQualifiers()) {
         Diag(Param->getBeginLoc(), diag::err_void_param_qualified);
         Body->setInvalidDecl();

>From 7af1afbb6bd5fd87c47a684555b065bcada4e9a2 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 30 Sep 2024 05:45:53 +0000
Subject: [PATCH 3/5] update test

---
 .../expr.prim/expr.prim.req/requires-expr.cpp    | 16 +++++++++++++++-
 .../invalid-requirement-requires-parameter.cpp   | 15 ---------------
 2 files changed, 15 insertions(+), 16 deletions(-)
 delete mode 100644 clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp

diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.req/requires-expr.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.req/requires-expr.cpp
index 90a38292d15d35..fff5cdc89f3ea1 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.req/requires-expr.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.req/requires-expr.cpp
@@ -65,4 +65,18 @@ template<typename T> requires requires { T::value; S<T>::s; }
 struct r4 { };
 
 using r4i = r4<int>;
-// expected-error at -1 {{constraints not satisfied for class template 'r4' [with T = int]}}
\ No newline at end of file
+// expected-error at -1 {{constraints not satisfied for class template 'r4' [with T = int]}}
+
+namespace GH109538 {
+static_assert(requires(void *t) { t; });
+static_assert(requires(void) { 42; });
+static_assert(!requires(void t) { // expected-error {{argument may not have 'void' type}}
+  t;
+});
+static_assert(!requires(void t, int a) {  // expected-error {{'void' must be the first and only parameter if specified}}
+  t;
+});
+static_assert(!requires(const void) { // expected-error {{'void' as parameter must not have type qualifiers}}
+  42;
+});
+} // namespace GH109538
diff --git a/clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp b/clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp
deleted file mode 100644
index 01aa0ca4d229b5..00000000000000
--- a/clang/test/SemaCXX/invalid-requirement-requires-parameter.cpp
+++ /dev/null
@@ -1,15 +0,0 @@
-// RUN: %clang -fsyntax-only -std=c++2a -Xclang -verify %s
-
-namespace GH109538 {
-static_assert(requires(void *t) { t; });
-static_assert(requires(void) { 42; });
-static_assert(requires(void t) { // expected-error {{argument may not have 'void' type}}
-  t;
-});
-static_assert(requires(void t, int a) {  // expected-error {{'void' must be the first and only parameter if specified}}
-  t;
-});
-static_assert(requires(const void) { // expected-error {{'void' as parameter must not have type qualifiers}}
-  42;
-});
-} // namespace GH109538

>From 590727f89686858921addfa89b527fd3f9d1eb3e Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 30 Sep 2024 05:46:19 +0000
Subject: [PATCH 4/5] static assert

---
 clang/lib/AST/ExprConcepts.cpp | 3 +++
 clang/lib/Sema/SemaExprCXX.cpp | 6 +++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ExprConcepts.cpp b/clang/lib/AST/ExprConcepts.cpp
index 0704630c0fc266..4a77050d996b2e 100644
--- a/clang/lib/AST/ExprConcepts.cpp
+++ b/clang/lib/AST/ExprConcepts.cpp
@@ -153,6 +153,9 @@ RequiresExpr::RequiresExpr(ASTContext &C, SourceLocation RequiresKWLoc,
   std::copy(Requirements.begin(), Requirements.end(),
             getTrailingObjects<concepts::Requirement *>());
   RequiresExprBits.IsSatisfied |= Dependent;
+  RequiresExprBits.IsSatisfied &=
+      llvm::none_of(LocalParameters,
+                    [](ParmVarDecl *Param) { return Param->isInvalidDecl(); });
   // FIXME: move the computing dependency logic to ComputeDependence.h
   if (ContainsUnexpandedParameterPack)
     setDependence(getDependence() | ExprDependence::UnexpandedPack);
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index d0914d990be6a7..4ec84d9c12afce 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -9512,13 +9512,13 @@ Sema::ActOnStartRequiresExpr(SourceLocation RequiresKWLoc,
     if (Param->getType()->isVoidType()) {
       if (LocalParameters.size() > 1) {
         Diag(Param->getBeginLoc(), diag::err_void_only_param);
-        Body->setInvalidDecl();
+        Param->setInvalidDecl();
       } else if (Param->getIdentifier()) {
         Diag(Param->getBeginLoc(), diag::err_param_with_void_type);
-        Body->setInvalidDecl();
+        Param->setInvalidDecl();
       } else if (Param->getType().hasQualifiers()) {
         Diag(Param->getBeginLoc(), diag::err_void_param_qualified);
-        Body->setInvalidDecl();
+        Param->setInvalidDecl();
       }
     }
 

>From a2b0e45326bd29bdb4848956505eb9acd85dbcb3 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 30 Sep 2024 05:51:03 +0000
Subject: [PATCH 5/5] release note

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e511614fcf2451..a503420386a045 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -336,6 +336,8 @@ Improvements to Clang's diagnostics
   local variables passed to function calls using the ``[[clang::musttail]]``
   attribute.
 
+- Clang now diagnoses when a required expression has a local parameter of void type, aligning with the function parameter (#GH109831).
+
 Improvements to Clang's time-trace
 ----------------------------------
 



More information about the cfe-commits mailing list