]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: value-init vs zero-init in expand_aggr_init_1 [PR65816]
authorPatrick Palka <ppalka@redhat.com>
Tue, 1 Jun 2021 16:23:49 +0000 (12:23 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 1 Jun 2021 20:21:10 +0000 (16:21 -0400)
In the case of value-initializing an object of class type T,
[dcl.init.general]/8 says:

  - if T has either no default constructor ([class.default.ctor]) or
    a default constructor that is user-provided or deleted, then the
    object is default-initialized;
  - otherwise, the object is zero-initialized and ...  if T has a
    non-trivial default constructor, the object is default-initialized;

But when determining whether to first zero-initialize the object,
expand_aggr_init_1 incorrectly considers the user-providedness of _all_
constructors rather than only that of the _default_ constructors.  This
causes us to skip the zero-initialization step when the class type has a
defaulted default constructor alongside a user-defined constructor.

It seems the predicate type_has_non_user_provided_default_constructor
accurately captures the above rule for when to first perform a
zero-initialization during value-initialization, so this patch adjusts
expand_aggr_init_1 to use this predicate instead.

PR c++/65816

gcc/cp/ChangeLog:

* init.c (expand_aggr_init_1): Check
type_has_non_user_provided_default_constructor instead of
type_has_user_provided_constructor.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/constexpr-delegating3.C: New test.
* g++.dg/cpp0x/dc10.C: New test.
* g++.dg/cpp0x/initlist-base4.C: New test.
* g++.dg/cpp2a/constexpr-init22.C: New test.

libstdc++-v3/ChangeLog:

* testsuite/23_containers/deque/allocator/default_init.cc,
testsuite/23_containers/forward_list/allocator/default_init.cc,
testsuite/23_containers/list/allocator/default_init.cc,
testsuite/23_containers/map/allocator/default_init.cc,
testsuite/23_containers/set/allocator/default_init.cc,
testsuite/23_containers/vector/allocator/default_init.cc,
testsuite/23_containers/vector/bool/allocator/default_init.cc:
Remove xfail.

12 files changed:
gcc/cp/init.c
gcc/testsuite/g++.dg/cpp0x/constexpr-delegating3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/dc10.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/initlist-base4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-init22.C [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/deque/allocator/default_init.cc
libstdc++-v3/testsuite/23_containers/forward_list/allocator/default_init.cc
libstdc++-v3/testsuite/23_containers/list/allocator/default_init.cc
libstdc++-v3/testsuite/23_containers/map/allocator/default_init.cc
libstdc++-v3/testsuite/23_containers/set/allocator/default_init.cc
libstdc++-v3/testsuite/23_containers/vector/allocator/default_init.cc
libstdc++-v3/testsuite/23_containers/vector/bool/allocator/default_init.cc

index 04d495807efd3ff9445974f5bf6fd51dd8feef2a..b11232873000f99e26bfdba72304c3303f7ec5ef 100644 (file)
@@ -2078,9 +2078,9 @@ expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
      that's value-initialization.  */
   if (init == void_type_node)
     {
-      /* If the type has data but no user-provided ctor, we need to zero
+      /* If the type has data but no user-provided default ctor, we need to zero
         out the object.  */
-      if (!type_has_user_provided_constructor (type)
+      if (type_has_non_user_provided_default_constructor (type)
          && !is_really_empty_class (type, /*ignore_vptr*/true))
        {
          tree field_size = NULL_TREE;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-delegating3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-delegating3.C
new file mode 100644 (file)
index 0000000..2263ec8
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/65816
+// { dg-do compile { target c++11 } }
+
+struct test {
+  int m;
+  test() = default;
+  constexpr test(int) : test() {}
+};
+
+static_assert(test(0).m == 0, "");
diff --git a/gcc/testsuite/g++.dg/cpp0x/dc10.C b/gcc/testsuite/g++.dg/cpp0x/dc10.C
new file mode 100644 (file)
index 0000000..c008a17
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/65816
+// { dg-do run { target c++11 } }
+
+void* operator new(decltype(sizeof(int)), void* ptr) { return ptr; }
+
+struct test {
+  int i;
+  test() = default;
+  test(int) : test() {}
+};
+
+int main() {
+  alignas(test) unsigned char space[sizeof(test)];
+  for (auto& c : space) c = 0xff;
+
+  auto ptr = ::new(&space) test(42);
+  int& i = static_cast<test&>(*ptr).i;
+  if (i != 0) __builtin_abort();
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-base4.C b/gcc/testsuite/g++.dg/cpp0x/initlist-base4.C
new file mode 100644 (file)
index 0000000..4a02af9
--- /dev/null
@@ -0,0 +1,26 @@
+// PR c++/65816
+// { dg-do run { target c++11 } }
+
+void* operator new(decltype(sizeof(int)), void* ptr) { return ptr; }
+
+struct item { int i; };
+
+struct collector : item {
+  int j;
+  collector() = default;
+  collector(int) {}
+};
+
+struct tuple : collector {
+  tuple() : collector() {}
+};
+
+int main() {
+  alignas(tuple) unsigned char space[sizeof(tuple)];
+  for (auto& c : space) c = 0xff;
+
+  auto ptr = ::new(&space) tuple;
+  int& i = static_cast<tuple&>(*ptr).i;
+  int& j = static_cast<tuple&>(*ptr).j;
+  if (i != 0 || j != 0) __builtin_abort();
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-init22.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-init22.C
new file mode 100644 (file)
index 0000000..50e4a95
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/65816
+// { dg-do compile { target c++20 } }
+
+struct X {
+  int i;
+  X() = default;
+  constexpr X(int) { }
+};
+
+struct Y : X {
+  constexpr Y() : X() { }
+};
+
+static_assert(Y().i == 0);
index f51cc5b49b9eb162606f1ca073995e786c10a810..9428206f2ef537b715ecd16ba6250f5cb0c532c3 100644 (file)
@@ -17,7 +17,6 @@
 
 // { dg-do run { target c++11 } }
 // { dg-options "-O0" }
-// { dg-xfail-run-if "PR c++/65816" { *-*-* } }
 
 #include <deque>
 #include <testsuite_hooks.h>
index a60192641d49548bf7d12c5cef5d383341893dec..4298fa5ffa57817c53976a68d797d3d4736b4009 100644 (file)
@@ -17,7 +17,6 @@
 
 // { dg-do run { target c++11 } }
 // { dg-options "-O0" }
-// { dg-xfail-run-if "PR c++/65816" { *-*-* } }
 
 #include <forward_list>
 #include <testsuite_hooks.h>
index 62bfd78df188a39c0438c1ca484060b86e074ea7..03d8cfc89e864a9fcd674fec05cc64c9a59bb4ee 100644 (file)
@@ -17,7 +17,6 @@
 
 // { dg-do run { target c++11 } }
 // { dg-options "-O0" }
-// { dg-xfail-run-if "PR c++/65816" { *-*-* } }
 
 #include <list>
 #include <testsuite_hooks.h>
index bc9027703fc430832f85a4f6404ee6620c8cef20..6e3f36d60a4726435d1298af4b265e9d101d3fa0 100644 (file)
@@ -17,7 +17,6 @@
 
 // { dg-do run { target c++11 } }
 // { dg-options "-O0" }
-// { dg-xfail-run-if "PR c++/65816" { *-*-* } }
 
 #include <map>
 #include <testsuite_hooks.h>
index 8a99b491c256376a140004b37e37bc4a80b2b89e..b031cb81504bbe6fcd38a216d7affc5d841368e2 100644 (file)
@@ -17,7 +17,6 @@
 
 // { dg-do run { target c++11 } }
 // { dg-options "-O0" }
-// { dg-xfail-run-if "PR c++/65816" { *-*-* } }
 
 #include <set>
 #include <testsuite_hooks.h>
index c16d3b046e44dd0a0ba5378c1412579953f3c4b9..4673db71b6c7a23afa77abb984fb4a31499884e9 100644 (file)
@@ -17,7 +17,6 @@
 
 // { dg-do run { target c++11 } }
 // { dg-options "-O0" }
-// { dg-xfail-run-if "PR c++/65816" { *-*-* } }
 
 #include <vector>
 #include <testsuite_hooks.h>
index 4111e3ac3eb42719f2d1398d51535f0ed7a3832c..61cc7859d7454f8250e0cded506ad9f86493020b 100644 (file)
@@ -17,7 +17,6 @@
 
 // { dg-do run { target c++11 } }
 // { dg-options "-O0" }
-// { dg-xfail-run-if "PR c++/65816" { *-*-* } }
 
 #include <vector>
 #include <testsuite_hooks.h>