From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 9BBA3386F808; Fri, 19 Mar 2021 23:28:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9BBA3386F808 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-9464] c++: Fix ICE with structured binding initialized to incomplete array [PR97878] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 025a0ee3911c0866c69f841df24a558c7c8df0eb X-Git-Newrev: 8b7f2d3eae16dd629ae7ae40bb76f4bb0099f441 Message-Id: <20210319232845.9BBA3386F808@sourceware.org> Date: Fri, 19 Mar 2021 23:28:45 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 19 Mar 2021 23:28:45 -0000 https://gcc.gnu.org/g:8b7f2d3eae16dd629ae7ae40bb76f4bb0099f441 commit r10-9464-g8b7f2d3eae16dd629ae7ae40bb76f4bb0099f441 Author: Jakub Jelinek 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 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 }