Skip to content

Commit

Permalink
[libc++] Remove extension to support allocator<const T>
Browse files Browse the repository at this point in the history
This extension is a portability trap for users, since no other standard
library supports it. Furthermore, the Standard explicitly allows
implementations to reject std::allocator<cv T>, so allowing it is
really going against the current.

This was discovered in D120684: this extension required `const_cast`ing
in `__construct_range_forward`, a fishy bit of code that can be removed
if we don't support the extension anymore.

Differential Revision: https://reviews.llvm.org/D120996
  • Loading branch information
ldionne committed Mar 7, 2022
1 parent 932f628 commit bed3240
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 166 deletions.
5 changes: 5 additions & 0 deletions libcxx/docs/ReleaseNotes.rst
Expand Up @@ -69,6 +69,11 @@ API Changes
- The C++14 function ``std::quoted(const char*)`` is no longer supported in
C++03 or C++11 modes.

- libc++ no longer supports containers of ``const``-qualified element type,
such as ``vector<const T>`` and ``list<const T>``. This used to be supported
as an extension. Likewise, ``std::allocator<const T>`` is no longer supported.
If you were using ``vector<const T>``, replace it with ``vector<T>`` instead.

ABI Changes
-----------

Expand Down
90 changes: 1 addition & 89 deletions libcxx/include/__memory/allocator.h
Expand Up @@ -37,17 +37,6 @@ class _LIBCPP_TEMPLATE_VIS allocator<void>

template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
};

template <>
class _LIBCPP_TEMPLATE_VIS allocator<const void>
{
public:
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type;

template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
};
#endif

// This class provides a non-trivial default constructor to the class that derives from it
Expand Down Expand Up @@ -80,6 +69,7 @@ template <class _Tp>
class _LIBCPP_TEMPLATE_VIS allocator
: private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> >
{
static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
public:
typedef size_t size_type;
Expand Down Expand Up @@ -158,84 +148,6 @@ class _LIBCPP_TEMPLATE_VIS allocator
#endif
};

template <class _Tp>
class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
: private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> >
{
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef const _Tp value_type;
typedef true_type propagate_on_container_move_assignment;
typedef true_type is_always_equal;

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
allocator() _NOEXCEPT = default;

template <class _Up>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
allocator(const allocator<_Up>&) _NOEXCEPT { }

_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
const _Tp* allocate(size_t __n) {
if (__n > allocator_traits<allocator>::max_size(*this))
__throw_bad_array_new_length();
if (__libcpp_is_constant_evaluated()) {
return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
} else {
return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
}
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void deallocate(const _Tp* __p, size_t __n) {
if (__libcpp_is_constant_evaluated()) {
::operator delete(const_cast<_Tp*>(__p));
} else {
_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
}
}

// C++20 Removed members
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;

template <class _Up>
struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
typedef allocator<_Up> other;
};

_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
const_pointer address(const_reference __x) const _NOEXCEPT {
return _VSTD::addressof(__x);
}

_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
const _Tp* allocate(size_t __n, const void*) {
return allocate(__n);
}

_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
return size_type(~0) / sizeof(_Tp);
}

template <class _Up, class... _Args>
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
void construct(_Up* __p, _Args&&... __args) {
::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
}

_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
void destroy(pointer __p) {
__p->~_Tp();
}
#endif
};

