public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Report invalid id-expression in decltype [PR100482]
@ 2023-04-30  2:00 Nathaniel Shead
  2023-05-27  7:11 ` ping: " Nathaniel Shead
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Nathaniel Shead @ 2023-04-30  2:00 UTC (permalink / raw)
  To: gcc-patches; +Cc: Nathaniel Shead

This patch ensures that any errors raised by finish_id_expression when
parsing a decltype expression are properly reported, rather than
potentially going ignored and causing invalid code to be accepted.

We can also now remove the separate check for templates without args as
this is also checked for in finish_id_expression.

	PR 100482

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_decltype_expr): Report errors raised by
	finish_id_expression.

gcc/testsuite/ChangeLog:

	* g++.dg/pr100482.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/parser.cc                | 22 +++++++++++-----------
 gcc/testsuite/g++.dg/pr100482.C | 11 +++++++++++
 2 files changed, 22 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr100482.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index e5f032f2330..20ebcdc3cfd 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
 	expr = cp_parser_lookup_name_simple (parser, expr,
 					     id_expr_start_token->location);
 
-      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
-	/* A template without args is not a complete id-expression.  */
-	expr = error_mark_node;
-
       if (expr
           && expr != error_mark_node
           && TREE_CODE (expr) != TYPE_DECL
@@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
                    &error_msg,
 		   id_expr_start_token->location));
 
-          if (expr == error_mark_node)
-            /* We found an id-expression, but it was something that we
-               should not have found. This is an error, not something
-               we can recover from, so note that we found an
-               id-expression and we'll recover as gracefully as
-               possible.  */
-            id_expression_or_member_access_p = true;
+	  if (error_msg)
+	    {
+	      /* We found an id-expression, but it was something that we
+		 should not have found. This is an error, not something
+		 we can recover from, so report the error we found and
+		 we'll recover as gracefully as possible.  */
+	      cp_parser_parse_definitely (parser);
+	      cp_parser_error (parser, error_msg);
+	      id_expression_or_member_access_p = true;
+	      return error_mark_node;
+	    }
         }
 
       if (expr
diff --git a/gcc/testsuite/g++.dg/pr100482.C b/gcc/testsuite/g++.dg/pr100482.C
new file mode 100644
index 00000000000..dcf6722fda5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr100482.C
@@ -0,0 +1,11 @@
+// { dg-do compile { target c++11 } }
+
+namespace N {}
+decltype(std) x;   // { dg-error "expected primary-expression" }
+
+struct S {};
+decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
+
+template <typename T>
+struct U {};
+decltype(U) z;  // { dg-error "missing template arguments" }
-- 
2.40.0


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

* ping: [PATCH] c++: Report invalid id-expression in decltype [PR100482]
  2023-04-30  2:00 [PATCH] c++: Report invalid id-expression in decltype [PR100482] Nathaniel Shead
@ 2023-05-27  7:11 ` Nathaniel Shead
  2023-06-13 10:13 ` Nathaniel Shead
  2023-06-23 16:15 ` Patrick Palka
  2 siblings, 0 replies; 8+ messages in thread
From: Nathaniel Shead @ 2023-05-27  7:11 UTC (permalink / raw)
  To: gcc-patches

Ping: https://gcc.gnu.org/pipermail/gcc-patches/2023-April/617145.html

Also it looks like I forgot to mention, but this patch was bootstrapped
and regtested on x86_64-pc-linux-gnu.

