[libcxx-commits] [libcxx] [libc++][test] Fix `size_type` issues with `MinSequenceContainer` and `min_allocator` (PR #126267)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Feb 7 09:15:43 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

`MinSequenceContainer::size` can be narrowing on 64-bit platforms, and MSVC complains about such implicit conversion. This PR changes the implicit conversion to explicit `static_cast`.

`min_allocator::allocate` and `min_allocator::deallocate` have `ptrdiff_t` as the parameter type, which seems weird, because the underlying `std::allocator`'s member functions take `size_t`. It seems better to use `size_t` consistently.

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


2 Files Affected:

- (modified) libcxx/test/support/MinSequenceContainer.h (+1-1) 
- (modified) libcxx/test/support/min_allocator.h (+2-2) 


``````````diff
diff --git a/libcxx/test/support/MinSequenceContainer.h b/libcxx/test/support/MinSequenceContainer.h
index d0e29ae40c400d3..7fee4dd0fbdc193 100644
--- a/libcxx/test/support/MinSequenceContainer.h
+++ b/libcxx/test/support/MinSequenceContainer.h
@@ -31,7 +31,7 @@ struct MinSequenceContainer {
   const_iterator cbegin() const { return const_iterator(data_.data()); }
   iterator end() { return begin() + size(); }
   const_iterator end() const { return begin() + size(); }
-  size_type size() const { return data_.size(); }
+  size_type size() const { return static_cast<size_type>(data_.size()); }
   bool empty() const { return data_.empty(); }
 
   void clear() { data_.clear(); }
diff --git a/libcxx/test/support/min_allocator.h b/libcxx/test/support/min_allocator.h
index 18f51f8072640d1..beee83feb95f1c0 100644
--- a/libcxx/test/support/min_allocator.h
+++ b/libcxx/test/support/min_allocator.h
@@ -394,12 +394,12 @@ class min_allocator
     template <class U>
     TEST_CONSTEXPR_CXX20 min_allocator(min_allocator<U>) {}
 
-    TEST_CONSTEXPR_CXX20 pointer allocate(std::ptrdiff_t n)
+    TEST_CONSTEXPR_CXX20 pointer allocate(std::size_t n)
     {
         return pointer(std::allocator<T>().allocate(n));
     }
 
-    TEST_CONSTEXPR_CXX20 void deallocate(pointer p, std::ptrdiff_t n)
+    TEST_CONSTEXPR_CXX20 void deallocate(pointer p, std::size_t n)
     {
         std::allocator<T>().deallocate(p.ptr_, n);
     }

``````````

</details>


https://github.com/llvm/llvm-project/pull/126267


More information about the libcxx-commits mailing list