]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix value-init crash in template [PR93676]
authorMarek Polacek <polacek@redhat.com>
Wed, 4 Mar 2020 23:57:08 +0000 (18:57 -0500)
committerMarek Polacek <polacek@redhat.com>
Wed, 4 Mar 2020 23:57:08 +0000 (18:57 -0500)
PR c++/93676 - value-init crash in template.
* init.c (build_new_1): Don't call build_vec_init in a template.

* g++.dg/cpp0x/nsdmi-template19.C: New test.

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

index cb5211e5789a2889e23f98629440c65a053aa921..26d9a08d9efd09941b9fdbf083b3a11913b45d6a 100644 (file)
@@ -1,3 +1,11 @@
+2020-03-04  Marek Polacek  <polacek@redhat.com>
+
+       Backported from mainline
+       2020-02-26  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/93676 - value-init crash in template.
+       * init.c (build_new_1): Don't call build_vec_init in a template.
+
 2020-03-04  Jason Merrill  <jason@redhat.com>
            Marek Polacek  <polacek@redhat.com>
 
index 44ddc8cd14471d6cadfee33250ecb5d5c3f22960..fcc91fc91727e9f8a2fc910b746d0900c2e6fe32 100644 (file)
@@ -3448,13 +3448,17 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
          explicit_value_init_p = true;
        }
 
-      if (processing_template_decl && explicit_value_init_p)
+      if (processing_template_decl)
        {
+         /* Avoid an ICE when converting to a base in build_simple_base_path.
+            We'll throw this all away anyway, and build_new will create
+            a NEW_EXPR.  */
+         tree t = fold_convert (build_pointer_type (elt_type), data_addr);
          /* build_value_init doesn't work in templates, and we don't need
             the initializer anyway since we're going to throw it away and
             rebuild it at instantiation time, so just build up a single
             constructor call to get any appropriate diagnostics.  */
-         init_expr = cp_build_fold_indirect_ref (data_addr);
+         init_expr = cp_build_fold_indirect_ref (t);
          if (type_build_ctor_call (elt_type))
            init_expr = build_special_member_call (init_expr,
                                                   complete_ctor_identifier,
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C
new file mode 100644 (file)
index 0000000..f3e2cb8
--- /dev/null
@@ -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;