re PR c++/67550 (Initialization of local struct array with elements of global array...
authorJason Merrill <jason@redhat.com>
Thu, 17 Dec 2015 16:51:58 +0000 (11:51 -0500)
committerJason Merrill <jason@gcc.gnu.org>
Thu, 17 Dec 2015 16:51:58 +0000 (11:51 -0500)
PR c++/67550

* init.c (constant_value_1): Don't return a CONSTRUCTOR missing
non-constant elements.

From-SVN: r231777

gcc/cp/ChangeLog
gcc/cp/init.c
gcc/testsuite/g++.dg/init/aggr13.C [new file with mode: 0644]

index 73906f3cb12bb7993c39062562d34d9d083378d1..4c7dc78a19726d11c1cb0d1880d770237fa4fee7 100644 (file)
@@ -1,5 +1,9 @@
 2015-12-17  Jason Merrill  <jason@redhat.com>
 
+       PR c++/67550
+       * init.c (constant_value_1): Don't return a CONSTRUCTOR missing
+       non-constant elements.
+
        PR c++/67576
        PR c++/25466
        * rtti.c (build_typeid): Use save_expr, not stabilize_reference.
index a08f7d70b258e5b2b7019e249c153d86e92bcd2d..b7f10a10e67516ad3746477e6fbbc2b6c9da7c62 100644 (file)
@@ -2093,6 +2093,11 @@ constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p)
              && (TREE_CODE (init) == CONSTRUCTOR
                  || TREE_CODE (init) == STRING_CST)))
        break;
+      /* Don't return a CONSTRUCTOR for a variable with partial run-time
+        initialization, since it doesn't represent the entire value.  */
+      if (TREE_CODE (init) == CONSTRUCTOR
+         && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
+       break;
       decl = unshare_expr (init);
     }
   return decl;
diff --git a/gcc/testsuite/g++.dg/init/aggr13.C b/gcc/testsuite/g++.dg/init/aggr13.C
new file mode 100644 (file)
index 0000000..08248a6
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/67550
+// { dg-do run }
+
+struct S {
+  int x;
+  int y;
+};
+int foo() { return 1; }
+
+int main() {
+  S const data[] = {{0, foo()}};
+
+  S data2[] = {data[0]};
+
+  if (!data2[0].y)
+    __builtin_abort();
+}