[libcxx-commits] [libcxx] [libc++][test] Fix issues found by MSVC's STL (PR #131787)

Stephan T. Lavavej via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 19 13:21:25 PDT 2025


https://github.com/StephanTLavavej updated https://github.com/llvm/llvm-project/pull/131787

>From a9df7df5ff8c9a6d793f00a42bebb91b014d85c1 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 17 Mar 2025 15:43:25 -0700
Subject: [PATCH 01/10] Fix tiny_size_allocator::rebind.

---
 libcxx/test/support/min_allocator.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/support/min_allocator.h b/libcxx/test/support/min_allocator.h
index 0be7ee0554ca9..24505bbb508d6 100644
--- a/libcxx/test/support/min_allocator.h
+++ b/libcxx/test/support/min_allocator.h
@@ -481,7 +481,7 @@ struct tiny_size_allocator {
 
   template <class U>
   struct rebind {
-    using other = tiny_size_allocator<MaxSize, T>;
+    using other = tiny_size_allocator<MaxSize, U>;
   };
 
   tiny_size_allocator() = default;

>From 25ae2d67ab3afe230a7fc71f11c0e380d7484620 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 17 Mar 2025 15:47:25 -0700
Subject: [PATCH 02/10] std::stable_partition requires bidirectional iterators.

---
 .../alg.partitions/stable_partition.pass.cpp                    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp
index ed55a41e83938..7e8100b7949e7 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp
@@ -289,7 +289,7 @@ TEST_CONSTEXPR_CXX26 void test() {
     vec[5]                             = 6;
     getGlobalMemCounter()->throw_after = 0;
     std::stable_partition(
-        forward_iterator<int*>(vec.data()), forward_iterator<int*>(vec.data() + vec.size()), [](int i) {
+        bidirectional_iterator<int*>(vec.data()), bidirectional_iterator<int*>(vec.data() + vec.size()), [](int i) {
           return i < 5;
         });
     assert(std::is_partitioned(vec.begin(), vec.end(), [](int i) { return i < 5; }));

>From fd4b90b31e6563db305900b9cffa9782495401fc Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 17 Mar 2025 21:23:27 -0700
Subject: [PATCH 03/10] Fix allocator type given to std::vector<bool>.

---
 .../test/std/containers/sequences/vector.bool/max_size.pass.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
index 86a163e0381ae..5ea67b56b9496 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
@@ -71,7 +71,7 @@ TEST_CONSTEXPR_CXX20 bool tests() {
   // Test with various allocators and different `size_type`s
   {
     test(std::vector<bool>());
-    test(std::vector<bool, std::allocator<int> >());
+    test(std::vector<bool, std::allocator<bool> >());
     test(std::vector<bool, min_allocator<bool> >());
     test(std::vector<bool, test_allocator<bool> >(test_allocator<bool>(1)));
     test(std::vector<bool, other_allocator<bool> >(other_allocator<bool>(5)));

>From 9f1f50a301e47b90a4ccd8863440a242dffae973 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 17 Mar 2025 21:27:38 -0700
Subject: [PATCH 04/10] Mark is_steady as [[maybe_unused]].

Fixes error: unused variable 'is_steady' [-Werror,-Wunused-const-variable]
---
 .../time.clock/time.clock.utc/types.compile.pass.cpp   | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libcxx/test/std/time/time.clock/time.clock.utc/types.compile.pass.cpp b/libcxx/test/std/time/time.clock/time.clock.utc/types.compile.pass.cpp
index 0322e9122e1cd..8cb3d78d97f52 100644
--- a/libcxx/test/std/time/time.clock/time.clock.utc/types.compile.pass.cpp
+++ b/libcxx/test/std/time/time.clock/time.clock.utc/types.compile.pass.cpp
@@ -36,11 +36,11 @@
 #include "test_macros.h"
 
 // class utc_clock
-using rep                = std::chrono::utc_clock::rep;
-using period             = std::chrono::utc_clock::period;
-using duration           = std::chrono::utc_clock::duration;
-using time_point         = std::chrono::utc_clock::time_point;
-constexpr bool is_steady = std::chrono::utc_clock::is_steady;
+using rep                                 = std::chrono::utc_clock::rep;
+using period                              = std::chrono::utc_clock::period;
+using duration                            = std::chrono::utc_clock::duration;
+using time_point                          = std::chrono::utc_clock::time_point;
+[[maybe_unused]] constexpr bool is_steady = std::chrono::utc_clock::is_steady;
 
 // Tests the values. Some of them are implementation-defined.
 LIBCPP_STATIC_ASSERT(std::same_as<rep, std::chrono::system_clock::rep>);

>From bf052af506d9a01d8bb44e09969377c4bb60684e Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Mon, 17 Mar 2025 21:45:18 -0700
Subject: [PATCH 05/10] Fix MSVC warning C4127 "conditional expression is
 constant".

---
 .../alg.modifying.operations/alg.rotate/rotate.pass.cpp         | 2 +-
 .../alg.modifying.operations/alg.swap/swap_ranges.pass.cpp      | 2 +-
 .../test/std/utilities/utility/utility.swap/swap_array.pass.cpp | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
index 448abdddbb4cb..e3d6b67cd316d 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
@@ -439,7 +439,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
   types::for_each(types::forward_iterator_list<int*>(), TestIter());
 
 #if TEST_STD_VER >= 11
-  if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED)
+  if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
     types::for_each(types::forward_iterator_list<std::unique_ptr<int>*>(), TestUniquePtr());
 #endif
 
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp
index 889794dff9fab..01cd33150e236 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp
@@ -144,7 +144,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
 
 #if TEST_STD_VER >= 11
   // We can't test unique_ptr in constant evaluation before C++23 as it's constexpr only since C++23.
-  if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED)
+  if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
     types::for_each(types::forward_iterator_list<std::unique_ptr<int>*>(), TestUniquePtr());
 #endif
 
diff --git a/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp b/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
index 3a6ed3856282f..6b77bdf3e699a 100644
--- a/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
+++ b/libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
@@ -137,7 +137,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
     static_assert(noexcept(std::swap(ma, ma)), "");
   }
 
-  if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED)
+  if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
     test_unique_ptr();
 #endif
 

>From 6ac6b6b0acf7a03c28e05cc6700baf5012121ed0 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Tue, 18 Mar 2025 03:39:52 -0700
Subject: [PATCH 06/10] Fix gcd() precondition violation for signed char.

---
 .../test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
index 456176a6444a9..067872a4d9a74 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
@@ -39,7 +39,7 @@ constexpr struct {
     {36, 18, 18},
     {25, 30, 5},
     {24, 16, 8},
-    {128, 100, 4}};
+    {124, 100, 4}};
 
 template <typename Input1, typename Input2, typename Output>
 constexpr bool test0(int in1, int in2, int out)

