]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::to_array for trivial-ish types [PR115522]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 18 Jun 2024 12:27:02 +0000 (13:27 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 21 Jun 2024 09:10:10 +0000 (10:10 +0100)
Due to PR c++/85723 the std::is_trivial trait is true for types with a
deleted default constructor, so the use of std::is_trivial in
std::to_array is not sufficient to ensure the type can be trivially
default constructed then filled using memcpy.

I also forgot that a type with a deleted assignment operator can still
be trivial, so we also need to check that it's assignable because the
is_constant_evaluated() path can't use memcpy.

Replace the uses of std::is_trivial with std::is_trivially_copyable
(needed for memcpy), std::is_trivially_default_constructible (needed so
that the default construction is valid and does no work) and
std::is_copy_assignable (needed for the constant evaluation case).

libstdc++-v3/ChangeLog:

PR libstdc++/115522
* include/std/array (to_array): Workaround the fact that
std::is_trivial is not sufficient to check that a type is
trivially default constructible and assignable.
* testsuite/23_containers/array/creation/115522.cc: New test.

libstdc++-v3/include/std/array
libstdc++-v3/testsuite/23_containers/array/creation/115522.cc [new file with mode: 0644]

index 39695471e245b307ab74538883e436c22737305f..8710bf75924bedec88e713dc28e1f6496f98a533 100644 (file)
@@ -431,7 +431,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static_assert(is_constructible_v<_Tp, _Tp&>);
       if constexpr (is_constructible_v<_Tp, _Tp&>)
        {
-         if constexpr (is_trivial_v<_Tp>)
+         if constexpr (is_trivially_copyable_v<_Tp>
+                         && is_trivially_default_constructible_v<_Tp>
+                         && is_copy_assignable_v<_Tp>)
            {
              array<remove_cv_t<_Tp>, _Nm> __arr;
              if (!__is_constant_evaluated() && _Nm != 0)
@@ -460,7 +462,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static_assert(is_move_constructible_v<_Tp>);
       if constexpr (is_move_constructible_v<_Tp>)
        {
-         if constexpr (is_trivial_v<_Tp>)
+         if constexpr (is_trivially_copyable_v<_Tp>
+                         && is_trivially_default_constructible_v<_Tp>
+                         && is_copy_assignable_v<_Tp>)
            {
              array<remove_cv_t<_Tp>, _Nm> __arr;
              if (!__is_constant_evaluated() && _Nm != 0)
diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/115522.cc b/libstdc++-v3/testsuite/23_containers/array/creation/115522.cc
new file mode 100644 (file)
index 0000000..37073e0
--- /dev/null
@@ -0,0 +1,33 @@
+// { dg-do compile { target c++20 } }
+
+// PR libstdc++/115522 std::to_array no longer works for struct which is
+// trivial but not default constructible
+
+#include <array>
+
+void
+test_deleted_ctor()
+{
+  struct S
+  {
+    S() = delete;
+    S(int) { }
+  };
+
+  S arr[1] = {{1}};
+  auto arr1 = std::to_array(arr);
+  auto arr2 = std::to_array(std::move(arr));
+}
+
+void
+test_deleted_assignment()
+{
+  struct S
+  {
+    void operator=(const S&) = delete;
+  };
+
+  S arr[1] = {};
+  auto a1 = std::to_array(arr);
+  auto a2 = std::to_array(std::move(arr));
+}