]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: undeclared identifier in requires-clause [PR99678]
authorPatrick Palka <ppalka@redhat.com>
Thu, 13 Jun 2024 14:16:10 +0000 (10:16 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 13 Jun 2024 14:16:10 +0000 (10:16 -0400)
Since the terms of a requires-clause are grammatically primary-expressions
and not e.g. postfix-expressions, it seems we need to explicitly handle
and diagnose the case where a term parses to a bare unresolved identifier,
like cp_parser_postfix_expression does, since cp_parser_primary_expression
leaves that up to its callers.  Otherwise we incorrectly accept the first
three requires-clauses below.

Note that the only occurrences of primary-expression in the grammar are
postfix-expression and constraint-logical-and-expression, so it's not too
surprising that we need this special handling here.

PR c++/99678

gcc/cp/ChangeLog:

* parser.cc (cp_parser_constraint_primary_expression): Diagnose
a bare unresolved unqualified-id.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/concepts-requires38.C: New test.

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

index 01a19080d6c7f22bfe63b04a7347860f05c43e8a..e7409b856f1127e303c6515a3bb2d61a10e7c378 100644 (file)
@@ -31525,6 +31525,8 @@ cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
     }
   if (pce == pce_ok)
     {
+      if (idk == CP_ID_KIND_UNQUALIFIED && identifier_p (expr))
+       expr = unqualified_name_lookup_error (expr);
       cp_lexer_commit_tokens (parser->lexer);
       return finish_constraint_primary_expr (expr);
     }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C
new file mode 100644 (file)
index 0000000..663195b
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/99678
+// { dg-do compile { target c++20 } }
+
+template<class T>
+void f1() requires undeclared_identifier; // { dg-error "not declared" }
+
+template<class T>
+void f2() requires true && undeclared_identifier; // { dg-error "not declared" }
+
+template<class T>
+void f3() requires false || undeclared_identifier; // { dg-error "not declared" }
+
+template<class T>
+void f4() requires undeclared_identifier(T{}); // { dg-error "must be enclosed in parentheses" }