[libcxx-commits] [libcxx] [libc++][test] Fix more MSVC warnings (PR #74256)

Stephan T. Lavavej via libcxx-commits libcxx-commits at lists.llvm.org
Sun Dec 3 14:11:01 PST 2023


https://github.com/StephanTLavavej created https://github.com/llvm/llvm-project/pull/74256

Found while running libc++'s test suite with MSVC's STL.

* Fix MSVC "warning C4285: return type for '`WithNonCopyableIterator::iterator::operator ->`' is recursive if applied using infix notation".
  + This warning is valid - the return type is super squirrelly :chipmunk: - even though the operator is never defined. This test doesn't care about this return type, and it appears to have been a simple typo. Changing it to `XYPoint*`, like `WithArrowOperator` immediately above, fixes the warning.
* Fix MSVC "warning C4389: '`==`': signed/unsigned mismatch".
  + This warning is valid-ish. MSVC is seeing a `repeat_view`'s `size()` being compared with the `int` return value of a function, and a negative value would undergo a value-modifying conversion, so it warns. This happens because MSVC is *exceptionally* lazy about `constexpr` function calls, so it's calling `std::numeric_limits<int>::max()` at runtime and doesn't realize that it's a positive constant. By extracting this into a `constexpr` variable, MSVC is satisfied that `int_max` will undergo a value-preserving conversion. This costs an extra line but slightly reduces the repetition of `std::numeric_limits<int>::max()` so I hope it's acceptable.
* Fix MSVC "warning C4244: 'argument': conversion from '`unsigned __int64`' to '`unsigned int`', possible loss of data".
  + An allocator's `deallocate()` should be able to take its size type. In these tests, MSVC is instantiating enough of the allocator machinery to emit this warning. These test allocators weren't intentionally trying to make `deallocate()` have a weird parameter type - these tests are interested in POCS etc. and the `allocate()`/`deallocate()` are just stubs.


>From 30710a27fb6ff41b060f6f4282be45b843a97f3e Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Sat, 2 Dec 2023 16:06:59 -0800
Subject: [PATCH 1/3] Fix MSVC "warning C4285: return type for
 'WithNonCopyableIterator::iterator::operator ->' is recursive if applied
 using infix notation".

---
 .../ranges/range.adaptors/range.filter/iterator/arrow.pass.cpp  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/ranges/range.adaptors/range.filter/iterator/arrow.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.filter/iterator/arrow.pass.cpp
index f139226b875f0..0c02cfdb76ad3 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.filter/iterator/arrow.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.filter/iterator/arrow.pass.cpp
@@ -59,7 +59,7 @@ struct WithNonCopyableIterator : std::ranges::view_base {
     iterator(iterator&&);
     iterator& operator=(iterator&&);
     XYPoint& operator*() const;
-    iterator operator->() const;
+    XYPoint* operator->() const;
     iterator& operator++();
     iterator operator++(int);
 

>From 0fbde104a480d2881e7acabc1370460ba3070c83 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Sat, 2 Dec 2023 19:24:40 -0800
Subject: [PATCH 2/3] Fix MSVC "warning C4389: '==': signed/unsigned mismatch".

---
 .../ranges/range.factories/range.repeat.view/size.pass.cpp   | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/std/ranges/range.factories/range.repeat.view/size.pass.cpp b/libcxx/test/std/ranges/range.factories/range.repeat.view/size.pass.cpp
index 72531b059aa24..6f24b3b9bf75a 100644
--- a/libcxx/test/std/ranges/range.factories/range.repeat.view/size.pass.cpp
+++ b/libcxx/test/std/ranges/range.factories/range.repeat.view/size.pass.cpp
@@ -31,8 +31,9 @@ constexpr bool test() {
   }
 
   {
-    std::ranges::repeat_view<int, int> rv(10, std::numeric_limits<int>::max());
-    assert(rv.size() == std::numeric_limits<int>::max());
+    constexpr int int_max = std::numeric_limits<int>::max();
+    std::ranges::repeat_view<int, int> rv(10, int_max);
+    assert(rv.size() == int_max);
   }
 
   return true;

>From fb44c61a78a0b272b0368241d959961e3acef4e3 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Sun, 3 Dec 2023 00:12:45 -0800
Subject: [PATCH 3/3] Fix MSVC "warning C4244: 'argument': conversion from
 'unsigned __int64' to 'unsigned int', possible loss of data".

An allocator's deallocate() should be able to take its size type.
---
 .../stringbuf/stringbuf.assign/member_swap_noexcept.pass.cpp    | 2 +-
 .../stringbuf/stringbuf.assign/nonmember_swap_noexcept.pass.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/member_swap_noexcept.pass.cpp b/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/member_swap_noexcept.pass.cpp
index cdb09df7c7a9a..0a0128e44658f 100644
--- a/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/member_swap_noexcept.pass.cpp
+++ b/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/member_swap_noexcept.pass.cpp
@@ -27,7 +27,7 @@ struct test_alloc {
   using value_type = T;
 
   [[nodiscard]] constexpr T* allocate(std::size_t) { return nullptr; }
-  void deallocate(void*, unsigned) {}
+  void deallocate(void*, std::size_t) {}
 };
 
 template <class T>
diff --git a/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/nonmember_swap_noexcept.pass.cpp b/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/nonmember_swap_noexcept.pass.cpp
index fdefc5ebe9af0..4f41e3a4d716a 100644
--- a/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/nonmember_swap_noexcept.pass.cpp
+++ b/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/nonmember_swap_noexcept.pass.cpp
@@ -26,7 +26,7 @@ struct test_alloc {
   using value_type = T;
 
   [[nodiscard]] constexpr T* allocate(std::size_t) { return nullptr; }
-  void deallocate(void*, unsigned) {}
+  void deallocate(void*, std::size_t) {}
 };
 
 template <class T>



More information about the libcxx-commits mailing list