[libcxx-commits] [PATCH] D141334: [libc++][test] Silence allocator conversion warnings

Casey Carter via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 9 16:57:07 PST 2023


CaseyCarter created this revision.
CaseyCarter added a reviewer: philnik.
CaseyCarter added a project: libc++.
Herald added a project: All.
CaseyCarter requested review of this revision.
Herald added a reviewer: libc++.

... by accepting `std::size_t` instead of `int` in `allocate` and `deallocate` functions.

Drive-by: To conform to the allocator requirements, the `Allocator` types in these tests need to have (1) converting constructors and (2) cross-specialization `==` that returns `true` at least for copies of the same allocator.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141334

Files:
  libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp
  libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp
  libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp


Index: libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
===================================================================
--- libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
+++ libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp
@@ -11,6 +11,7 @@
 // (bug report: https://llvm.org/PR58392)
 // Check that vector constructors don't leak memory when an operation inside the constructor throws an exception
 
+#include <cstddef>
 #include <type_traits>
 #include <vector>
 
@@ -22,15 +23,19 @@
   using value_type      = T;
   using is_always_equal = std::false_type;
 
+  template <class U>
+  Allocator(const Allocator<U>&) {}
+
   Allocator(bool should_throw = true) {
     if (should_throw)
       throw 0;
   }
 
-  T* allocate(int n) { return std::allocator<T>().allocate(n); }
-  void deallocate(T* ptr, int n) { std::allocator<T>().deallocate(ptr, n); }
+  T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+  void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
 
-  friend bool operator==(const Allocator&, const Allocator&) { return false; }
+  template <class U>
+  friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
 };
 
 struct ThrowingT {
Index: libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp
===================================================================
--- libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp
+++ libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp
@@ -11,6 +11,7 @@
 // (bug report: https://llvm.org/PR58392)
 // Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
 
+#include <cstddef>
 #include <type_traits>
 #include <vector>
 
@@ -30,10 +31,11 @@
       throw 0;
   }
 
-  T* allocate(int n) { return std::allocator<T>().allocate(n); }
-  void deallocate(T* ptr, int n) { std::allocator<T>().deallocate(ptr, n); }
+  T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+  void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
 
-  friend bool operator==(const Allocator&, const Allocator&) { return false; }
+  template <class U>
+  friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
 };
 
 template <class IterCat>
Index: libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp
===================================================================
--- libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp
+++ libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp
@@ -10,6 +10,7 @@
 
 // <vector>
 
+#include <cstddef>
 #include <vector>
 
 #include "test_macros.h"
@@ -22,8 +23,8 @@
     using value_type = T;
     MyAlloc() = default;
     template<class U> MyAlloc(const MyAlloc<U>&) {}
-    T *allocate(int n) { return std::allocator<T>().allocate(n); }
-    void deallocate(T *p, int n) { return std::allocator<T>().deallocate(p, n); }
+    T *allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+    void deallocate(T *p, std::size_t n) { return std::allocator<T>().deallocate(p, n); }
 };
 
 int main(int, char**)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141334.487605.patch
Type: text/x-patch
Size: 3312 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230110/7c5edbc1/attachment.bin>


More information about the libcxx-commits mailing list