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 <jakub@redhat.com>
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)
{
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;
--- /dev/null
+// 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 ()
+{
+}
--- /dev/null
+// PR c++/108474
+// { dg-do link { target c++17 } }
+
+namespace std {
+ template <typename T> struct tuple_size;
+ template <int, typename> struct tuple_element;
+}
+
+struct A {
+ int i;
+ template <int I> int& get() { return i; }
+};
+
+template <> struct std::tuple_size <A> { static const int value = 2; };
+template <int I> struct std::tuple_element <I, A> { 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 ()
+{
+}