diff --git a/libcxx/include/span b/libcxx/include/span --- a/libcxx/include/span +++ b/libcxx/include/span @@ -209,8 +209,8 @@ static constexpr index_type extent = _Extent; // [span.cons], span constructors, copy, assignment, and destructor - _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} - { static_assert(_Extent == 0, "Can't default construct a statically sized span with size > 0"); } + template = nullptr> + _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {} constexpr span (const span&) noexcept = default; constexpr span& operator=(const span&) noexcept = default; @@ -279,6 +279,7 @@ -> span { static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()"); + static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()"); return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; } @@ -291,7 +292,7 @@ _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; - _LIBCPP_ASSERT(__offset <= size() - __count, "count + offset out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); return {data() + __offset, __count}; } @@ -307,13 +308,13 @@ _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept { - static_assert(_Extent > 0, "span[].front() on empty span"); + static_assert(_Extent > 0, "span[].front() on empty span"); return __data[0]; } _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept { - static_assert(_Extent > 0, "span[].back() on empty span"); + static_assert(_Extent > 0, "span[].back() on empty span"); return __data[size()-1]; } @@ -445,10 +446,10 @@ template _LIBCPP_INLINE_VISIBILITY - constexpr span<_Tp, dynamic_extent> subspan() const noexcept + constexpr span subspan() const noexcept { _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()"); - _LIBCPP_ASSERT(_Count == dynamic_extent || _Offset + _Count <= size(), "Count out of range in span::subspan()"); + _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()"); return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; } @@ -460,7 +461,7 @@ _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; - _LIBCPP_ASSERT(__offset <= size() - __count, "Offset + count out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); return {data() + __offset, __count}; } diff --git a/libcxx/test/std/containers/views/span.cons/default.fail.cpp b/libcxx/test/std/containers/views/span.cons/default.fail.cpp --- a/libcxx/test/std/containers/views/span.cons/default.fail.cpp +++ b/libcxx/test/std/containers/views/span.cons/default.fail.cpp @@ -13,7 +13,7 @@ // constexpr span() noexcept; // // Remarks: This constructor shall not participate in overload resolution -// unless Extent <= 0 is true. +// unless Extent == 0 || Extent == dynamic_extent is true. #include @@ -24,10 +24,7 @@ int main(int, char**) { - std::span s; // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Can't default construct a statically sized span with size > 0"}} - -// TODO: This is what I want: -// eXpected-error {{no matching constructor for initialization of 'std::span'}} + std::span s; // expected-error {{no matching constructor for initialization of 'std::span'}} return 0; } diff --git a/libcxx/test/std/containers/views/span.elem/elem.fail.cpp b/libcxx/test/std/containers/views/span.elem/elem.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/views/span.elem/elem.fail.cpp @@ -0,0 +1,43 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// 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 +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr reference back() const noexcept; +// Expects: empty() is false. +// Effects: Equivalent to: return *(data() + (size() - 1)); +// +// constexpr reference front() const noexcept; +// Expects: empty() is false. +// Effects: Equivalent to: return *data(); +// +// constexpr reference operator[](index_type idx) const; +// constexpr reference operator()(index_type idx) const; +// + +#include + +#include "test_macros.h" + +int main(int, char**) { + std::span sp; + + // Empty back + { + [[maybe_unused]] auto s1 = sp.back(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "span[].back() on empty span"}} + } + + // Empty front + { + [[maybe_unused]] auto s1 = sp.front(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "span[].front() on empty span"}} + } + + return 0; +} diff --git a/libcxx/test/std/containers/views/span.sub/first.fail.cpp b/libcxx/test/std/containers/views/span.sub/first.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/views/span.sub/first.fail.cpp @@ -0,0 +1,40 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// 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 +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span first() const; +// +// constexpr span first(index_type count) const; +// +// Requires: 0 <= Count && Count <= size(). + +#include + +#include "test_macros.h" + +constexpr int carr[] = {1, 2, 3, 4}; + +int main(int, char**) { + std::span sp(carr); + + // Count too large + { + [[maybe_unused]] auto s1 = sp.first<5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::first()"}} + } + + // Count numeric_limits + { + [[maybe_unused]] auto s1 = sp.first(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::first()"}} + } + + return 0; +} diff --git a/libcxx/test/std/containers/views/span.sub/first.pass.cpp b/libcxx/test/std/containers/views/span.sub/first.pass.cpp --- a/libcxx/test/std/containers/views/span.sub/first.pass.cpp +++ b/libcxx/test/std/containers/views/span.sub/first.pass.cpp @@ -15,7 +15,7 @@ // // constexpr span first(index_type count) const; // -// Requires: 0 <= Count && Count <= size(). +// Requires: Count <= size(). #include diff --git a/libcxx/test/std/containers/views/span.sub/last.fail.cpp b/libcxx/test/std/containers/views/span.sub/last.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/views/span.sub/last.fail.cpp @@ -0,0 +1,40 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// 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 +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span last() const; +// +// constexpr span last(index_type count) const; +// +// Requires: Count <= size(). + +#include + +#include "test_macros.h" + +constexpr int carr[] = {1, 2, 3, 4}; + +int main(int, char**) { + std::span sp(carr); + + // Count too large + { + [[maybe_unused]] auto s1 = sp.last<5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::last()"}} + } + + // Count numeric_limits + { + [[maybe_unused]] auto s1 = sp.last(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::last()"}} + } + + return 0; +} diff --git a/libcxx/test/std/containers/views/span.sub/last.pass.cpp b/libcxx/test/std/containers/views/span.sub/last.pass.cpp --- a/libcxx/test/std/containers/views/span.sub/last.pass.cpp +++ b/libcxx/test/std/containers/views/span.sub/last.pass.cpp @@ -15,7 +15,7 @@ // // constexpr span last(index_type count) const; // -// Requires: 0 <= Count && Count <= size(). +// Requires: Count <= size(). #include diff --git a/libcxx/test/std/containers/views/span.sub/subspan.fail.cpp b/libcxx/test/std/containers/views/span.sub/subspan.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/views/span.sub/subspan.fail.cpp @@ -0,0 +1,52 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// 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 +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span subspan() const; +// +// constexpr span subspan( +// index_type offset, index_type count = dynamic_extent) const; +// +// Requires: offset <= size() && +// (count == dynamic_extent || count <= size() - offset) + +#include + +#include "test_macros.h" + +constexpr int carr[] = {1, 2, 3, 4}; + +int main(int, char**) { + std::span sp(carr); + + // Offset too large templatized + { + [[maybe_unused]] auto s1 = sp.subspan<5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset out of range in span::subspan()"}} + } + + // Count too large templatized + { + [[maybe_unused]] auto s1 = sp.subspan<0, 5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset + count out of range in span::subspan()"}} + } + + // Offset + Count too large templatized + { + [[maybe_unused]] auto s1 = sp.subspan<2, 3>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset + count out of range in span::subspan()"}} + } + + // Offset + Count overflow templatized + { + [[maybe_unused]] auto s1 = sp.subspan<3, std::size_t(-2)>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset + count out of range in span::subspan()"}}, expected-error-re@span:* {{array is too large{{(.* elements)}}}} + } + + return 0; +} diff --git a/libcxx/test/std/containers/views/span.sub/subspan.pass.cpp b/libcxx/test/std/containers/views/span.sub/subspan.pass.cpp --- a/libcxx/test/std/containers/views/span.sub/subspan.pass.cpp +++ b/libcxx/test/std/containers/views/span.sub/subspan.pass.cpp @@ -37,8 +37,11 @@ using S2 = decltype(s2); ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); - static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Count), ""); - static_assert(S2::extent == std::dynamic_extent, ""); + static_assert(S1::extent == + ((Count != std::dynamic_extent) ? Count + : ((Span::extent != std::dynamic_extent) ? Span::extent - Offset + : std::dynamic_extent)), ""); + static_assert(S2::extent == std::dynamic_extent, ""); return s1.data() == s2.data() && s1.size() == s2.size() @@ -76,7 +79,10 @@ using S2 = decltype(s2); ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); - static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Count), ""); + static_assert(S1::extent == + ((Count != std::dynamic_extent) ? Count + : ((Span::extent != std::dynamic_extent) ? Span::extent - Offset + : std::dynamic_extent)), ""); static_assert(S2::extent == std::dynamic_extent, ""); assert(s1.data() == s2.data()); assert(s1.size() == s2.size());