>From fb40f8c5bf3f950b55846ef8f772ffa3fd5ca6b5 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Tue, 18 Mar 2025 04:17:18 -0700
Subject: [PATCH 07/10] Split some REQUIRES and XFAIL lines.

---
 .../std/containers/sequences/array/assert.iterators.pass.cpp   | 3 ++-
 .../vector.modifiers/assert.push_back.invalidation.pass.cpp    | 3 ++-
 .../iostream.format/print.fun/no_file_description.pass.cpp     | 3 ++-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/std/containers/sequences/array/assert.iterators.pass.cpp b/libcxx/test/std/containers/sequences/array/assert.iterators.pass.cpp
index 21a763e71e18a..85e2df1eba981 100644
--- a/libcxx/test/std/containers/sequences/array/assert.iterators.pass.cpp
+++ b/libcxx/test/std/containers/sequences/array/assert.iterators.pass.cpp
@@ -6,7 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-// REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators-in-std-array
+// REQUIRES: has-unix-headers
+// REQUIRES: libcpp-has-abi-bounded-iterators-in-std-array
 // UNSUPPORTED: c++03
 // UNSUPPORTED: libcpp-hardening-mode=none
 // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
diff --git a/libcxx/test/std/containers/sequences/vector/vector.modifiers/assert.push_back.invalidation.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.modifiers/assert.push_back.invalidation.pass.cpp
index 193c00891da7f..4a899139d5f68 100644
--- a/libcxx/test/std/containers/sequences/vector/vector.modifiers/assert.push_back.invalidation.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector/vector.modifiers/assert.push_back.invalidation.pass.cpp
@@ -14,7 +14,8 @@
 // the insertion point remain valid but those at or after the insertion point,
 // including the past-the-end iterator, are invalidated.
 
-// REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators-in-vector
+// REQUIRES: has-unix-headers
+// REQUIRES: libcpp-has-abi-bounded-iterators-in-vector
 // UNSUPPORTED: c++03
 // UNSUPPORTED: libcpp-hardening-mode=none
 // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
