From a02f26f6d8162f5fe400b75d6aa2cde4c43ae86a Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Mon, 9 Feb 2015 14:15:55 -0500 Subject: [PATCH] re PR c++/64899 (Illegal dynamic initialization) PR c++/64899 * init.c (build_vec_init): Handle default-initialized array with constexpr default constructor. From-SVN: r220544 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/init.c | 13 ++++++++++++- gcc/testsuite/g++.dg/cpp0x/constexpr-array10.C | 12 ++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-array10.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3ab1d77a362..37ce9098c10 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2015-02-06 Jason Merrill + + PR c++/64899 + * init.c (build_vec_init): Handle default-initialized array with + constexpr default constructor. + 2015-02-04 Jakub Jelinek PR c/64824 diff --git a/gcc/cp/init.c b/gcc/cp/init.c index e600472c72c..0274663c8f4 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3539,7 +3539,9 @@ build_vec_init (tree base, tree maxindex, tree init, /* Should we try to create a constant initializer? */ bool try_const = (TREE_CODE (atype) == ARRAY_TYPE && TREE_CONSTANT (maxindex) - && init && TREE_CODE (init) == CONSTRUCTOR + && (init ? TREE_CODE (init) == CONSTRUCTOR + : (type_has_constexpr_default_constructor + (inner_elt_type))) && (literal_type_p (inner_elt_type) || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type))); vec *const_vec = NULL; @@ -3677,6 +3679,12 @@ build_vec_init (tree base, tree maxindex, tree init, We do need to keep going if we're copying an array. */ + if (try_const && !init) + /* With a constexpr default constructor, which we checked for when + setting try_const above, default-initialization is equivalent to + value-initialization, and build_value_init gives us something more + friendly to maybe_constant_init. */ + explicit_value_init_p = true; if (from_array || ((type_build_ctor_call (type) || init || explicit_value_init_p) && ! (tree_fits_shwi_p (maxindex) @@ -3781,6 +3789,7 @@ build_vec_init (tree base, tree maxindex, tree init, if (try_const) { + /* FIXME refs to earlier elts */ tree e = maybe_constant_init (elt_init); if (reduced_constant_expression_p (e)) { @@ -3795,6 +3804,8 @@ build_vec_init (tree base, tree maxindex, tree init, saw_non_const = true; if (do_static_init) e = build_zero_init (TREE_TYPE (e), NULL_TREE, true); + else + e = NULL_TREE; } if (e) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array10.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array10.C new file mode 100644 index 00000000000..f7aaa4b943e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array10.C @@ -0,0 +1,12 @@ +// PR c++/64899 +// { dg-do compile { target c++11 } } + +struct S +{ + int i; + constexpr S (): i(42) {} +}; + +constexpr S sa[2]; +#define SA(X) static_assert((X),#X) +SA(sa[1].i == 42); -- 2.30.2