public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++] Fix decomp ICE with invalid initializer (PR c++/81258)
@ 2017-06-30 17:24 Jakub Jelinek
  2017-06-30 17:38 ` Nathan Sidwell
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2017-06-30 17:24 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

The initializer for structured binding has to be one of:
= assignment-expression
( assignment-expression )
{ assignment-expression }
but cp_parser_initializer can parse other forms, with fewer or more
expressions in there.  Some cases we caught with various cryptic errors
or pedwarns, but others we just ICEd on.

The following patch attempts to check this.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2017-06-30  Jakub Jelinek  <jakub@redhat.com>

	PR c++/81258
	* parser.c (cp_parser_decomposition_declaration): Diagnose invalid
	forms of structured binding initializers.

	* g++.dg/cpp1z/decomp21.C (foo): Adjust expected diagnostics.
	* g++.dg/cpp1z/decomp30.C: New test.

--- gcc/cp/parser.c.jj	2017-06-30 09:49:25.000000000 +0200
+++ gcc/cp/parser.c	2017-06-30 11:03:18.526521000 +0200
@@ -13196,6 +13196,15 @@ cp_parser_decomposition_declaration (cp_
       *init_loc = cp_lexer_peek_token (parser->lexer)->location;
       tree initializer = cp_parser_initializer (parser, &is_direct_init,
 						&non_constant_p);
+      if (initializer == NULL_TREE
+	  || (TREE_CODE (initializer) == TREE_LIST
+	      && TREE_CHAIN (initializer))
+	  || (TREE_CODE (initializer) == CONSTRUCTOR
+	      && CONSTRUCTOR_NELTS (initializer) != 1))
+	{
+	  error_at (loc, "invalid initializer for structured binding");
+	  initializer = error_mark_node;
+	}
 
       if (decl != error_mark_node)
 	{
--- gcc/testsuite/g++.dg/cpp1z/decomp21.C.jj	2017-01-19 17:01:21.000000000 +0100
+++ gcc/testsuite/g++.dg/cpp1z/decomp21.C	2017-06-30 11:07:04.786746784 +0200
@@ -12,5 +12,5 @@ foo ()
   auto [ n, o, p ] { a };
   auto [ q, r, t ] ( s );
   auto [ u, v, w ] ( s, );      // { dg-error "expected primary-expression before '.' token" }
-  auto [ x, y, z ] ( a );       // { dg-error "expression list treated as compound expression in initializer" "" { target *-*-* } .-1 }
+  auto [ x, y, z ] ( a );       // { dg-error "invalid initializer for structured binding" "" { target *-*-* } .-1 }
 }
--- gcc/testsuite/g++.dg/cpp1z/decomp30.C.jj	2017-06-30 11:09:31.934942575 +0200
+++ gcc/testsuite/g++.dg/cpp1z/decomp30.C	2017-06-30 11:09:22.000000000 +0200
@@ -0,0 +1,12 @@
+// PR c++/81258
+// { dg-options -std=c++1z }
+
+int a[2];
+auto [b, c] (a);
+auto [d, e] { a };
+auto [f, g] = a;
+auto [h, i] ( a, a );	// { dg-error "invalid initializer for structured binding" }
+auto [j, k] { a, a };	// { dg-error "invalid initializer for structured binding" }
+auto [l, m] = { a };	// { dg-error "deducing from brace-enclosed initializer list requires" }
+auto [n, o] {};		// { dg-error "invalid initializer for structured binding" }
+auto [p, q] ();		// { dg-error "invalid initializer for structured binding" }

	Jakub

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

* Re: [C++] Fix decomp ICE with invalid initializer (PR c++/81258)
  2017-06-30 17:24 [C++] Fix decomp ICE with invalid initializer (PR c++/81258) Jakub Jelinek
@ 2017-06-30 17:38 ` Nathan Sidwell
  2017-07-03 16:05   ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Nathan Sidwell @ 2017-06-30 17:38 UTC (permalink / raw)
  To: Jakub Jelinek, Jason Merrill; +Cc: gcc-patches

On 06/30/2017 01:24 PM, Jakub Jelinek wrote:

> The initializer for structured binding has to be one of:
> = assignment-expression
> ( assignment-expression )
> { assignment-expression }
> but cp_parser_initializer can parse other forms, with fewer or more
> expressions in there.  Some cases we caught with various cryptic errors
> or pedwarns, but others we just ICEd on.
> 
> The following patch attempts to check this.

ok, but ...

> --- gcc/testsuite/g++.dg/cpp1z/decomp21.C.jj	2017-01-19 17:01:21.000000000 +0100
> +++ gcc/testsuite/g++.dg/cpp1z/decomp21.C	2017-06-30 11:07:04.786746784 +0200
> @@ -12,5 +12,5 @@ foo ()
>     auto [ n, o, p ] { a };
>     auto [ q, r, t ] ( s );
>     auto [ u, v, w ] ( s, );      // { dg-error "expected primary-expression before '.' token" }
> -  auto [ x, y, z ] ( a );       // { dg-error "expression list treated as compound expression in initializer" "" { target *-*-* } .-1 }
> +  auto [ x, y, z ] ( a );       // { dg-error "invalid initializer for structured binding" "" { target *-*-* } .-1 }
>   }

The .-1 on the final error is actually about the previous statement, not 
the line it's lexically on.  Could you put it on a line on its own, 
while you're there?

-- 
Nathan Sidwell

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