On Sun, Apr 30, 2023 at 12:00:05PM +1000, Nathaniel Shead wrote:
> This patch ensures that any errors raised by finish_id_expression when
> parsing a decltype expression are properly reported, rather than
> potentially going ignored and causing invalid code to be accepted.
> 
> We can also now remove the separate check for templates without args as
> this is also checked for in finish_id_expression.
> 
> 	PR 100482
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_parser_decltype_expr): Report errors raised by
> 	finish_id_expression.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/pr100482.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>  gcc/cp/parser.cc                | 22 +++++++++++-----------
>  gcc/testsuite/g++.dg/pr100482.C | 11 +++++++++++
>  2 files changed, 22 insertions(+), 11 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/pr100482.C
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index e5f032f2330..20ebcdc3cfd 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
>  	expr = cp_parser_lookup_name_simple (parser, expr,
>  					     id_expr_start_token->location);
>  
> -      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
> -	/* A template without args is not a complete id-expression.  */
> -	expr = error_mark_node;
> -
>        if (expr
>            && expr != error_mark_node
>            && TREE_CODE (expr) != TYPE_DECL
> @@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
>                     &error_msg,
>  		   id_expr_start_token->location));
>  
> -          if (expr == error_mark_node)
> -            /* We found an id-expression, but it was something that we
> -               should not have found. This is an error, not something
> -               we can recover from, so note that we found an
> -               id-expression and we'll recover as gracefully as
> -               possible.  */
> -            id_expression_or_member_access_p = true;
> +	  if (error_msg)
> +	    {
> +	      /* We found an id-expression, but it was something that we
> +		 should not have found. This is an error, not something
> +		 we can recover from, so report the error we found and
> +		 we'll recover as gracefully as possible.  */
> +	      cp_parser_parse_definitely (parser);
> +	      cp_parser_error (parser, error_msg);
> +	      id_expression_or_member_access_p = true;
> +	      return error_mark_node;
> +	    }
>          }
>  
>        if (expr
> diff --git a/gcc/testsuite/g++.dg/pr100482.C b/gcc/testsuite/g++.dg/pr100482.C
> new file mode 100644
> index 00000000000..dcf6722fda5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/pr100482.C
> @@ -0,0 +1,11 @@
> +// { dg-do compile { target c++11 } }
> +
> +namespace N {}
> +decltype(std) x;   // { dg-error "expected primary-expression" }
> +
> +struct S {};
> +decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
> +
> +template <typename T>
> +struct U {};
> +decltype(U) z;  // { dg-error "missing template arguments" }
> -- 
> 2.40.0
> 

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

* [PATCH] c++: Report invalid id-expression in decltype [PR100482]
  2023-04-30  2:00 [PATCH] c++: Report invalid id-expression in decltype [PR100482] Nathaniel Shead
  2023-05-27  7:11 ` ping: " Nathaniel Shead
@ 2023-06-13 10:13 ` Nathaniel Shead
  2023-06-23 16:15 ` Patrick Palka
  2 siblings, 0 replies; 8+ messages in thread
From: Nathaniel Shead @ 2023-06-13 10:13 UTC (permalink / raw)
  To: gcc-patches

(Another) ping.

On Sun, Apr 30, 2023 at 12:00:05PM +1000, Nathaniel Shead wrote:
> This patch ensures that any errors raised by finish_id_expression when
> parsing a decltype expression are properly reported, rather than
> potentially going ignored and causing invalid code to be accepted.
> 
> We can also now remove the separate check for templates without args as
> this is also checked for in finish_id_expression.
> 
> 	PR 100482
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_parser_decltype_expr): Report errors raised by
> 	finish_id_expression.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/pr100482.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>  gcc/cp/parser.cc                | 22 +++++++++++-----------
>  gcc/testsuite/g++.dg/pr100482.C | 11 +++++++++++
>  2 files changed, 22 insertions(+), 11 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/pr100482.C
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index e5f032f2330..20ebcdc3cfd 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
>  	expr = cp_parser_lookup_name_simple (parser, expr,
>  					     id_expr_start_token->location);
>  
> -      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
> -	/* A template without args is not a complete id-expression.  */
> -	expr = error_mark_node;
> -
>        if (expr
>            && expr != error_mark_node
>            && TREE_CODE (expr) != TYPE_DECL
> @@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
>                     &error_msg,
>  		   id_expr_start_token->location));
>  
> -          if (expr == error_mark_node)
> -            /* We found an id-expression, but it was something that we
> -               should not have found. This is an error, not something
> -               we can recover from, so note that we found an
> -               id-expression and we'll recover as gracefully as
> -               possible.  */
> -            id_expression_or_member_access_p = true;
> +	  if (error_msg)
> +	    {
> +	      /* We found an id-expression, but it was something that we
> +		 should not have found. This is an error, not something
> +		 we can recover from, so report the error we found and
> +		 we'll recover as gracefully as possible.  */
> +	      cp_parser_parse_definitely (parser);
> +	      cp_parser_error (parser, error_msg);
> +	      id_expression_or_member_access_p = true;
> +	      return error_mark_node;
> +	    }
>          }
>  
>        if (expr
> diff --git a/gcc/testsuite/g++.dg/pr100482.C b/gcc/testsuite/g++.dg/pr100482.C
> new file mode 100644
> index 00000000000..dcf6722fda5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/pr100482.C
> @@ -0,0 +1,11 @@
> +// { dg-do compile { target c++11 } }
> +
> +namespace N {}
> +decltype(std) x;   // { dg-error "expected primary-expression" }
> +
> +struct S {};
> +decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
> +
> +template <typename T>
> +struct U {};
> +decltype(U) z;  // { dg-error "missing template arguments" }
> -- 
> 2.40.0
> 

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

* Re: [PATCH] c++: Report invalid id-expression in decltype [PR100482]
  2023-04-30  2:00 [PATCH] c++: Report invalid id-expression in decltype [PR100482] Nathaniel Shead
  2023-05-27  7:11 ` ping: " Nathaniel Shead
  2023-06-13 10:13 ` Nathaniel Shead
@ 2023-06-23 16:15 ` Patrick Palka
  2023-06-24 13:27   ` Nathaniel Shead
  2 siblings, 1 reply; 8+ messages in thread
From: Patrick Palka @ 2023-06-23 16:15 UTC (permalink / raw)
  To: Nathaniel Shead; +Cc: gcc-patches

On Sun, 30 Apr 2023, Nathaniel Shead via Gcc-patches wrote:

> This patch ensures that any errors raised by finish_id_expression when
> parsing a decltype expression are properly reported, rather than
> potentially going ignored and causing invalid code to be accepted.
> 
> We can also now remove the separate check for templates without args as
> this is also checked for in finish_id_expression.
> 
> 	PR 100482
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_parser_decltype_expr): Report errors raised by
> 	finish_id_expression.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/pr100482.C: New test.

LGTM.  Some minor comments about the new testcase below:

> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>  gcc/cp/parser.cc                | 22 +++++++++++-----------
>  gcc/testsuite/g++.dg/pr100482.C | 11 +++++++++++
>  2 files changed, 22 insertions(+), 11 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/pr100482.C
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index e5f032f2330..20ebcdc3cfd 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
>  	expr = cp_parser_lookup_name_simple (parser, expr,
>  					     id_expr_start_token->location);
>  
> -      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
> -	/* A template without args is not a complete id-expression.  */
> -	expr = error_mark_node;
> -
>        if (expr
>            && expr != error_mark_node
>            && TREE_CODE (expr) != TYPE_DECL
> @@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
>                     &error_msg,
>  		   id_expr_start_token->location));
>  
> -          if (expr == error_mark_node)
> -            /* We found an id-expression, but it was something that we
> -               should not have found. This is an error, not something
> -               we can recover from, so note that we found an
> -               id-expression and we'll recover as gracefully as
> -               possible.  */
> -            id_expression_or_member_access_p = true;
> +	  if (error_msg)
> +	    {
> +	      /* We found an id-expression, but it was something that we
> +		 should not have found. This is an error, not something
> +		 we can recover from, so report the error we found and
> +		 we'll recover as gracefully as possible.  */
> +	      cp_parser_parse_definitely (parser);
> +	      cp_parser_error (parser, error_msg);
> +	      id_expression_or_member_access_p = true;
> +	      return error_mark_node;
> +	    }
>          }
>  
>        if (expr
> diff --git a/gcc/testsuite/g++.dg/pr100482.C b/gcc/testsuite/g++.dg/pr100482.C
> new file mode 100644
> index 00000000000..dcf6722fda5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/pr100482.C

We generally prefer to organize tests according to the language dialect
they apply to and the langugae construct that they're primarily testing.
In this case we could name the test e.g.

  gcc/testsuite/g++.dg/cpp0x/decltype-100482.C

> @@ -0,0 +1,11 @@
> +// { dg-do compile { target c++10 } }

We also usually mention the PR number in the test as a comment:

// PR c++/100482

One benefit of doing so is that the git alias 'git gcc-commit-mklog'
(https://gcc.gnu.org/gitwrite.html#vendor) will then automatically
include the PR number in the commit message template.

> +
> +namespace N {}
> +decltype(std) x;   // { dg-error "expected primary-expression" }
> +
> +struct S {};
> +decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
> +
> +template <typename T>
> +struct U {};
> +decltype(U) z;  // { dg-error "missing template arguments" }
> -- 
> 2.40.0
> 
> 


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

* Re: [PATCH] c++: Report invalid id-expression in decltype [PR100482]
  2023-06-23 16:15 ` Patrick Palka
@ 2023-06-24 13:27   ` Nathaniel Shead
  2023-08-08  2:40     ` Nathaniel Shead
  0 siblings, 1 reply; 8+ messages in thread
From: Nathaniel Shead @ 2023-06-24 13:27 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches

On Fri, Jun 23, 2023 at 12:15:32PM -0400, Patrick Palka wrote:
> On Sun, 30 Apr 2023, Nathaniel Shead via Gcc-patches wrote:
> 
> > This patch ensures that any errors raised by finish_id_expression when
> > parsing a decltype expression are properly reported, rather than
> > potentially going ignored and causing invalid code to be accepted.
> > 
> > We can also now remove the separate check for templates without args as
> > this is also checked for in finish_id_expression.
> > 
> > 	PR 100482
> > 
> > gcc/cp/ChangeLog:
> > 
> > 	* parser.cc (cp_parser_decltype_expr): Report errors raised by
> > 	finish_id_expression.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > 	* g++.dg/pr100482.C: New test.
> 
> LGTM.  Some minor comments about the new testcase below:
> 
> > 
> > Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> > ---
> >  gcc/cp/parser.cc                | 22 +++++++++++-----------
> >  gcc/testsuite/g++.dg/pr100482.C | 11 +++++++++++
> >  2 files changed, 22 insertions(+), 11 deletions(-)
> >  create mode 100644 gcc/testsuite/g++.dg/pr100482.C
> > 
> > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> > index e5f032f2330..20ebcdc3cfd 100644
> > --- a/gcc/cp/parser.cc
> > +++ b/gcc/cp/parser.cc
> > @@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
> >  	expr = cp_parser_lookup_name_simple (parser, expr,
> >  					     id_expr_start_token->location);
> >  
> > -      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
> > -	/* A template without args is not a complete id-expression.  */
> > -	expr = error_mark_node;
> > -
> >        if (expr
> >            && expr != error_mark_node
> >            && TREE_CODE (expr) != TYPE_DECL
> > @@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
> >                     &error_msg,
> >  		   id_expr_start_token->location));
> >  
> > -          if (expr == error_mark_node)
> > -            /* We found an id-expression, but it was something that we
> > -               should not have found. This is an error, not something
> > -               we can recover from, so note that we found an
> > -               id-expression and we'll recover as gracefully as
> > -               possible.  */
> > -            id_expression_or_member_access_p = true;
> > +	  if (error_msg)
> > +	    {
> > +	      /* We found an id-expression, but it was something that we
> > +		 should not have found. This is an error, not something
> > +		 we can recover from, so report the error we found and
> > +		 we'll recover as gracefully as possible.  */
> > +	      cp_parser_parse_definitely (parser);
> > +	      cp_parser_error (parser, error_msg);
> > +	      id_expression_or_member_access_p = true;
> > +	      return error_mark_node;
> > +	    }
> >          }
> >  
> >        if (expr
> > diff --git a/gcc/testsuite/g++.dg/pr100482.C b/gcc/testsuite/g++.dg/pr100482.C
> > new file mode 100644
> > index 00000000000..dcf6722fda5
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/pr100482.C
> 
> We generally prefer to organize tests according to the language dialect
> they apply to and the langugae construct that they're primarily testing.
> In this case we could name the test e.g.
> 
>   gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
> 
> > @@ -0,0 +1,11 @@
> > +// { dg-do compile { target c++10 } }
> 
> We also usually mention the PR number in the test as a comment:
> 
> // PR c++/100482
> 
> One benefit of doing so is that the git alias 'git gcc-commit-mklog'
> (https://gcc.gnu.org/gitwrite.html#vendor) will then automatically
> include the PR number in the commit message template.
> 
> > +
> > +namespace N {}
> > +decltype(std) x;   // { dg-error "expected primary-expression" }
> > +
> > +struct S {};
> > +decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
> > +
> > +template <typename T>
> > +struct U {};
> > +decltype(U) z;  // { dg-error "missing template arguments" }
> > -- 
> > 2.40.0
> > 
> > 
> 

Thanks for the comments. I've fixed the test, does this look OK?

-- 8< --

This patch ensures that any errors raised by finish_id_expression when
parsing a decltype expression are properly reported, rather than
potentially going ignored and causing invalid code to be accepted.

We can also now remove the separate check for templates without args as
this is also checked for in finish_id_expression.

	PR c++/100482

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_decltype_expr): Report errors raised by
        finish_id_expression.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/decltype-100482.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/parser.cc                             | 22 ++++++++++----------
 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C | 12 +++++++++++
 2 files changed, 23 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index dd3665c8ccf..990f2034f54 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
 	expr = cp_parser_lookup_name_simple (parser, expr,
 					     id_expr_start_token->location);
 
-      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
-	/* A template without args is not a complete id-expression.  */
-	expr = error_mark_node;
-
       if (expr
           && expr != error_mark_node
           && TREE_CODE (expr) != TYPE_DECL
@@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
                    &error_msg,
 		   id_expr_start_token->location));
 
