From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id B8CC938708AD; Tue, 2 May 2023 20:14:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8CC938708AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683058484; bh=auqYy8gBFVGQx069CGnTS2ck0H051xNVi6jksZboHKY=; h=From:To:Subject:Date:From; b=JjumtFN+OPFexGC3X/8QBYX0msvc86Y7S8gpNXAPwbllpiw65HHHNstu+7igKkCnG dB71iRZTZUjC2YJsSXpYX0Pc+QxNOuvkvFfIlTjH7nFs2HY4OYPrDjDOWfh7HzK+zA jcx40U8FQ6GEgpElpYvPp3LoIQzPy3SJl6eOnJcM= 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 r11-10712] c++: Handle structured bindings like anon unions in initializers [PR108474] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 77df8c7fc78b4ea7dc340f9f0fc239032ce0f9dd X-Git-Newrev: bc75dd303c8f8aa6191670ce3725d26e8011f5ac Message-Id: <20230502201444.B8CC938708AD@sourceware.org> Date: Tue, 2 May 2023 20:14:44 +0000 (GMT) List-Id: https://gcc.gnu.org/g:bc75dd303c8f8aa6191670ce3725d26e8011f5ac commit r11-10712-gbc75dd303c8f8aa6191670ce3725d26e8011f5ac Author: Jakub Jelinek Date: Tue Jan 24 11:28:00 2023 +0100 c++: Handle structured bindings like anon unions in initializers [PR108474] As reported by Andrew Pinski, structured bindings (with the exception of the ones using std::tuple_{size,element} and get which are really standalone variables in addition to the binding one) also use DECL_VALUE_EXPR and needs the same treatment in static initializers. On Sun, Jan 22, 2023 at 07:19:07PM -0500, Jason Merrill wrote: > Though, actually, why not instead fix expand_expr_real_1 (and staticp) to > look through DECL_VALUE_EXPR? Doing it when emitting the initializers seems to be too late to me, we in various spots try to put parts of the static var DECL_INITIAL expressions into the IL, or e.g. for varpool purposes remember which vars are referenced there. This patch moves it to record_reference, which is called from varpool_node::analyze and so about the same time as gimplification of the bodies which also replaces DECL_VALUE_EXPRs. 2023-01-24 Jakub Jelinek PR c++/108474 * cp-gimplify.c (cp_fold_r): Handle structured bindings vars like anon union artificial vars. * g++.dg/cpp1z/decomp57.C: New test. * g++.dg/cpp1z/decomp58.C: New test. (cherry picked from commit b84e21115700523b4d0ac44275443f7b9c670344) Diff: --- gcc/cp/cp-gimplify.c | 8 +++++-- gcc/testsuite/g++.dg/cpp1z/decomp57.C | 27 ++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1z/decomp58.C | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index a929e9f83d4..0cee22560b7 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -893,8 +893,12 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_) { case VAR_DECL: /* In initializers replace anon union artificial VAR_DECLs - with their DECL_VALUE_EXPRs, as nothing will do it later. */ - if (DECL_ANON_UNION_VAR_P (stmt) && !data->genericize) + with their DECL_VALUE_EXPRs, as nothing will do it later. + Ditto for structured bindings. */ + if (!data->genericize + && DECL_HAS_VALUE_EXPR_P (stmt) + && (DECL_ANON_UNION_VAR_P (stmt) + || (DECL_DECOMPOSITION_P (stmt) && DECL_DECOMP_BASE (stmt)))) { *stmt_p = stmt = unshare_expr (DECL_VALUE_EXPR (stmt)); break; diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp57.C b/gcc/testsuite/g++.dg/cpp1z/decomp57.C new file mode 100644 index 00000000000..923862e78d1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp57.C @@ -0,0 +1,27 @@ +// PR c++/108474 +// { dg-do link { target c++17 } } + +struct T { int i, j; }; +T h; +auto [i, j] = h; +int &r = i; +int s = i; +int *t = &i; + +void +foo (int **p, int *q) +{ + static int &u = i; + static int v = i; + static int *w = &i; + int &x = i; + int y = i; + int *z = &i; + *p = &i; + *q = i; +} + +int +main () +{ +} diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp58.C b/gcc/testsuite/g++.dg/cpp1z/decomp58.C new file mode 100644 index 00000000000..b2604373bde --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp58.C @@ -0,0 +1,39 @@ +// PR c++/108474 +// { dg-do link { target c++17 } } + +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; }; + +struct A a; +auto [i, j] = a; +int &r = i; +int s = i; +int *t = &i; + +void +foo (int **p, int *q) +{ + static int &u = i; + static int v = i; + static int *w = &i; + int &x = i; + int y = i; + int *z = &i; + *p = &i; + *q = i; +} + +int +main () +{ +}