Since <https://gcc.gnu.org/ml/gcc-patches/2015-02/msg00556.html> we
attempt to value-initialize in build_vec_init even when there's no
initializer but the type has a constexpr default constructor.  But
build_value_init doesn't work in templates, so I think let's avoid
this scenario; we'll go to the normal build_aggr_init path then.

Bootstrapped/regtested on x86_64-linux, ok for trunk and branches?

        PR c++/93676 - value-init crash in template.
        * init.c (build_vec_init): Don't perform value-init in a template.

        * g++.dg/cpp0x/nsdmi-template19.C: New test.
---
 gcc/cp/init.c                                 |  2 +-
 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index d480660445e..c9c0f03c58b 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -4520,7 +4520,7 @@ 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)
+  if (try_const && !init && !processing_template_decl)
     /* 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
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C 
b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C
new file mode 100644
index 00000000000..f3e2cb87fd6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C
@@ -0,0 +1,13 @@
+// PR c++/93676 - value-init crash in template.
+// { dg-do compile { target c++11 } }
+
+struct P {
+  int x = 0;
+};
+
+template<class T>
+struct S {
+  S() { new P[2][2]; }
+};
+
+S<int> s;

base-commit: 7a775242ea296849a34ce27de179eaaec411e880
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

Reply via email to