From 3e07e7a6a7f34f0ec2f1a3e50ebc52b77de11a30 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 23 Mar 2021 10:23:42 +0100 Subject: c++: Diagnose references to void in structured bindings [PR99650] We ICE on the following testcase, because std::tuple_element<...,...>::type is void and for structured bindings we therefore need to create void & or void && which is invalid. We created such REFERENCE_TYPE and later ICEd in the middle-end. The following patch fixes it by diagnosing that. 2021-03-23 Jakub Jelinek PR c++/99650 * decl.c (cp_finish_decomp): Diagnose void initializers when using tuple_element and get. * g++.dg/cpp1z/decomp55.C: New test. --- gcc/cp/decl.c | 6 ++++++ gcc/testsuite/g++.dg/cpp1z/decomp55.C | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp55.C (limited to 'gcc') diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 8e8f37d..b1d8e44 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -8629,6 +8629,12 @@ cp_finish_decomp (tree decl, tree first, unsigned int count) : get_tuple_element_type (type, i)); input_location = sloc; + if (VOID_TYPE_P (eltype)) + { + error ("%::type%> is %", + i, type); + eltype = error_mark_node; + } if (init == error_mark_node || eltype == error_mark_node) { inform (dloc, "in initialization of structured binding " diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp55.C b/gcc/testsuite/g++.dg/cpp1z/decomp55.C new file mode 100644 index 0000000..bb4bf75 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp55.C @@ -0,0 +1,19 @@ +// PR c++/99650 +// { dg-do compile { target c++17 } } + +namespace std { + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int i; + template void get() { } +}; + +template<> struct std::tuple_size { static const int value = 2; }; +template struct std::tuple_element { using type = void; }; + +A a = { 42 }; +auto [ x, y ] = a; // { dg-error ".std::tuple_element<0, A>::type. is .void." } +// { dg-message "in initialization of structured binding variable 'x'" "" { target *-*-* } .-1 } -- cgit v1.1