diff mbox series

c++: Fix ICE with structured binding initialized to incomplete array [PR97878]

Message ID 20210204084821.GJ4020736@tucnak
State New
Headers show
Series c++: Fix ICE with structured binding initialized to incomplete array [PR97878] | expand

Commit Message

Jakub Jelinek Feb. 4, 2021, 8:48 a.m. UTC
Hi!

We ICE on the following testcase, for incomplete array a on auto [b] { a }; without
giving any kind of diagnostics, with auto [c] = a; during error-recovery.
The problem is that we get too far through check_initializer and e.g.
store_init_value -> constexpr stuff can't deal with incomplete array types.

As the type of the structured binding artificial variable is always deduced,
I think it is easiest to diagnose this early, even if they have array types
we'll need their deduced type to be complete rather than just its element
type.

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

2021-02-04  Jakub Jelinek  <jakub@redhat.com>

	PR c++/97878
	* decl.c (check_initializer): For structured bindings, require
	complete type even when they have array type.

	* g++.dg/cpp1z/decomp54.C: New test.


	Jakub

Comments

Jason Merrill Feb. 4, 2021, 4:03 p.m. UTC | #1
On 2/4/21 3:48 AM, Jakub Jelinek wrote:
> Hi!
> 
> We ICE on the following testcase, for incomplete array a on auto [b] { a }; without
> giving any kind of diagnostics, with auto [c] = a; during error-recovery.
> The problem is that we get too far through check_initializer and e.g.
> store_init_value -> constexpr stuff can't deal with incomplete array types.
> 
> As the type of the structured binding artificial variable is always deduced,
> I think it is easiest to diagnose this early, even if they have array types
> we'll need their deduced type to be complete rather than just its element
> type.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> 2021-02-04  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/97878
> 	* decl.c (check_initializer): For structured bindings, require
> 	complete type even when they have array type.
> 
> 	* g++.dg/cpp1z/decomp54.C: New test.
> 
> --- gcc/cp/decl.c.jj	2021-01-28 16:13:04.000000000 +0100
> +++ gcc/cp/decl.c	2021-02-03 15:57:16.980557836 +0100
> @@ -6837,7 +6837,8 @@ check_initializer (tree decl, tree init,
>       /* We will have already complained.  */
>       return NULL_TREE;
>   
> -  if (TREE_CODE (type) == ARRAY_TYPE)
> +  if (TREE_CODE (type) == ARRAY_TYPE
> +      && (!DECL_DECOMPOSITION_P (decl) || DECL_DECOMP_BASE (decl)))

This needs a comment.  Or handle this inside check_array_initializer, 
where we could give a more helpful diagnostic?

>       {
>         if (check_array_initializer (decl, type, init))
>   	return NULL_TREE;
> --- gcc/testsuite/g++.dg/cpp1z/decomp54.C.jj	2021-02-03 16:21:27.991237734 +0100
> +++ gcc/testsuite/g++.dg/cpp1z/decomp54.C	2021-02-03 16:21:10.984428831 +0100
> @@ -0,0 +1,17 @@
> +// PR c++/97878
> +// { dg-do compile { target c++11 } }
> +// { dg-options "" }
> +
> +extern int a[];
> +auto [b] { a };	// { dg-error "has incomplete type" }
> +		// { dg-warning "only available with" "" { target c++14_down } .-1 }
> +auto [c] = a;	// { dg-error "has incomplete type" }
> +		// { dg-warning "only available with" "" { target c++14_down } .-1 }
> +extern int d[0];
> +auto [e] { d };	// { dg-error "too many initializers for" }
> +		// { dg-error "1 name provided for structured binding" "" { target *-*-* } .-1 }
> +		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-2 }
> +		// { dg-warning "only available with" "" { target c++14_down } .-3 }
> +auto [f] = d;	// { dg-error "1 name provided for structured binding" }
> +		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-1 }
> +		// { dg-warning "only available with" "" { target c++14_down } .-2 }
> 
> 	Jakub
>
Jakub Jelinek Feb. 4, 2021, 5:47 p.m. UTC | #2
On Thu, Feb 04, 2021 at 11:03:10AM -0500, Jason Merrill wrote:
> > --- gcc/cp/decl.c.jj	2021-01-28 16:13:04.000000000 +0100
> > +++ gcc/cp/decl.c	2021-02-03 15:57:16.980557836 +0100
> > @@ -6837,7 +6837,8 @@ check_initializer (tree decl, tree init,
> >       /* We will have already complained.  */
> >       return NULL_TREE;
> > -  if (TREE_CODE (type) == ARRAY_TYPE)
> > +  if (TREE_CODE (type) == ARRAY_TYPE
> > +      && (!DECL_DECOMPOSITION_P (decl) || DECL_DECOMP_BASE (decl)))
> 
> This needs a comment.  Or handle this inside check_array_initializer, where
> we could give a more helpful diagnostic?

So like this?

2021-02-04  Jakub Jelinek  <jakub@redhat.com>

	PR c++/97878
	* decl.c (check_array_initializer): For structured bindings, require
	the array type to be complete.

	* g++.dg/cpp1z/decomp54.C: New test.

--- gcc/cp/decl.c.jj	2021-02-03 17:13:58.567872007 +0100
+++ gcc/cp/decl.c	2021-02-04 18:42:35.347374198 +0100
@@ -6768,6 +6768,19 @@ check_array_initializer (tree decl, tree
 {
   tree element_type = TREE_TYPE (type);
 
+  /* Structured binding when initialized with an array type needs
+     to have complete type.  */
+  if (decl
+      && DECL_DECOMPOSITION_P (decl)
+      && !DECL_DECOMP_BASE (decl)
+      && !COMPLETE_TYPE_P (type))
+    {
+      error_at (DECL_SOURCE_LOCATION (decl),
+		"structured binding has incomplete type");
+      TREE_TYPE (decl) = error_mark_node;
+      return true;
+    }
+
   /* The array type itself need not be complete, because the
      initializer may tell us how many elements are in the array.
      But, the elements of the array must be complete.  */
--- gcc/testsuite/g++.dg/cpp1z/decomp54.C.jj	2021-02-04 18:33:34.829393825 +0100
+++ gcc/testsuite/g++.dg/cpp1z/decomp54.C	2021-02-04 18:33:34.829393825 +0100
@@ -0,0 +1,17 @@
+// PR c++/97878
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+extern int a[];
+auto [b] { a };	// { dg-error "has incomplete type" }
+		// { dg-warning "only available with" "" { target c++14_down } .-1 }
+auto [c] = a;	// { dg-error "has incomplete type" }
+		// { dg-warning "only available with" "" { target c++14_down } .-1 }
+extern int d[0];
+auto [e] { d };	// { dg-error "too many initializers for" }
+		// { dg-error "1 name provided for structured binding" "" { target *-*-* } .-1 }
+		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-2 }
+		// { dg-warning "only available with" "" { target c++14_down } .-3 }
+auto [f] = d;	// { dg-error "1 name provided for structured binding" }
+		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-1 }
+		// { dg-warning "only available with" "" { target c++14_down } .-2 }


	Jakub
Jason Merrill Feb. 4, 2021, 8:49 p.m. UTC | #3
On 2/4/21 12:47 PM, Jakub Jelinek wrote:
> On Thu, Feb 04, 2021 at 11:03:10AM -0500, Jason Merrill wrote:
>>> --- gcc/cp/decl.c.jj	2021-01-28 16:13:04.000000000 +0100
>>> +++ gcc/cp/decl.c	2021-02-03 15:57:16.980557836 +0100
>>> @@ -6837,7 +6837,8 @@ check_initializer (tree decl, tree init,
>>>        /* We will have already complained.  */
>>>        return NULL_TREE;
>>> -  if (TREE_CODE (type) == ARRAY_TYPE)
>>> +  if (TREE_CODE (type) == ARRAY_TYPE
>>> +      && (!DECL_DECOMPOSITION_P (decl) || DECL_DECOMP_BASE (decl)))
>>
>> This needs a comment.  Or handle this inside check_array_initializer, where
>> we could give a more helpful diagnostic?
> 
> So like this?
> 
> 2021-02-04  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/97878
> 	* decl.c (check_array_initializer): For structured bindings, require
> 	the array type to be complete.
> 
> 	* g++.dg/cpp1z/decomp54.C: New test.
> 
> --- gcc/cp/decl.c.jj	2021-02-03 17:13:58.567872007 +0100
> +++ gcc/cp/decl.c	2021-02-04 18:42:35.347374198 +0100
> @@ -6768,6 +6768,19 @@ check_array_initializer (tree decl, tree
>   {
>     tree element_type = TREE_TYPE (type);
>   
> +  /* Structured binding when initialized with an array type needs
> +     to have complete type.  */
> +  if (decl
> +      && DECL_DECOMPOSITION_P (decl)
> +      && !DECL_DECOMP_BASE (decl)
> +      && !COMPLETE_TYPE_P (type))
> +    {
> +      error_at (DECL_SOURCE_LOCATION (decl),
> +		"structured binding has incomplete type");

Let's print the type.  OK with that change.

> +      TREE_TYPE (decl) = error_mark_node;
> +      return true;
> +    }
> +
>     /* The array type itself need not be complete, because the
>        initializer may tell us how many elements are in the array.
>        But, the elements of the array must be complete.  */
> --- gcc/testsuite/g++.dg/cpp1z/decomp54.C.jj	2021-02-04 18:33:34.829393825 +0100
> +++ gcc/testsuite/g++.dg/cpp1z/decomp54.C	2021-02-04 18:33:34.829393825 +0100
> @@ -0,0 +1,17 @@
> +// PR c++/97878
> +// { dg-do compile { target c++11 } }
> +// { dg-options "" }
> +
> +extern int a[];
> +auto [b] { a };	// { dg-error "has incomplete type" }
> +		// { dg-warning "only available with" "" { target c++14_down } .-1 }
> +auto [c] = a;	// { dg-error "has incomplete type" }
> +		// { dg-warning "only available with" "" { target c++14_down } .-1 }
> +extern int d[0];
> +auto [e] { d };	// { dg-error "too many initializers for" }
> +		// { dg-error "1 name provided for structured binding" "" { target *-*-* } .-1 }
> +		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-2 }
> +		// { dg-warning "only available with" "" { target c++14_down } .-3 }
> +auto [f] = d;	// { dg-error "1 name provided for structured binding" }
> +		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-1 }
> +		// { dg-warning "only available with" "" { target c++14_down } .-2 }
> 
> 
> 	Jakub
>
diff mbox series

Patch

--- gcc/cp/decl.c.jj	2021-01-28 16:13:04.000000000 +0100
+++ gcc/cp/decl.c	2021-02-03 15:57:16.980557836 +0100
@@ -6837,7 +6837,8 @@  check_initializer (tree decl, tree init,
     /* We will have already complained.  */
     return NULL_TREE;
 
-  if (TREE_CODE (type) == ARRAY_TYPE)
+  if (TREE_CODE (type) == ARRAY_TYPE
+      && (!DECL_DECOMPOSITION_P (decl) || DECL_DECOMP_BASE (decl)))
     {
       if (check_array_initializer (decl, type, init))
 	return NULL_TREE;
--- gcc/testsuite/g++.dg/cpp1z/decomp54.C.jj	2021-02-03 16:21:27.991237734 +0100
+++ gcc/testsuite/g++.dg/cpp1z/decomp54.C	2021-02-03 16:21:10.984428831 +0100
@@ -0,0 +1,17 @@ 
+// PR c++/97878
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+extern int a[];
+auto [b] { a };	// { dg-error "has incomplete type" }
+		// { dg-warning "only available with" "" { target c++14_down } .-1 }
+auto [c] = a;	// { dg-error "has incomplete type" }
+		// { dg-warning "only available with" "" { target c++14_down } .-1 }
+extern int d[0];
+auto [e] { d };	// { dg-error "too many initializers for" }
+		// { dg-error "1 name provided for structured binding" "" { target *-*-* } .-1 }
+		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-2 }
+		// { dg-warning "only available with" "" { target c++14_down } .-3 }
+auto [f] = d;	// { dg-error "1 name provided for structured binding" }
+		// { dg-message "decomposes into 0 elements" "" { target *-*-* } .-1 }
+		// { dg-warning "only available with" "" { target c++14_down } .-2 }