[libcxx-commits] [PATCH] D135803: [libc++] Improve error message for invalid allocators

Nikolas Klauser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 12 11:26:38 PDT 2022


philnik created this revision.
philnik added reviewers: ldionne, Mordante, var-const, huixie90.
Herald added a project: All.
philnik requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135803

Files:
  libcxx/include/__memory/allocator_traits.h
  libcxx/test/libcxx/containers/sequences/vector/invalid_allocator.verify.cpp


Index: libcxx/test/libcxx/containers/sequences/vector/invalid_allocator.verify.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/containers/sequences/vector/invalid_allocator.verify.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Check that vector diagnoses an allocator which has to implement rebind with an appropriate error message
+
+#include <vector>
+
+class FooAllocator {
+ public:
+  using value_type = int;
+  FooAllocator() = default;
+
+  int* allocate(int num_objects);
+
+  void deallocate(int* ptr, int num_objects);
+
+  bool operator==(const FooAllocator&) const { return true; }
+  bool operator!=(const FooAllocator&) const { return false; }
+};
+
+void func() {
+  std::vector<int, FooAllocator> v; //expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}This allocator has to implement rebind}}
+}
Index: libcxx/include/__memory/allocator_traits.h
===================================================================
--- libcxx/include/__memory/allocator_traits.h
+++ libcxx/include/__memory/allocator_traits.h
@@ -156,7 +156,8 @@
 
 template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
 struct __allocator_traits_rebind {
-    using type _LIBCPP_NODEBUG = typename _Tp::template rebind<_Up>::other;
+  static_assert(__has_rebind_other<_Tp, _Up>::value, "This allocator has to implement rebind");
+  using type _LIBCPP_NODEBUG = typename _Tp::template rebind<_Up>::other;
 };
 template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
 struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true> {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135803.467209.patch
Type: text/x-patch
Size: 2013 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20221012/a138c04f/attachment-0001.bin>


More information about the libcxx-commits mailing list