template <class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/__memory/shared_ptr.h
Expand Up @@ -288,7 +288,7 @@ struct __shared_ptr_emplace
: __storage_(_VSTD::move(__a))
{
#if _LIBCPP_STD_VER > 17
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, remove_cv_t<_Tp>>::type;
_TpAlloc __tmp(*__get_alloc());
allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
#else
Expand All @@ -305,7 +305,7 @@ struct __shared_ptr_emplace
private:
virtual void __on_zero_shared() _NOEXCEPT {
#if _LIBCPP_STD_VER > 17
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, remove_cv_t<_Tp>>::type;
_TpAlloc __tmp(*__get_alloc());
allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
#else
Expand Down Expand Up @@ -960,7 +960,7 @@ template<class _Tp, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value>
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp> make_shared(_Args&& ...__args)
{
return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
return _VSTD::allocate_shared<_Tp>(allocator<typename remove_cv<_Tp>::type>(), _VSTD::forward<_Args>(__args)...);
}

template<class _Tp, class _Up>
Expand Down
6 changes: 2 additions & 4 deletions libcxx/include/memory
Expand Up @@ -895,19 +895,17 @@ void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr&
}

template <class _Alloc, class _Source, class _Dest,
class _RawSource = typename remove_const<_Source>::type,
class _RawDest = typename remove_const<_Dest>::type,
class =
typename enable_if<
is_trivially_copy_constructible<_Dest>::value &&
is_same<_RawSource, _RawDest>::value &&
is_same<typename remove_const<_Source>::type, _Dest>::value &&
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
>::type>
_LIBCPP_INLINE_VISIBILITY
void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
ptrdiff_t _Np = __end1 - __begin1;
if (_Np > 0) {
_VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
_VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Dest));
__begin2 += _Np;
}
}
Expand Down

This file was deleted.

Expand Up @@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//

// http://wg21.link/LWG2447 gives implementors freedom to reject volatile types in `std::allocator`.
// http://wg21.link/LWG2447 gives implementors freedom to reject const or volatile types in `std::allocator`.

#include <memory>

std::allocator<volatile int> A1; // expected-error@*:* {{std::allocator does not support volatile types}}
std::allocator<const volatile int> A2; // expected-error@*:* {{std::allocator does not support volatile types}}
std::allocator<const int> A2; // expected-error@*:* {{std::allocator does not support const types}}
Expand Up @@ -17,18 +17,10 @@
#include <type_traits>

typedef std::allocator<void> A1;
typedef std::allocator<void const> A2;
struct A3 : std::allocator<void> { };
struct A4 : std::allocator<void const> { };
struct A2 : std::allocator<void> { };

static_assert(std::is_trivially_default_constructible<A1>::value, "");
static_assert(std::is_trivial<A1>::value, "");

static_assert(std::is_trivially_default_constructible<A2>::value, "");
static_assert(std::is_trivial<A2>::value, "");

static_assert(std::is_trivially_default_constructible<A3>::value, "");
static_assert(std::is_trivial<A3>::value, "");

static_assert(std::is_trivially_default_constructible<A4>::value, "");
static_assert(std::is_trivial<A4>::value, "");
Expand Up @@ -202,9 +202,6 @@ void test()
test_not_const<std::array< volatile int, 1>>();
test_false <std::array<const volatile int, 1>>();
test_true <std::deque< int>>();
#ifdef _LIBCPP_VERSION
test_true <std::deque<const int>>();
#endif // _LIBCPP_VERSION
test_true <std::forward_list<int>>();
test_true <std::list<int>>();
test_true <std::vector<int>>();
Expand All @@ -223,9 +220,6 @@ void test()

// Container adaptors
test_true <std::stack< int>>();
#ifdef _LIBCPP_VERSION
test_true <std::stack<const int>>();
#endif // _LIBCPP_VERSION
test_true <std::queue<int>>();
test_true <std::priority_queue<int>>();

