]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: broken direct-init with trailing array member [PR114439]
authorMarek Polacek <polacek@redhat.com>
Mon, 25 Mar 2024 19:32:20 +0000 (15:32 -0400)
committerMarek Polacek <polacek@redhat.com>
Mon, 25 Mar 2024 22:19:50 +0000 (18:19 -0400)
can_init_array_with_p is wrongly saying that the init for 's' here:

  struct S {
    int *list = arr;
    int arr[];
  };

  struct A {
    A() {}
    S s[2]{};
  };

is invalid.  But as process_init_constructor_array says, for "non-constant
initialization of trailing elements with no explicit initializers" we use
a VEC_INIT_EXPR wrapped in a TARGET_EXPR, built in process_init_constructor.

Unfortunately we didn't have a test for this scenario so I didn't
realize can_init_array_with_p must handle it.

PR c++/114439

gcc/cp/ChangeLog:

* init.cc (can_init_array_with_p): Return true for a VEC_INIT_EXPR
wrapped in a TARGET_EXPR.

gcc/testsuite/ChangeLog:

* g++.dg/init/array65.C: New test.

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

index dbd37d47cbf584329399e9fe3b9f1cb12206e21c..a93ce00800c47ce1659f427ef90a920463c898d4 100644 (file)
@@ -950,12 +950,16 @@ can_init_array_with_p (tree type, tree init)
      mem-initializers of a constructor.  */
   if (DECL_DEFAULTED_FN (current_function_decl))
     return true;
-  /* As an extension, we allow copying from a compound literal.  */
   if (TREE_CODE (init) == TARGET_EXPR)
     {
       init = TARGET_EXPR_INITIAL (init);
+      /* As an extension, we allow copying from a compound literal.  */
       if (TREE_CODE (init) == CONSTRUCTOR)
        return CONSTRUCTOR_C99_COMPOUND_LITERAL (init);
+      /* VEC_INIT_EXPR is used for non-constant initialization of trailing
+        elements with no explicit initializers.  */
+      else if (TREE_CODE (init) == VEC_INIT_EXPR)
+       return true;
     }
 
   return false;
diff --git a/gcc/testsuite/g++.dg/init/array65.C b/gcc/testsuite/g++.dg/init/array65.C
new file mode 100644 (file)
index 0000000..0b144f4
--- /dev/null
@@ -0,0 +1,38 @@
+// PR c++/114439
+// { dg-do compile { target c++11 } }
+
+struct S {
+  int *list = arr;
+  __extension__ int arr[];
+};
+
+struct R {
+  int *list = arr;
+  int arr[2];
+};
+
+struct A {
+  A() {}
+  S s[2]{};
+};
+
+struct A2 {
+  A2() {}
+  S s[2]{ {}, {} };
+};
+
+struct B {
+  B() {}
+  R r[2]{};
+};
+
+struct B2 {
+  B2() {}
+  R r[2]{ {}, {} };
+};
+
+struct S1 { S1(); };
+struct S2 {
+  S2() {}
+  S1 a[1] {};
+};