]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: elaborated-type-spec in requires-expr [PR101677]
authorJason Merrill <jason@redhat.com>
Mon, 28 Mar 2022 02:31:51 +0000 (22:31 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 5 Apr 2022 16:28:59 +0000 (12:28 -0400)
We were failing to declare class S in the global namespace because we were
treating the requires-expression parameter scope as a normal block scope, so
the implicit declaration went there.

It seems to me that the requires parameter scope is more like a function
parameter scope (not least in the use of the word "parameter"), so let's
change the scope kind.  But then we need to adjust the prohibition on
placeholders declaring implicit template parameters.

PR c++/101677

gcc/cp/ChangeLog:

* name-lookup.h (struct cp_binding_level): Add requires_expression
bit-field.
* parser.cc (cp_parser_requires_expression): Set it.
(synthesize_implicit_template_parm): Check it.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/concepts-pr67178.C: Adjust error.
* g++.dg/cpp2a/concepts-requires28.C: New test.

gcc/cp/name-lookup.h
gcc/cp/parser.cc
gcc/testsuite/g++.dg/cpp2a/concepts-pr67178.C
gcc/testsuite/g++.dg/cpp2a/concepts-requires28.C [new file with mode: 0644]

index 68d08725a001b840cad0b6c411d32d601c484a61..fa039028847aebdb6c5407465b23216c809e5427 100644 (file)
@@ -309,7 +309,10 @@ struct GTY(()) cp_binding_level {
   /* true for SK_FUNCTION_PARMS of immediate functions.  */
   unsigned immediate_fn_ctx_p : 1;
 
-  /* 22 bits left to fill a 32-bit word.  */
+  /* True for SK_FUNCTION_PARMS of a requires-expression.  */
+  unsigned requires_expression: 1;
+
+  /* 21 bits left to fill a 32-bit word.  */
 };
 
 /* The binding level currently in effect.  */
index 7e1c777364e79d3b5fe606ffeb5b0537f1b9acbb..bfd16e1ef620b6c5c5a763cf8e716dd03174cc1f 100644 (file)
@@ -29964,7 +29964,8 @@ cp_parser_requires_expression (cp_parser *parser)
       scope_sentinel ()
       {
        ++cp_unevaluated_operand;
-       begin_scope (sk_block, NULL_TREE);
+       begin_scope (sk_function_parms, NULL_TREE);
+       current_binding_level->requires_expression = true;
       }
 
       ~scope_sentinel ()
@@ -48082,7 +48083,7 @@ static tree
 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
 {
   /* A requires-clause is not a function and cannot have placeholders.  */
-  if (current_binding_level->kind == sk_block)
+  if (current_binding_level->requires_expression)
     {
       error ("placeholder type not allowed in this context");
       return error_mark_node;
index c74f6f00a5c56aa0ba5458a471d03d79e06f9273..bdd5f9699e09f8f341de6066288c8fa3a412d93c 100644 (file)
@@ -12,7 +12,7 @@ concept C0 = requires (auto x) { // { dg-error "placeholder type" }
 template<typename T>
 concept C1 = requires (C1 auto x) { // { dg-error "not been declared|placeholder|two or more|in requirements" }
   x; // { dg-error "not declared" }
-  { x } -> c; // { dg-message "is invalid" }
+  { x } -> c; // { dg-message "is invalid|not declared" }
 };
 
 template<typename T>
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires28.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires28.C
new file mode 100644 (file)
index 0000000..e632f01
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/101677
+// { dg-do compile { target c++20 } }
+
+template<class T>
+concept C_bug_with_forward_decl = requires(T& t){
+    t.template f<class S>();
+};
+
+struct good {
+    template<class T> void f() {}
+};
+
+static_assert(C_bug_with_forward_decl<good>);