* Re: [C++] Fix decomp ICE with invalid initializer (PR c++/81258)
  2017-06-30 17:38 ` Nathan Sidwell
@ 2017-07-03 16:05   ` Jakub Jelinek
  2017-07-03 16:48     ` Nathan Sidwell
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2017-07-03 16:05 UTC (permalink / raw)
  To: Jason Merrill, Nathan Sidwell; +Cc: gcc-patches

On Fri, Jun 30, 2017 at 01:38:13PM -0400, Nathan Sidwell wrote:
> On 06/30/2017 01:24 PM, Jakub Jelinek wrote:
> 
> > The initializer for structured binding has to be one of:
> > = assignment-expression
> > ( assignment-expression )
> > { assignment-expression }
> > but cp_parser_initializer can parse other forms, with fewer or more
> > expressions in there.  Some cases we caught with various cryptic errors
> > or pedwarns, but others we just ICEd on.
> > 
> > The following patch attempts to check this.
> 
> ok, but ...
> 
> > --- gcc/testsuite/g++.dg/cpp1z/decomp21.C.jj	2017-01-19 17:01:21.000000000 +0100
> > +++ gcc/testsuite/g++.dg/cpp1z/decomp21.C	2017-06-30 11:07:04.786746784 +0200
> > @@ -12,5 +12,5 @@ foo ()
> >     auto [ n, o, p ] { a };
> >     auto [ q, r, t ] ( s );
> >     auto [ u, v, w ] ( s, );      // { dg-error "expected primary-expression before '.' token" }
> > -  auto [ x, y, z ] ( a );       // { dg-error "expression list treated as compound expression in initializer" "" { target *-*-* } .-1 }
> > +  auto [ x, y, z ] ( a );       // { dg-error "invalid initializer for structured binding" "" { target *-*-* } .-1 }
> >   }
> 
> The .-1 on the final error is actually about the previous statement, not the
> line it's lexically on.  Could you put it on a line on its own, while you're
> there?

Ok.  In the light of the http://gcc.gnu.org/ml/gcc-patches/2017-06/msg02432.html
thread, shouldn't this be structured binding declaration then?
I.e.

2017-07-03  Jakub Jelinek  <jakub@redhat.com>

	PR c++/81258
	* parser.c (cp_parser_decomposition_declaration): Diagnose invalid
	forms of structured binding initializers.

	* g++.dg/cpp1z/decomp21.C (foo): Adjust expected diagnostics.
	* g++.dg/cpp1z/decomp30.C: New test.

--- gcc/cp/parser.c.jj	2017-06-30 09:49:25.000000000 +0200
+++ gcc/cp/parser.c	2017-06-30 11:03:18.526521000 +0200
@@ -13196,6 +13196,16 @@ cp_parser_decomposition_declaration (cp_
       *init_loc = cp_lexer_peek_token (parser->lexer)->location;
       tree initializer = cp_parser_initializer (parser, &is_direct_init,
 						&non_constant_p);
+      if (initializer == NULL_TREE
+	  || (TREE_CODE (initializer) == TREE_LIST
+	      && TREE_CHAIN (initializer))
+	  || (TREE_CODE (initializer) == CONSTRUCTOR
+	      && CONSTRUCTOR_NELTS (initializer) != 1))
+	{
+	  error_at (loc, "invalid initializer for structured binding "
+		    "declaration");
+	  initializer = error_mark_node;
+	}
 
       if (decl != error_mark_node)
 	{
--- gcc/testsuite/g++.dg/cpp1z/decomp21.C.jj	2017-01-19 17:01:21.000000000 +0100
+++ gcc/testsuite/g++.dg/cpp1z/decomp21.C	2017-06-30 11:07:04.786746784 +0200
@@ -12,5 +12,6 @@ foo ()
   auto [ n, o, p ] { a };
   auto [ q, r, t ] ( s );
   auto [ u, v, w ] ( s, );      // { dg-error "expected primary-expression before '.' token" }
-  auto [ x, y, z ] ( a );       // { dg-error "expression list treated as compound expression in initializer" "" { target *-*-* } .-1 }
+				// { dg-error "invalid initializer for structured binding declaration" "" { target *-*-* } .-1 }
+  auto [ x, y, z ] ( a );
 }
--- gcc/testsuite/g++.dg/cpp1z/decomp30.C.jj	2017-06-30 11:09:31.934942575 +0200
+++ gcc/testsuite/g++.dg/cpp1z/decomp30.C	2017-06-30 11:09:22.000000000 +0200
@@ -0,0 +1,12 @@
+// PR c++/81258
+// { dg-options -std=c++1z }
+
+int a[2];
+auto [b, c] (a);
+auto [d, e] { a };
+auto [f, g] = a;
+auto [h, i] ( a, a );	// { dg-error "invalid initializer for structured binding declaration" }
+auto [j, k] { a, a };	// { dg-error "invalid initializer for structured binding declaration" }
+auto [l, m] = { a };	// { dg-error "deducing from brace-enclosed initializer list requires" }
+auto [n, o] {};		// { dg-error "invalid initializer for structured binding declaration" }
+auto [p, q] ();		// { dg-error "invalid initializer for structured binding declaration" }


	Jakub

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

* Re: [C++] Fix decomp ICE with invalid initializer (PR c++/81258)
  2017-07-03 16:05   ` Jakub Jelinek
@ 2017-07-03 16:48     ` Nathan Sidwell
  0 siblings, 0 replies; 4+ messages in thread
From: Nathan Sidwell @ 2017-07-03 16:48 UTC (permalink / raw)
  To: Jakub Jelinek, Jason Merrill; +Cc: gcc-patches

On 07/03/2017 12:05 PM, Jakub Jelinek wrote:

> Ok.  In the light of the http://gcc.gnu.org/ml/gcc-patches/2017-06/msg02432.html
> thread, shouldn't this be structured binding declaration then?
> I.e.

Yes, I think that's better, thanks!

nathan
-- 
Nathan Sidwell

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

end of thread, other threads:[~2017-07-03 16:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-30 17:24 [C++] Fix decomp ICE with invalid initializer (PR c++/81258) Jakub Jelinek
2017-06-30 17:38 ` Nathan Sidwell
2017-07-03 16:05   ` Jakub Jelinek
2017-07-03 16:48     ` Nathan Sidwell

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).