]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: ICE with structured bindings and m-d array [PR102594]
authorMarek Polacek <polacek@redhat.com>
Thu, 5 Sep 2024 20:45:32 +0000 (16:45 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 9 Jan 2025 19:46:34 +0000 (14:46 -0500)
We ICE in decay_conversion with this test:

  struct S {
    S() {}
  };
  S arr[1][1];
  auto [m](arr3);

But not when the last line is:

  auto [n] = arr3;

Therefore the difference is between copy- and direct-init.  In
particular, in build_vec_init we have:

  if (direct_init)
    from = build_tree_list (NULL_TREE, from);

and then we call build_vec_init again with init==from.  Then
decay_conversion gets the TREE_LIST and it crashes.

build_aggr_init has:

              /* Wrap the initializer in a CONSTRUCTOR so that build_vec_init
                 recognizes it as direct-initialization.  */
              init = build_constructor_single (init_list_type_node,
                                               NULL_TREE, init);
              CONSTRUCTOR_IS_DIRECT_INIT (init) = true;

so I propose to do the same in build_vec_init.

PR c++/102594

gcc/cp/ChangeLog:

* init.cc (build_vec_init): Build up a CONSTRUCTOR to signal
direct-initialization rather than a TREE_LIST.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/decomp61.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
(cherry picked from commit 96e0370f4daef29b918aafcff68c7f5e4ef397fd)

gcc/cp/init.cc
gcc/testsuite/g++.dg/cpp1z/decomp61.C [new file with mode: 0644]

index 5a994378451b09ff00d6f8a5924944ea0974bce2..f9b651be460506f9f4c5a8ac3c49fc154fa317f3 100644 (file)
@@ -4919,7 +4919,13 @@ build_vec_init (tree base, tree maxindex, tree init,
              if (xvalue)
                from = move (from);
              if (direct_init)
-               from = build_tree_list (NULL_TREE, from);
+               {
+                 /* Wrap the initializer in a CONSTRUCTOR so that
+                    build_vec_init recognizes it as direct-initialization.  */
+                 from = build_constructor_single (init_list_type_node,
+                                                  NULL_TREE, from);
+                 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
+               }
            }
          else
            from = NULL_TREE;
diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp61.C b/gcc/testsuite/g++.dg/cpp1z/decomp61.C
new file mode 100644 (file)
index 0000000..ad0a20c
--- /dev/null
@@ -0,0 +1,28 @@
+// PR c++/102594
+// { dg-do compile { target c++17 } }
+
+struct S {
+  S() {}
+};
+S arr1[2];
+S arr2[2][1];
+S arr3[1][1];
+auto [m](arr3);
+auto [n] = arr3;
+
+struct X {
+  int i;
+};
+
+void
+g (X x)
+{
+  auto [a, b](arr2);
+  auto [c, d] = arr2;
+  auto [e, f] = (arr2);
+  auto [i, j](arr1);
+  auto [k, l] = arr1;
+  auto [m, n] = (arr1);
+  auto [z] = x;
+  auto [y](x);
+}