From: Jason Merrill Date: Thu, 17 Dec 2015 16:51:58 +0000 (-0500) Subject: re PR c++/67550 (Initialization of local struct array with elements of global array... X-Git-Tag: basepoints/gcc-7~2005 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=62f9ab0d432554c33c8d9c449ebcae73b2789812;p=thirdparty%2Fgcc.git re PR c++/67550 (Initialization of local struct array with elements of global array yields zeros instead of initializer values) PR c++/67550 * init.c (constant_value_1): Don't return a CONSTRUCTOR missing non-constant elements. From-SVN: r231777 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 73906f3cb12b..4c7dc78a1972 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2015-12-17 Jason Merrill + 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. diff --git a/gcc/cp/init.c b/gcc/cp/init.c index a08f7d70b258..b7f10a10e675 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -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 index 000000000000..08248a6df946 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/aggr13.C @@ -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(); +}