-          if (expr == error_mark_node)
-            /* We found an id-expression, but it was something that we
-               should not have found. This is an error, not something
-               we can recover from, so note that we found an
-               id-expression and we'll recover as gracefully as
-               possible.  */
-            id_expression_or_member_access_p = true;
+	  if (error_msg)
+	    {
+	      /* We found an id-expression, but it was something that we
+		 should not have found. This is an error, not something
+		 we can recover from, so report the error we found and
+		 we'll recover as gracefully as possible.  */
+	      cp_parser_parse_definitely (parser);
+	      cp_parser_error (parser, error_msg);
+	      id_expression_or_member_access_p = true;
+	      return error_mark_node;
+	    }
         }
 
       if (expr
diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
new file mode 100644
index 00000000000..3df9c8496d7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
@@ -0,0 +1,12 @@
+// PR c++/100482
+// { dg-do compile { target c++11 } }
+
+namespace N {}
+decltype(std) x;   // { dg-error "expected primary-expression" }
+
+struct S {};
+decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
+
+template <typename T>
+struct U {};
+decltype(U) z;  // { dg-error "missing template arguments" }
-- 
2.41.0


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

* [PATCH] c++: Report invalid id-expression in decltype [PR100482]
  2023-06-24 13:27   ` Nathaniel Shead
@ 2023-08-08  2:40     ` Nathaniel Shead
  2023-08-08  2:48       ` Nathaniel Shead
  0 siblings, 1 reply; 8+ messages in thread
From: Nathaniel Shead @ 2023-08-08  2:40 UTC (permalink / raw)
  To: gcc-patches; +Cc: Patrick Palka, Jason Merrill

Ping for https://gcc.gnu.org/pipermail/gcc-patches/2023-June/622681.html.

-- 8< --

This patch ensures that any errors raised by finish_id_expression when
parsing a decltype expression are properly reported, rather than
potentially going ignored and causing invalid code to be accepted.

We can also now remove the separate check for templates without args as
this is also checked for in finish_id_expression.

	PR c++/100482

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_decltype_expr): Report errors raised by
        finish_id_expression.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/decltype-100482.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/parser.cc                             | 22 ++++++++++----------
 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C | 12 +++++++++++
 2 files changed, 23 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index dd3665c8ccf..990f2034f54 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
 	expr = cp_parser_lookup_name_simple (parser, expr,
 					     id_expr_start_token->location);
 
-      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
-	/* A template without args is not a complete id-expression.  */
-	expr = error_mark_node;
-
       if (expr
           && expr != error_mark_node
           && TREE_CODE (expr) != TYPE_DECL
@@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
                    &error_msg,
 		   id_expr_start_token->location));
 
-          if (expr == error_mark_node)
-            /* We found an id-expression, but it was something that we
-               should not have found. This is an error, not something
-               we can recover from, so note that we found an
-               id-expression and we'll recover as gracefully as
-               possible.  */
-            id_expression_or_member_access_p = true;
+	  if (error_msg)
+	    {
+	      /* We found an id-expression, but it was something that we
+		 should not have found. This is an error, not something
+		 we can recover from, so report the error we found and
+		 we'll recover as gracefully as possible.  */
+	      cp_parser_parse_definitely (parser);
+	      cp_parser_error (parser, error_msg);
+	      id_expression_or_member_access_p = true;
+	      return error_mark_node;
+	    }
         }
 
       if (expr
diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
new file mode 100644
index 00000000000..3df9c8496d7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
@@ -0,0 +1,12 @@
+// PR c++/100482
+// { dg-do compile { target c++11 } }
+
+namespace N {}
+decltype(std) x;   // { dg-error "expected primary-expression" }
+
+struct S {};
+decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
+
+template <typename T>
+struct U {};
+decltype(U) z;  // { dg-error "missing template arguments" }
-- 
2.41.0


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

* [PATCH] c++: Report invalid id-expression in decltype [PR100482]
  2023-08-08  2:40     ` Nathaniel Shead
@ 2023-08-08  2:48       ` Nathaniel Shead
  2023-08-08 20:04         ` Jason Merrill
  0 siblings, 1 reply; 8+ messages in thread
From: Nathaniel Shead @ 2023-08-08  2:48 UTC (permalink / raw)
  To: gcc-patches; +Cc: Patrick Palka, Jason Merrill

Sorry, noticed I provided the wrong version of the test. Here is the
correct version (not relying on 'namespace std' being implicitly
defined). Bootstrapped + regtested on x86_64-pc-linux-gnu.

-- 8< --

This patch ensures that any errors raised by finish_id_expression when
parsing a decltype expression are properly reported, rather than
potentially going ignored and causing invalid code to be accepted.

We can also now remove the separate check for templates without args as
this is also checked for in finish_id_expression.

	PR c++/100482

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_decltype_expr): Report errors raised by
        finish_id_expression.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/decltype-100482.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/parser.cc                             | 22 ++++++++++----------
 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C | 12 +++++++++++
 2 files changed, 23 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index d7ef5b34d42..119a14d03b9 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -16506,10 +16506,6 @@ cp_parser_decltype_expr (cp_parser *parser,
 	expr = cp_parser_lookup_name_simple (parser, expr,
 					     id_expr_start_token->location);
 
-      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
-	/* A template without args is not a complete id-expression.  */
-	expr = error_mark_node;
-
       if (expr
           && expr != error_mark_node
           && TREE_CODE (expr) != TYPE_DECL
@@ -16530,13 +16526,17 @@ cp_parser_decltype_expr (cp_parser *parser,
                    &error_msg,
 		   id_expr_start_token->location));
 
-          if (expr == error_mark_node)
-            /* We found an id-expression, but it was something that we
-               should not have found. This is an error, not something
-               we can recover from, so note that we found an
-               id-expression and we'll recover as gracefully as
-               possible.  */
-            id_expression_or_member_access_p = true;
+	  if (error_msg)
+	    {
+	      /* We found an id-expression, but it was something that we
+		 should not have found. This is an error, not something
+		 we can recover from, so report the error we found and
+		 we'll recover as gracefully as possible.  */
+	      cp_parser_parse_definitely (parser);
+	      cp_parser_error (parser, error_msg);
+	      id_expression_or_member_access_p = true;
+	      return error_mark_node;
+	    }
         }
 
       if (expr
diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
new file mode 100644
index 00000000000..1df8b162743
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
@@ -0,0 +1,12 @@
+// PR c++/100482
+// { dg-do compile { target c++11 } }
+
+namespace N {}
+decltype(N) x;   // { dg-error "expected primary-expression" }
+
+struct S {};
+decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
+
+template <typename T>
+struct U {};
+decltype(U) z;  // { dg-error "missing template arguments" }
-- 
2.41.0


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

* Re: [PATCH] c++: Report invalid id-expression in decltype [PR100482]
  2023-08-08  2:48       ` Nathaniel Shead
@ 2023-08-08 20:04         ` Jason Merrill
  0 siblings, 0 replies; 8+ messages in thread
From: Jason Merrill @ 2023-08-08 20:04 UTC (permalink / raw)
  To: Nathaniel Shead, gcc-patches; +Cc: Patrick Palka

On 8/7/23 22:48, Nathaniel Shead wrote:
> Sorry, noticed I provided the wrong version of the test. Here is the
> correct version (not relying on 'namespace std' being implicitly
> defined). Bootstrapped + regtested on x86_64-pc-linux-gnu.

Pushed, thanks!

> -- 8< --
> 
> This patch ensures that any errors raised by finish_id_expression when
> parsing a decltype expression are properly reported, rather than
> potentially going ignored and causing invalid code to be accepted.
> 
> We can also now remove the separate check for templates without args as
> this is also checked for in finish_id_expression.
> 
> 	PR c++/100482
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_parser_decltype_expr): Report errors raised by
>          finish_id_expression.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/decltype-100482.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>   gcc/cp/parser.cc                             | 22 ++++++++++----------
>   gcc/testsuite/g++.dg/cpp0x/decltype-100482.C | 12 +++++++++++
>   2 files changed, 23 insertions(+), 11 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index d7ef5b34d42..119a14d03b9 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -16506,10 +16506,6 @@ cp_parser_decltype_expr (cp_parser *parser,
>   	expr = cp_parser_lookup_name_simple (parser, expr,
>   					     id_expr_start_token->location);
>   
> -      if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
> -	/* A template without args is not a complete id-expression.  */
> -	expr = error_mark_node;
> -
>         if (expr
>             && expr != error_mark_node
>             && TREE_CODE (expr) != TYPE_DECL
> @@ -16530,13 +16526,17 @@ cp_parser_decltype_expr (cp_parser *parser,
>                      &error_msg,
>   		   id_expr_start_token->location));
>   
> -          if (expr == error_mark_node)
> -            /* We found an id-expression, but it was something that we
> -               should not have found. This is an error, not something
> -               we can recover from, so note that we found an
> -               id-expression and we'll recover as gracefully as
> -               possible.  */
> -            id_expression_or_member_access_p = true;
> +	  if (error_msg)
> +	    {
> +	      /* We found an id-expression, but it was something that we
> +		 should not have found. This is an error, not something
> +		 we can recover from, so report the error we found and
> +		 we'll recover as gracefully as possible.  */
> +	      cp_parser_parse_definitely (parser);
> +	      cp_parser_error (parser, error_msg);
> +	      id_expression_or_member_access_p = true;
> +	      return error_mark_node;
> +	    }
>           }
>   
>         if (expr
> diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
> new file mode 100644
> index 00000000000..1df8b162743
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
> @@ -0,0 +1,12 @@
> +// PR c++/100482
> +// { dg-do compile { target c++11 } }
> +
> +namespace N {}
> +decltype(N) x;   // { dg-error "expected primary-expression" }
> +
> +struct S {};
> +decltype(S) y;  // { dg-error "argument to .decltype. must be an expression" }
> +
> +template <typename T>
> +struct U {};
> +decltype(U) z;  // { dg-error "missing template arguments" }


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

end of thread, other threads:[~2023-08-08 20:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-30  2:00 [PATCH] c++: Report invalid id-expression in decltype [PR100482] Nathaniel Shead
2023-05-27  7:11 ` ping: " Nathaniel Shead
2023-06-13 10:13 ` Nathaniel Shead
2023-06-23 16:15 ` Patrick Palka
2023-06-24 13:27   ` Nathaniel Shead
2023-08-08  2:40     ` Nathaniel Shead
2023-08-08  2:48       ` Nathaniel Shead
2023-08-08 20:04         ` 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).