Expand Down
Expand Up @@ -37,12 +37,12 @@ TEST_CONSTEXPR_CXX20 bool test() {

int main(int, char**) {
test<char>();
test<char const>();
test<int>();
test<void>();

#if TEST_STD_VER > 17
static_assert(test<char>());
static_assert(test<char const>());
static_assert(test<int>());
static_assert(test<void>());
#endif
return 0;
Expand Down
Expand Up @@ -26,15 +26,9 @@ int main(int, char**)
{
test<int>();
test<void>();
#ifdef _LIBCPP_VERSION // extension
test<int const>();
#endif // _LIBCPP_VERSION

static_assert(test<int>());
static_assert(test<void>());
#ifdef _LIBCPP_VERSION // extension
static_assert(test<int const>());
#endif // _LIBCPP_VERSION

return 0;
}
Expand Up @@ -35,6 +35,5 @@ constexpr bool test()
int main(int, char**)
{
static_assert(test<double>()); // expected-error {{static_assert expression is not an integral constant expression}}
LIBCPP_STATIC_ASSERT(test<const double>()); // expected-error {{static_assert expression is not an integral constant expression}}
return 0;
}
Expand Up @@ -44,7 +44,6 @@ void test()
int main(int, char**)
{
test<double>();
LIBCPP_ONLY(test<const double>());

return 0;
}
Expand Up @@ -35,13 +35,6 @@ int main(int, char**) {
typedef std::allocator<char>::const_reference ConstReference; // expected-warning {{'const_reference' is deprecated}}
typedef std::allocator<char>::rebind<int>::other Rebind; // expected-warning {{'rebind<int>' is deprecated}}
}
{
typedef std::allocator<char const>::pointer Pointer; // expected-warning {{'pointer' is deprecated}}
typedef std::allocator<char const>::const_pointer ConstPointer; // expected-warning {{'const_pointer' is deprecated}}
typedef std::allocator<char const>::reference Reference; // expected-warning {{'reference' is deprecated}}
typedef std::allocator<char const>::const_reference ConstReference; // expected-warning {{'const_reference' is deprecated}}
typedef std::allocator<char const>::rebind<int>::other Rebind; // expected-warning {{'rebind<int>' is deprecated}}
}
{
typedef std::allocator<void>::pointer Pointer; // expected-warning {{'pointer' is deprecated}}
typedef std::allocator<void>::const_pointer ConstPointer; // expected-warning {{'const_pointer' is deprecated}}
Expand Down
Expand Up @@ -59,8 +59,5 @@ void test() {

int main(int, char**) {
test<char>();
#ifdef _LIBCPP_VERSION
test<char const>(); // extension
#endif
return 0;
}
Expand Up @@ -31,17 +31,16 @@
template <typename T>
void check()
{
typedef typename std::allocator<T>::pointer AP; // expected-error 3 {{no type named 'pointer'}}
typedef typename std::allocator<T>::const_pointer ACP; // expected-error 3 {{no type named 'const_pointer'}}
typedef typename std::allocator<T>::reference AR; // expected-error 3 {{no type named 'reference'}}
typedef typename std::allocator<T>::const_reference ACR; // expected-error 3 {{no type named 'const_reference'}}
typedef typename std::allocator<T>::template rebind<int>::other ARO; // expected-error 3 {{no member named 'rebind'}}
typedef typename std::allocator<T>::pointer AP; // expected-error 2 {{no type named 'pointer'}}
typedef typename std::allocator<T>::const_pointer ACP; // expected-error 2 {{no type named 'const_pointer'}}
typedef typename std::allocator<T>::reference AR; // expected-error 2 {{no type named 'reference'}}
typedef typename std::allocator<T>::const_reference ACR; // expected-error 2 {{no type named 'const_reference'}}
typedef typename std::allocator<T>::template rebind<int>::other ARO; // expected-error 2 {{no member named 'rebind'}}
}

int main(int, char**)
{
check<char>();
check<char const>();
check<void>();
return 0;
}
Expand Up @@ -82,7 +82,7 @@ constexpr bool test()
}

{
std::allocator<Counted const> a;
std::allocator<Counted> a;
Counted const* p = a.allocate(2);
int count = 0;
std::construct_at(p, count);
Expand All @@ -93,7 +93,7 @@ constexpr bool test()
assert(count == 1);
p->~Counted();
assert(count == 0);
a.deallocate(p, 2);
a.deallocate(const_cast<Counted*>(p), 2);
}

return true;
Expand Down

0 comments on commit bed3240

Please sign in to comment.