Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++][test] Fix more MSVC warnings #74256

Merged
merged 3 commits into from
Dec 6, 2023

Conversation

StephanTLavavej
Copy link
Member

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 🐿️ - 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.

…erator::operator ->' is recursive if applied using infix notation".
…4' to 'unsigned int', possible loss of data".

An allocator's deallocate() should be able to take its size type.
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner December 3, 2023 22:10
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 3, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 3, 2023

@llvm/pr-subscribers-libcxx

Author: Stephan T. Lavavej (StephanTLavavej)

Changes

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

  • Fix MSVC "warning C4285: return type for 'WithNonCopyableIterator::iterator::operator -&gt;' is recursive if applied using infix notation".
    • This warning is valid - the return type is super squirrelly 🐿️ - 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&lt;int&gt;::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&lt;int&gt;::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.

Full diff: https://github.com/llvm/llvm-project/pull/74256.diff

4 Files Affected:

  • (modified) libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/member_swap_noexcept.pass.cpp (+1-1)
  • (modified) libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/nonmember_swap_noexcept.pass.cpp (+1-1)
  • (modified) libcxx/test/std/ranges/range.adaptors/range.filter/iterator/arrow.pass.cpp (+1-1)
  • (modified) libcxx/test/std/ranges/range.factories/range.repeat.view/size.pass.cpp (+3-2)
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>
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);
 
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;

@ldionne ldionne merged commit bcb917e into llvm:main Dec 6, 2023
42 checks passed
@StephanTLavavej StephanTLavavej deleted the stl-17-more-simple-warnings branch December 6, 2023 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants