From 610fc67809b52442079f1ec2fe357006dcc69d76 Mon Sep 17 00:00:00 2001 From: Nirav Dave Date: Tue, 6 Feb 2018 03:03:37 +0000 Subject: [PATCH] Revert "[libc++] Fix PR35491 - std::array of zero-size doesn't work with non-default constructible types." Revert "Fix initialization of array with GCC." Revert "Make array non-CopyAssignable and make swap and fill ill-formed." This reverts commit r324182, r324185, and r324194 which were causing issues with zero-length std::arrays. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@324309 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/array | 87 ++++------------- .../array/array.cons/default.pass.cpp | 17 ---- .../array/array.cons/implicit_copy.pass.cpp | 93 ------------------- .../sequences/array/array.data/data.pass.cpp | 18 ---- .../array/array.data/data_const.pass.cpp | 10 -- .../sequences/array/array.fill/fill.fail.cpp | 29 ------ .../sequences/array/array.swap/swap.fail.cpp | 30 ------ .../containers/sequences/array/begin.pass.cpp | 9 -- 8 files changed, 17 insertions(+), 276 deletions(-) delete mode 100644 test/std/containers/sequences/array/array.cons/implicit_copy.pass.cpp delete mode 100644 test/std/containers/sequences/array/array.fill/fill.fail.cpp delete mode 100644 test/std/containers/sequences/array/array.swap/swap.fail.cpp diff --git a/include/array b/include/array index 068ab506a..4eb2fe6fc 100644 --- a/include/array +++ b/include/array @@ -117,57 +117,6 @@ template const T&& get(const array&&) noexce _LIBCPP_BEGIN_NAMESPACE_STD -template -struct __array_traits { - typedef _Tp _StorageT[_Size]; - - _LIBCPP_INLINE_VISIBILITY - static _LIBCPP_CONSTEXPR_AFTER_CXX14 typename remove_const<_Tp>::type* - __data(typename remove_const<_StorageT>::type& __store) { - return __store; - } - - _LIBCPP_INLINE_VISIBILITY - static _LIBCPP_CONSTEXPR_AFTER_CXX14 _Tp const* __data(const _StorageT& __store) { - return __store; - } - - _LIBCPP_INLINE_VISIBILITY - static void __swap(_StorageT& __lhs, _StorageT& __rhs) { - std::swap_ranges(__lhs, __lhs + _Size, __rhs); - } - - _LIBCPP_INLINE_VISIBILITY - static void __fill(_StorageT& __arr, _Tp const& __val) { - _VSTD::fill_n(__arr, _Size, __val); - } -}; - -template -struct __array_traits<_Tp, 0> { - typedef typename aligned_storage::value>::type - _NonConstStorageT[1]; - typedef typename conditional::value, const _NonConstStorageT, - _NonConstStorageT>::type _StorageT; - typedef typename remove_const<_Tp>::type _NonConstTp; - - _LIBCPP_INLINE_VISIBILITY - static _NonConstTp* __data(_NonConstStorageT &__store) { - return reinterpret_cast<_NonConstTp*>(__store); - } - - _LIBCPP_INLINE_VISIBILITY - static const _Tp* __data(const _StorageT &__store) { - return reinterpret_cast(__store); - } - - _LIBCPP_INLINE_VISIBILITY - static void __swap(_StorageT&, _StorageT&) {} - - _LIBCPP_INLINE_VISIBILITY - static void __fill(_StorageT&, _Tp const&) {} -}; - template struct _LIBCPP_TEMPLATE_VIS array { @@ -185,33 +134,31 @@ struct _LIBCPP_TEMPLATE_VIS array typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; - typedef __array_traits<_Tp, _Size> _Traits; - typename _Traits::_StorageT __elems_; + value_type __elems_[_Size > 0 ? _Size : 1]; // No explicit construct/copy/destroy for aggregate type - _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) { - static_assert(_Size != 0 || !is_const<_Tp>::value, - "cannot fill zero-sized array of type 'const T'"); - _Traits::__fill(__elems_, __u); - } + _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) + {_VSTD::fill_n(__elems_, _Size, __u);} + _LIBCPP_INLINE_VISIBILITY + void swap(array& __a) _NOEXCEPT_(_Size == 0 || __is_nothrow_swappable<_Tp>::value) + { __swap_dispatch((std::integral_constant()), __a); } _LIBCPP_INLINE_VISIBILITY - void swap(array& __a) - _NOEXCEPT_(_Size == 0 || __is_nothrow_swappable<_Tp>::value) { - static_assert(_Size != 0 || !is_const<_Tp>::value, - "cannot swap zero-sized array of type 'const T'"); - _Traits::__swap(__elems_, __a.__elems_); - } + void __swap_dispatch(std::true_type, array&) {} + + _LIBCPP_INLINE_VISIBILITY + void __swap_dispatch(std::false_type, array& __a) + { _VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);} // iterators: _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 - iterator begin() _NOEXCEPT {return iterator(data());} + iterator begin() _NOEXCEPT {return iterator(__elems_);} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 - const_iterator begin() const _NOEXCEPT {return const_iterator(data());} + const_iterator begin() const _NOEXCEPT {return const_iterator(__elems_);} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 - iterator end() _NOEXCEPT {return iterator(data() + _Size);} + iterator end() _NOEXCEPT {return iterator(__elems_ + _Size);} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 - const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);} + const_iterator end() const _NOEXCEPT {return const_iterator(__elems_ + _Size);} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} @@ -254,9 +201,9 @@ struct _LIBCPP_TEMPLATE_VIS array _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 - value_type* data() _NOEXCEPT {return _Traits::__data(__elems_);} + value_type* data() _NOEXCEPT {return __elems_;} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 - const value_type* data() const _NOEXCEPT {return _Traits::__data(__elems_);} + const value_type* data() const _NOEXCEPT {return __elems_;} }; template diff --git a/test/std/containers/sequences/array/array.cons/default.pass.cpp b/test/std/containers/sequences/array/array.cons/default.pass.cpp index 9a2a6eaa3..7bc62b759 100644 --- a/test/std/containers/sequences/array/array.cons/default.pass.cpp +++ b/test/std/containers/sequences/array/array.cons/default.pass.cpp @@ -14,14 +14,6 @@ #include #include -// std::array is explicitly allowed to be initialized with A a = { init-list };. -// Disable the missing braces warning for this reason. -#include "disable_missing_braces_warning.h" - -struct NoDefault { - NoDefault(int) {} -}; - int main() { { @@ -36,13 +28,4 @@ int main() C c; assert(c.size() == 0); } - { - typedef std::array C; - C c; - assert(c.size() == 0); - C c1 = {}; - assert(c1.size() == 0); - C c2 = {{}}; - assert(c2.size() == 0); - } } diff --git a/test/std/containers/sequences/array/array.cons/implicit_copy.pass.cpp b/test/std/containers/sequences/array/array.cons/implicit_copy.pass.cpp deleted file mode 100644 index a8434c018..000000000 --- a/test/std/containers/sequences/array/array.cons/implicit_copy.pass.cpp +++ /dev/null @@ -1,93 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// - -// implicitly generated array constructors / assignment operators - -#include -#include -#include -#include "test_macros.h" - -// std::array is explicitly allowed to be initialized with A a = { init-list };. -// Disable the missing braces warning for this reason. -#include "disable_missing_braces_warning.h" - -// In C++03 the copy assignment operator is not deleted when the implicitly -// generated operator would be ill-formed; like in the case of a struct with a -// const member. -#if TEST_STD_VER < 11 -#define TEST_NOT_COPY_ASSIGNABLE(T) ((void)0) -#else -#define TEST_NOT_COPY_ASSIGNABLE(T) static_assert(!std::is_copy_assignable::value, "") -#endif - -struct NoDefault { - NoDefault(int) {} -}; - -int main() { - { - typedef double T; - typedef std::array C; - C c = {1.1, 2.2, 3.3}; - C c2 = c; - c2 = c; - static_assert(std::is_copy_constructible::value, ""); - static_assert(std::is_copy_assignable::value, ""); - } - { - typedef double T; - typedef std::array C; - C c = {1.1, 2.2, 3.3}; - C c2 = c; - ((void)c2); - static_assert(std::is_copy_constructible::value, ""); - TEST_NOT_COPY_ASSIGNABLE(C); - } - { - typedef double T; - typedef std::array C; - C c = {}; - C c2 = c; - c2 = c; - static_assert(std::is_copy_constructible::value, ""); - static_assert(std::is_copy_assignable::value, ""); - } - { - // const arrays of size 0 should disable the implicit copy assignment operator. - typedef double T; - typedef std::array C; - C c = {}; - C c2 = c; - ((void)c2); - static_assert(std::is_copy_constructible::value, ""); - TEST_NOT_COPY_ASSIGNABLE(C); - } - { - typedef NoDefault T; - typedef std::array C; - C c = {}; - C c2 = c; - c2 = c; - static_assert(std::is_copy_constructible::value, ""); - static_assert(std::is_copy_assignable::value, ""); - } - { - typedef NoDefault T; - typedef std::array C; - C c = {}; - C c2 = c; - ((void)c2); - static_assert(std::is_copy_constructible::value, ""); - TEST_NOT_COPY_ASSIGNABLE(C); - } - -} diff --git a/test/std/containers/sequences/array/array.data/data.pass.cpp b/test/std/containers/sequences/array/array.data/data.pass.cpp index 452d6b7f3..d7aed70c9 100644 --- a/test/std/containers/sequences/array/array.data/data.pass.cpp +++ b/test/std/containers/sequences/array/array.data/data.pass.cpp @@ -36,22 +36,4 @@ int main() T* p = c.data(); (void)p; // to placate scan-build } - { - typedef double T; - typedef std::array C; - C c = {}; - const T* p = c.data(); - static_assert((std::is_same::value), ""); - (void)p; // to placate scan-build - } - { - struct NoDefault { - NoDefault(int) {} - }; - typedef NoDefault T; - typedef std::array C; - C c = {}; - T* p = c.data(); - assert(p != nullptr); - } } diff --git a/test/std/containers/sequences/array/array.data/data_const.pass.cpp b/test/std/containers/sequences/array/array.data/data_const.pass.cpp index f10d51580..5be082eeb 100644 --- a/test/std/containers/sequences/array/array.data/data_const.pass.cpp +++ b/test/std/containers/sequences/array/array.data/data_const.pass.cpp @@ -38,16 +38,6 @@ int main() const T* p = c.data(); (void)p; // to placate scan-build } - { - struct NoDefault { - NoDefault(int) {} - }; - typedef NoDefault T; - typedef std::array C; - const C c = {}; - const T* p = c.data(); - assert(p != nullptr); - } #if TEST_STD_VER > 14 { typedef std::array C; diff --git a/test/std/containers/sequences/array/array.fill/fill.fail.cpp b/test/std/containers/sequences/array/array.fill/fill.fail.cpp deleted file mode 100644 index 07816c7c7..000000000 --- a/test/std/containers/sequences/array/array.fill/fill.fail.cpp +++ /dev/null @@ -1,29 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// - -// void fill(const T& u); - -#include -#include - -// std::array is explicitly allowed to be initialized with A a = { init-list };. -// Disable the missing braces warning for this reason. -#include "disable_missing_braces_warning.h" - -int main() { - { - typedef double T; - typedef std::array C; - C c = {}; - // expected-error@array:* {{static_assert failed "cannot fill zero-sized array of type 'const T'"}} - c.fill(5.5); // expected-note {{requested here}} - } -} diff --git a/test/std/containers/sequences/array/array.swap/swap.fail.cpp b/test/std/containers/sequences/array/array.swap/swap.fail.cpp deleted file mode 100644 index 26febadb7..000000000 --- a/test/std/containers/sequences/array/array.swap/swap.fail.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// - -// void swap(array& a); - -#include -#include - -// std::array is explicitly allowed to be initialized with A a = { init-list };. -// Disable the missing braces warning for this reason. -#include "disable_missing_braces_warning.h" - -int main() { - { - typedef double T; - typedef std::array C; - C c = {}; - C c2 = {}; - // expected-error@array:* {{static_assert failed "cannot swap zero-sized array of type 'const T'"}} - c.swap(c2); // expected-note {{requested here}} - } -} diff --git a/test/std/containers/sequences/array/begin.pass.cpp b/test/std/containers/sequences/array/begin.pass.cpp index 8cdef466a..b12ffc851 100644 --- a/test/std/containers/sequences/array/begin.pass.cpp +++ b/test/std/containers/sequences/array/begin.pass.cpp @@ -31,13 +31,4 @@ int main() *i = 5.5; assert(c[0] == 5.5); } - { - struct NoDefault { - NoDefault(int) {} - }; - typedef NoDefault T; - typedef std::array C; - C c = {}; - assert(c.begin() == c.end()); - } }