From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id ABE893858C66; Fri, 1 Mar 2024 16:28:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ABE893858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709310493; bh=6v5FUBJy8PgSJjKW4tEwz2P5DmXPS4dzgiCW72SIt8w=; h=From:To:Subject:Date:From; b=vL+Uk/mMAYfAGaUUrw/jWyQ1DOFFpNE4tIA0FGc6yUhx2KJav9o5CCR56lTFd94oB oWSODFUIW/xMYmsHnlG8v4pyiydwiplZmmaJFOG+5+XFMf8mfCm9Tk2RGUxv2xTlcz PWBj47b4NgrHjqYsdvSfLd4QHWJ6hX9GsXmr0ov0= 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 r14-9258] c++: Fix up decltype of non-dependent structured binding decl in template [PR92687] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 4f82d5a95a244d0aa4f8b2541b47a21bce8a191b X-Git-Newrev: 867cbadb912ab75d0eaf919a3f992595e508482b Message-Id: <20240301162813.ABE893858C66@sourceware.org> Date: Fri, 1 Mar 2024 16:28:13 +0000 (GMT) List-Id: https://gcc.gnu.org/g:867cbadb912ab75d0eaf919a3f992595e508482b commit r14-9258-g867cbadb912ab75d0eaf919a3f992595e508482b Author: Jakub Jelinek Date: Fri Mar 1 16:59:08 2024 +0100 c++: Fix up decltype of non-dependent structured binding decl in template [PR92687] finish_decltype_type uses DECL_HAS_VALUE_EXPR_P (expr) check for DECL_DECOMPOSITION_P (expr) to determine if it is array/struct/vector/complex etc. subobject proxy case vs. structured binding using std::tuple_{size,element}. For non-templates or when templates are already instantiated, that works correctly, finalized DECL_DECOMPOSITION_P non-base vars indeed have DECL_VALUE_EXPR in the former case and don't have it in the latter. It works fine for dependent structured bindings as well, cp_finish_decomp in that case creates DECLTYPE_TYPE tree and defers the handling until instantiation. As the testcase shows, this doesn't work for the non-dependent structured binding case in templates, because DECL_HAS_VALUE_EXPR_P is set in that case always; cp_finish_decomp ends with: if (processing_template_decl) { for (unsigned int i = 0; i < count; i++) if (!DECL_HAS_VALUE_EXPR_P (v[i])) { tree a = build_nt (ARRAY_REF, decl, size_int (i), NULL_TREE, NULL_TREE); SET_DECL_VALUE_EXPR (v[i], a); DECL_HAS_VALUE_EXPR_P (v[i]) = 1; } } and those artificial ARRAY_REFs are used in various places during instantiation to find out what base the DECL_DECOMPOSITION_P VAR_DECLs have and their positions. The following patch fixes that by changing lookup_decomp_type, such that it doesn't ICE when called on a DECL_DECOMPOSITION_P var which isn't in a hash table, but returns NULL_TREE in that case, and for processing_template_decl asserts DECL_HAS_VALUE_EXPR_P is non-NULL and just calls lookup_decomp_type. If it returns non-NULL, it is a structured binding using tuple and its result is returned, otherwise it falls through to returning unlowered_expr_type (expr) because it is an array, structure etc. subobject proxy. For !processing_template_decl it keeps doing what it did before, DECL_HAS_VALUE_EXPR_P meaning it is an array/structure etc. subobject proxy, otherwise the tuple case. 2024-03-01 Jakub Jelinek PR c++/92687 * decl.cc (lookup_decomp_type): Return NULL_TREE if decomp_type_table doesn't have entry for V. * semantics.cc (finish_decltype_type): If ptds.saved, assert DECL_HAS_VALUE_EXPR_P is true and decide on tuple vs. non-tuple based on if lookup_decomp_type is NULL or not. * g++.dg/cpp1z/decomp59.C: New test. Diff: --- gcc/cp/decl.cc | 4 ++- gcc/cp/semantics.cc | 9 +++++ gcc/testsuite/g++.dg/cpp1z/decomp59.C | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 05e4600c7bb..993d7ef4d2b 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -9262,7 +9262,9 @@ static GTY((cache)) decl_tree_cache_map *decomp_type_table; tree lookup_decomp_type (tree v) { - return *decomp_type_table->get (v); + if (tree *slot = decomp_type_table->get (v)) + return *slot; + return NULL_TREE; } /* Mangle a decomposition declaration if needed. Arguments like diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 57840176863..adb1ba48d29 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11804,6 +11804,15 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p, access expression). */ if (DECL_DECOMPOSITION_P (expr)) { + if (ptds.saved) + { + gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (expr)); + /* DECL_HAS_VALUE_EXPR_P is always set if + processing_template_decl. If lookup_decomp_type + returns non-NULL, it is the tuple case. */ + if (tree ret = lookup_decomp_type (expr)) + return ret; + } if (DECL_HAS_VALUE_EXPR_P (expr)) /* Expr is an array or struct subobject proxy, handle bit-fields properly. */ diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp59.C b/gcc/testsuite/g++.dg/cpp1z/decomp59.C new file mode 100644 index 00000000000..52a72fa9c08 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp59.C @@ -0,0 +1,63 @@ +// PR c++/92687 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +namespace std { + template struct tuple_size; + template struct tuple_element; +} + +struct A { + int i; + template int& get() { return i; } +}; + +template<> struct std::tuple_size { static const int value = 2; }; +template struct std::tuple_element { using type = int; }; + +template +struct is_reference { + static const bool value = false; +}; + +template +struct is_reference +{ + static const bool value = true; +}; + +template +struct is_reference +{ + static const bool value = true; +}; + +template +void +foo () +{ + auto [x, y] = A {}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + static_assert (!is_reference::value, ""); +} + +void +bar () +{ + auto [x, y] = A {}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + static_assert (!is_reference::value, ""); +} + +template +void +baz () +{ + auto [x, y] = T {}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + static_assert (!is_reference::value, ""); +} + +void +qux () +{ + foo<0> (); + baz (); +}