]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: diagnose this specifier in requires expr [PR116798]
authorMarek Polacek <polacek@redhat.com>
Mon, 23 Sep 2024 16:19:40 +0000 (12:19 -0400)
committerMarek Polacek <polacek@redhat.com>
Mon, 23 Sep 2024 19:24:07 +0000 (15:24 -0400)
We don't detect an explicit object parameter in a requires expression.
We can get there by way of requires-expression -> requirement-parameter-list
-> parameter-declaration-clause -> ... -> parameter-declaration with
this[opt].  But [dcl.fct]/5 doesn't allow an explicit object parameter
in this context.  So let's fix it like r14-9033 and not like r14-8832.

PR c++/116798

gcc/cp/ChangeLog:

* parser.cc (cp_parser_parameter_declaration): Detect an explicit
object parameter in a requires expression.

gcc/testsuite/ChangeLog:

* g++.dg/cpp23/explicit-obj-diagnostics12.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/parser.cc
gcc/testsuite/g++.dg/cpp23/explicit-obj-diagnostics12.C [new file with mode: 0644]

index 4dd9474cf609e22d076e4f7a2af0a74f10dc5791..dbc607027dfb3324021495d1f3438c59e0dc1ec4 100644 (file)
@@ -25982,10 +25982,15 @@ cp_parser_parameter_declaration (cp_parser *parser,
 
   bool xobj_param_p
     = decl_spec_seq_has_spec_p (&decl_specifiers, ds_this);
-  if (xobj_param_p && template_parm_p)
+  if (xobj_param_p
+      && (template_parm_p || current_binding_level->requires_expression))
     {
-      error_at (decl_specifiers.locations[ds_this],
-               "%<this%> specifier in template parameter declaration");
+      if (template_parm_p)
+       error_at (decl_specifiers.locations[ds_this],
+                 "%<this%> specifier in template parameter declaration");
+      else
+       error_at (decl_specifiers.locations[ds_this],
+                 "%<this%> specifier in a requires-expression parameter");
       xobj_param_p = false;
       decl_specifiers.locations[ds_this] = 0;
     }
diff --git a/gcc/testsuite/g++.dg/cpp23/explicit-obj-diagnostics12.C b/gcc/testsuite/g++.dg/cpp23/explicit-obj-diagnostics12.C
new file mode 100644 (file)
index 0000000..ec0aced
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/116798
+// { dg-do compile { target c++23 } }
+
+template<typename T>
+concept C = requires(this T u,   // { dg-error "'this' specifier in a requires-expression parameter" }
+                    this T v) {  // { dg-error "'this' specifier in a requires-expression parameter" }
+    u + v;
+};
+
+static_assert(C<int>);