c++: array {}-init [PR105589]
authorJason Merrill <jason@redhat.com>
Fri, 13 May 2022 20:07:10 +0000 (16:07 -0400)
committerJason Merrill <jason@redhat.com>
Sun, 15 May 2022 16:29:04 +0000 (12:29 -0400)
My patch for 105191 made us use build_value_init more frequently from
build_vec_init_expr, but build_value_init doesn't like to be called to
initialize a class in a template.  That's caused trouble in the past, and
seems like a strange restriction, so let's fix it.

PR c++/105589
PR c++/105191
PR c++/92385

gcc/cp/ChangeLog:

* init.cc (build_value_init): Handle class in template.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/initlist-array16.C: New test.

gcc/cp/init.cc
gcc/testsuite/g++.dg/cpp0x/initlist-array16.C [new file with mode: 0644]

index 75ab965..773d26a 100644 (file)
@@ -343,10 +343,6 @@ build_value_init (tree type, tsubst_flags_t complain)
      A program that calls for default-initialization or
      value-initialization of an entity of reference type is ill-formed.  */
 
-  /* The AGGR_INIT_EXPR tweaking below breaks in templates.  */
-  gcc_assert (!processing_template_decl
-             || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
-
   if (CLASS_TYPE_P (type) && type_build_ctor_call (type))
     {
       tree ctor
@@ -354,6 +350,9 @@ build_value_init (tree type, tsubst_flags_t complain)
                                     NULL, type, LOOKUP_NORMAL, complain);
       if (ctor == error_mark_node || TREE_CONSTANT (ctor))
        return ctor;
+      if (processing_template_decl)
+       /* The AGGR_INIT_EXPR tweaking below breaks in templates.  */
+       return build_min (CAST_EXPR, type, NULL_TREE);
       tree fn = NULL_TREE;
       if (TREE_CODE (ctor) == CALL_EXPR)
        fn = get_callee_fndecl (ctor);
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-array16.C b/gcc/testsuite/g++.dg/cpp0x/initlist-array16.C
new file mode 100644 (file)
index 0000000..bb1d8d8
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/105589
+// { dg-do compile { target c++11 } }
+
+struct X { X(); };
+
+struct array { X m[2]; };
+
+template<class>
+void f() {
+  array w = array{};
+}