public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: undeclared identifier in requires-clause [PR99678]
@ 2024-06-12 18:22 Patrick Palka
  2024-06-13  3:17 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2024-06-12 18:22 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
for trunk/14?

-- >8 --

Since the terms of a requires-clause are grammatically primary-expressions
rather than e.g. postfix-expressions, it seems we need to explicitly
handle and diagnose the case where a term parses to bare unresolved
unqualified-id, like cp_parser_postfix_expression does, since
cp_parser_primary_expression doesn't do so itself.  Otherwise we fail to
reject the first three requires-clauses those below.

Note that the only users of primary-expression in the C++ grammar are
indeed postfix-expression and constraint-logical-and-expression, so it
seems not so surprising that we need this special handling here.

	PR c++/99678

gcc/cp/ChangeLog:

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

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-requires38.C: New test.
---
 gcc/cp/parser.cc                                 |  2 ++
 gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index bc4a2359153..0d59f7d2690 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -31496,6 +31496,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
index 00000000000..663195b79cc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C
@@ -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" }
-- 
2.45.2.457.g8d94cfb545


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: undeclared identifier in requires-clause [PR99678]
  2024-06-12 18:22 [PATCH] c++: undeclared identifier in requires-clause [PR99678] Patrick Palka
@ 2024-06-13  3:17 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-06-13  3:17 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

On 6/12/24 14:22, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk/14?

OK.

> -- >8 --
> 
> Since the terms of a requires-clause are grammatically primary-expressions
> rather than e.g. postfix-expressions, it seems we need to explicitly
> handle and diagnose the case where a term parses to bare unresolved
> unqualified-id, like cp_parser_postfix_expression does, since
> cp_parser_primary_expression doesn't do so itself.  Otherwise we fail to
> reject the first three requires-clauses those below.
> 
> Note that the only users of primary-expression in the C++ grammar are
> indeed postfix-expression and constraint-logical-and-expression, so it
> seems not so surprising that we need this special handling here.
> 
> 	PR c++/99678
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_parser_constraint_primary_expression): Reject
> 	a bare unresolved unqualified-id.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-requires38.C: New test.
> ---
>   gcc/cp/parser.cc                                 |  2 ++
>   gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C | 14 ++++++++++++++
>   2 files changed, 16 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index bc4a2359153..0d59f7d2690 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -31496,6 +31496,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
> index 00000000000..663195b79cc
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-requires38.C
> @@ -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" }


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-13  3:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-12 18:22 [PATCH] c++: undeclared identifier in requires-clause [PR99678] Patrick Palka
2024-06-13  3:17 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).