From acedaeadba2d2fb36697b476bce02522c9f7c9da Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 10 Oct 2024 09:20:10 -0700 Subject: [PATCH] Undo changes to '__move_range', fix test failure in 'insert' --- libcxx/include/vector | 57 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/libcxx/include/vector b/libcxx/include/vector index 787ee23c355786..ca5b774074ae43 100644 --- a/libcxx/include/vector +++ b/libcxx/include/vector @@ -1626,18 +1626,6 @@ vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointe pointer __old_last = this->__end_; difference_type __n = __old_last - __to; -#if _LIBCPP_STD_VER >= 26 - if ! consteval { - if constexpr (is_trivially_relocatable_v<_Tp> || is_nothrow_move_constructible_v<_Tp>) { - const size_t __numSpaces = __to - __from_s; - _ConstructTransaction __tx(*this, __numSpaces); - (void) relocate(std::__to_address(__from_s), std::__to_address(__from_e), std::__to_address(__to)); - __tx.__pos_ += __numSpaces; - return; - } - } -#endif - { pointer __i = __from_s + __n; _ConstructTransaction __tx(*this, __from_e - __i); @@ -1656,6 +1644,23 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x) if (__p == this->__end_) { __construct_one_at_end(__x); } else { +#if _LIBCPP_STD_VER >= 26 + if constexpr (is_trivially_relocatable_v<_Tp> || is_nothrow_move_constructible_v<_Tp>) { + // Make space by trivially relocating everything + _ConstructTransaction __tx(*this, 1); + (void) relocate(std::__to_address(__p), std::__to_address(this->__end_), std::__to_address(__p + 1)); + // construct the new element (not assign!) + const_pointer __xr = pointer_traits::pointer_to(__x); + if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end_), std::addressof(__x))) + ++__xr; + __alloc_traits::construct(this->__alloc(), std::__to_address(__p), *__xr); + ++__tx.__pos_; + // Need to fix up upon an exception! + // update all the invariants. + // return an iterator to the new entry + return __make_iter(__p); + } + #endif __move_range(__p, this->__end_, __p + 1); const_pointer __xr = pointer_traits::pointer_to(__x); if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end_), std::addressof(__x))) @@ -1679,6 +1684,20 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x) { if (__p == this->__end_) { __construct_one_at_end(std::move(__x)); } else { +#if _LIBCPP_STD_VER >= 26 + if constexpr (is_trivially_relocatable_v<_Tp> || is_nothrow_move_constructible_v<_Tp>) { + // Make space by trivially relocating everything + _ConstructTransaction __tx(*this, 1); + (void) relocate(std::__to_address(__p), std::__to_address(this->__end_), std::__to_address(__p + 1)); + // construct the new element (not assign!) + __alloc_traits::construct(this->__alloc(), std::__to_address(__p), std::forward(__x)); + ++__tx.__pos_; + // Need to fix up upon an exception! + // update all the invariants. + // return an iterator to the new entry + return __make_iter(__p); + } + #endif __move_range(__p, this->__end_, __p + 1); *__p = std::move(__x); } @@ -1701,6 +1720,20 @@ vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) { __construct_one_at_end(std::forward<_Args>(__args)...); } else { __temp_value __tmp(this->__alloc(), std::forward<_Args>(__args)...); +#if _LIBCPP_STD_VER >= 26 + if constexpr (is_trivially_relocatable_v<_Tp> || is_nothrow_move_constructible_v<_Tp>) { + // Make space by trivially relocating everything + _ConstructTransaction __tx(*this, 1); + (void) relocate(std::__to_address(__p), std::__to_address(this->__end_), std::__to_address(__p + 1)); + // construct the new element + __alloc_traits::construct(this->__alloc(), std::__to_address(__p), std::move(__tmp.get())); + ++__tx.__pos_; + // Need to fix up upon an exception! + // update all the invariants. + // return an iterator to the new entry + return __make_iter(__p); + } + #endif __move_range(__p, this->__end_, __p + 1); *__p = std::move(__tmp.get()); }