[libcxx-commits] [PATCH] D71995: [libcxx] span: Fix incorrect static asserts

Michael Schellenberger Costa via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 11 03:54:01 PST 2020


miscco updated this revision to Diff 243797.
miscco added a comment.

Add tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71995/new/

https://reviews.llvm.org/D71995

Files:
  libcxx/include/span
  libcxx/test/std/containers/views/span.elem/back.pass.cpp
  libcxx/test/std/containers/views/span.elem/front.pass.cpp


Index: libcxx/test/std/containers/views/span.elem/front.pass.cpp
===================================================================
--- libcxx/test/std/containers/views/span.elem/front.pass.cpp
+++ libcxx/test/std/containers/views/span.elem/front.pass.cpp
@@ -38,6 +38,12 @@
     assert(std::addressof(sp.front()) == sp.data());
 }
 
+template <typename Span>
+void testEmptySpan(Span sp)
+{
+    if (!sp.empty())
+        sp.front();
+}
 
 struct A{};
 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
@@ -71,5 +77,8 @@
     testRuntimeSpan(std::span<std::string>   (&s, 1));
     testRuntimeSpan(std::span<std::string, 1>(&s, 1));
 
+    std::span<int, 0> sp;
+    testEmptySpan(sp);
+
     return 0;
 }
Index: libcxx/test/std/containers/views/span.elem/back.pass.cpp
===================================================================
--- libcxx/test/std/containers/views/span.elem/back.pass.cpp
+++ libcxx/test/std/containers/views/span.elem/back.pass.cpp
@@ -38,6 +38,13 @@
     assert(std::addressof(sp.back()) == sp.data() + sp.size() - 1);
 }
 
+template <typename Span>
+void testEmptySpan(Span sp)
+{
+    if (!sp.empty())
+        sp.back();
+}
+
 
 struct A{};
 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
@@ -71,5 +78,8 @@
     testRuntimeSpan(std::span<std::string>   (&s, 1));
     testRuntimeSpan(std::span<std::string, 1>(&s, 1));
 
+    std::span<int, 0> sp;
+    testEmptySpan(sp);
+
     return 0;
 }
Index: libcxx/include/span
===================================================================
--- libcxx/include/span
+++ libcxx/include/span
@@ -307,13 +307,13 @@
 
     _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
     {
-        static_assert(_Extent > 0, "span<T,N>[].front() on empty span");
+        _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
         return __data[0];
     }
 
     _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
     {
-        static_assert(_Extent > 0, "span<T,N>[].back() on empty span");
+        _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
         return __data[size()-1];
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71995.243797.patch
Type: text/x-patch
Size: 2158 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200211/51ab6c42/attachment.bin>


More information about the libcxx-commits mailing list