]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Prevent unwanted ADL in std::to_array [PR111512]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 21 Sep 2023 08:14:57 +0000 (09:14 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 26 Sep 2023 20:11:18 +0000 (21:11 +0100)
Qualify the calls to the __to_array helper to prevent ADL, so we don't
try to complete associated classes.

libstdc++-v3/ChangeLog:

PR libstdc++/111511
PR c++/111512
* include/std/array (to_array): Qualify calls to __to_array.
* testsuite/23_containers/array/creation/111512.cc: New test.

(cherry picked from commit 77cf3773021b0a20d89623e09d620747a05588ec)

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

index 70280c1beeb45d2fa8bef7f9aa26b99e05b67918..97cca454ef9bf70d1bf0f84f369d33c75e9c2fb3 100644 (file)
@@ -436,7 +436,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static_assert(!is_array_v<_Tp>);
       static_assert(is_constructible_v<_Tp, _Tp&>);
       if constexpr (is_constructible_v<_Tp, _Tp&>)
-       return __to_array(__a, make_index_sequence<_Nm>{});
+       return std::__to_array(__a, make_index_sequence<_Nm>{});
       __builtin_unreachable(); // FIXME: see PR c++/91388
     }
 
@@ -449,7 +449,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static_assert(!is_array_v<_Tp>);
       static_assert(is_move_constructible_v<_Tp>);
       if constexpr (is_move_constructible_v<_Tp>)
-       return __to_array<1>(__a, make_index_sequence<_Nm>{});
+       return std::__to_array<1>(__a, make_index_sequence<_Nm>{});
       __builtin_unreachable(); // FIXME: see PR c++/91388
     }
 #endif // C++20
diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/111512.cc b/libstdc++-v3/testsuite/23_containers/array/creation/111512.cc
new file mode 100644 (file)
index 0000000..f510480
--- /dev/null
@@ -0,0 +1,25 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do compile { target c++20 } }
+
+// Bug libstdc++/111511 - Incorrect ADL in std::to_array in GCC 11/12/13
+// Bug c++/111512 - GCC's __builtin_memcpy can trigger ADL
+
+#include <array>
+#include <utility>
+
+struct incomplete;
+
+template<class T>
+struct holder {
+    T t; // { dg-bogus "'holder<T>::t' has incomplete type" }
+};
+
+// A complete type that cannot be used as an associated type for ADL.
+using adl_bomb = holder<incomplete>*;
+
+int main()
+{
+    adl_bomb a[1]{};
+    (void) std::to_array(a);
+    (void) std::to_array(std::move(a));
+}