public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r10-9464] c++: Fix ICE with structured binding initialized to incomplete array [PR97878]
Date: Fri, 19 Mar 2021 23:28:45 +0000 (GMT)	[thread overview]
Message-ID: <20210319232845.9BBA3386F808@sourceware.org> (raw)

https://gcc.gnu.org/g:8b7f2d3eae16dd629ae7ae40bb76f4bb0099f441

commit r10-9464-g8b7f2d3eae16dd629ae7ae40bb76f4bb0099f441
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Feb 5 10:22:07 2021 +0100

    c++: Fix ICE with structured binding initialized to incomplete array [PR97878]
    
    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.
    
    2021-02-05  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.
    
    (cherry picked from commit b229baa75ce4627d1bd38f2d3dcd91af1a7071db)

Diff:
---
 gcc/cp/decl.c                         | 13 +++++++++++++
 gcc/testsuite/g++.dg/cpp1z/decomp54.C | 17 +++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index fab98d1ea1f..cd6cc124479 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -6580,6 +6580,19 @@ check_array_initializer (tree decl, tree type, tree init)
 {
   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 %qT", 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.  */
diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp54.C b/gcc/testsuite/g++.dg/cpp1z/decomp54.C
new file mode 100644
index 00000000000..1bee772d5ba
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/decomp54.C
@@ -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 }


                 reply	other threads:[~2021-03-19 23:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210319232845.9BBA3386F808@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).