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

Michael Schellenberger Costa via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 30 05:08:15 PST 2019


miscco created this revision.
Herald added a reviewer: EricWF.
Herald added subscribers: libcxx-commits, ldionne, christof.
Herald added a project: libc++.

The static asserts in span<T, N>::front() and span<T, N>::back() are incorrect as they may be triggered from valid code
due to evaluation of a never taken branch.

span foo;
if (!foo.empty()) {

  auto x = foo.front();

}

The problem is that the branch is always evaluated by the compiler creating invalid compile errors for span<T, 0>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71995

Files:
  libcxx/include/span


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.235582.patch
Type: text/x-patch
Size: 697 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20191230/37539fc3/attachment.bin>


More information about the libcxx-commits mailing list