diff --git a/libcxx/test/std/input.output/iostream.format/print.fun/no_file_description.pass.cpp b/libcxx/test/std/input.output/iostream.format/print.fun/no_file_description.pass.cpp
index 5561a1a8b3334..283a8fddb8e8f 100644
--- a/libcxx/test/std/input.output/iostream.format/print.fun/no_file_description.pass.cpp
+++ b/libcxx/test/std/input.output/iostream.format/print.fun/no_file_description.pass.cpp
@@ -10,7 +10,8 @@
 // UNSUPPORTED: no-filesystem
 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
 
-// XFAIL: msvc, target={{.+}}-windows-gnu
+// XFAIL: msvc
+// XFAIL: target={{.+}}-windows-gnu
 // XFAIL: availability-fp_to_chars-missing
 
 // fmemopen is available starting in Android M (API 23)

>From 4e3a77adfb20029825894429b46d8be10e82336c Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Tue, 18 Mar 2025 21:12:18 -0700
Subject: [PATCH 08/10] Fix x86 truncation warnings in sized_allocator.h.

sized_allocator.h(40): warning C4244: 'argument': conversion from 'unsigned __int64' to 'const size_t', possible loss of data
sized_allocator.h(43): warning C4244: 'argument': conversion from 'unsigned __int64' to 'const size_t', possible loss of data

std::allocator takes std::size_t, so we need to static_cast.
---
 libcxx/test/support/sized_allocator.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/support/sized_allocator.h b/libcxx/test/support/sized_allocator.h
index 8d52f5bf252c7..cbd4685ccf24d 100644
--- a/libcxx/test/support/sized_allocator.h
+++ b/libcxx/test/support/sized_allocator.h
@@ -37,10 +37,12 @@ class sized_allocator {
   TEST_CONSTEXPR_CXX20 T* allocate(size_type n) {
     if (n > max_size())
       TEST_THROW(std::bad_array_new_length());
-    return std::allocator<T>().allocate(n);
+    return std::allocator<T>().allocate(static_cast<std::size_t>(n));
   }
 
-  TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
+  TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT {
+    std::allocator<T>().deallocate(p, static_cast<std::size_t>(n));
+  }
 
   TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT {
     return std::numeric_limits<size_type>::max() / sizeof(value_type);

>From d8ad20d309d9f7d43b8a2f75dddb0c2fce1ef5b1 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Tue, 18 Mar 2025 21:13:30 -0700
Subject: [PATCH 09/10] Fix x86 truncation warning in offset_range.pass.cpp.

offset_range.pass.cpp(49): warning C4244: 'initializing': conversion from 'const _Ty' to 'size_t', possible loss of data

std::min() is returning std::streamoff, which was being unnecessarily narrowed to std::size_t.
---
 .../fstreams/ifstream.members/offset_range.pass.cpp             | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp b/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
index c6e07d045e145..9fc3cb4a94478 100644
--- a/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
+++ b/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
@@ -46,7 +46,7 @@ void test_tellg(std::streamoff total_size) {
     ofs.open(p, std::ios::out | std::ios::binary);
     assert(ofs.is_open());
     for (std::streamoff size = 0; size < total_size;) {
-      std::size_t n = std::min(static_cast<std::streamoff>(data.size()), total_size - size);
+      std::streamoff n = std::min(static_cast<std::streamoff>(data.size()), total_size - size);
       ofs.write(data.data(), n);
       size += n;
     }

>From 537e81943e406235f78456797795361f3397bb44 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Wed, 19 Mar 2025 12:56:55 -0700
Subject: [PATCH 10/10] Fix MSVC warning C4127 "conditional expression is
 constant" for an always-true branch.

This was very recently introduced by LLVM-129008 making `N` constexpr. As it's a local constant
just nine lines above, we don't need to test whether 100 is greater than 0.
---
 .../alg.sorting/alg.merge/inplace_merge_comp.pass.cpp     | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp
index 03824df2be2fe..45f79df1636f6 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp
@@ -161,11 +161,9 @@ TEST_CONSTEXPR_CXX26 bool test() {
     std::sort(ia, ia + M, indirect_less());
     std::sort(ia + M, ia + N, indirect_less());
     std::inplace_merge(ia, ia + M, ia + N, indirect_less());
-    if (N > 0) {
-      assert(*ia[0] == 0);
-      assert(*ia[N - 1] == N - 1);
-      assert(std::is_sorted(ia, ia + N, indirect_less()));
-    }
+    assert(*ia[0] == 0);
+    assert(*ia[N - 1] == N - 1);
+    assert(std::is_sorted(ia, ia + N, indirect_less()));
     delete[] ia;
   }
 #endif // TEST_STD_VER >= 11



More information about the libcxx-commits mailing list