mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2024-12-23 16:06:24 +00:00
[libc++] span: Fix incorrect static asserts
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<int, 0> 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>. Thanks to Michael Schellenberger Costa for the patch. Differential Revision: https://reviews.llvm.org/D71995
This commit is contained in:
parent
84240e0db8
commit
0a0e0afaa0
@ -307,13 +307,13 @@ public:
|
||||
|
||||
_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];
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,6 @@ constexpr bool testConstexprSpan(Span sp)
|
||||
return std::addressof(sp.back()) == sp.data() + sp.size() - 1;
|
||||
}
|
||||
|
||||
|
||||
template <typename Span>
|
||||
void testRuntimeSpan(Span sp)
|
||||
{
|
||||
@ -38,6 +37,12 @@ void testRuntimeSpan(Span sp)
|
||||
assert(std::addressof(sp.back()) == sp.data() + sp.size() - 1);
|
||||
}
|
||||
|
||||
template <typename Span>
|
||||
void testEmptySpan(Span sp)
|
||||
{
|
||||
if (!sp.empty())
|
||||
[[maybe_unused]] auto res = sp.back();
|
||||
}
|
||||
|
||||
struct A{};
|
||||
constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
@ -71,5 +76,8 @@ int main(int, char**)
|
||||
testRuntimeSpan(std::span<std::string> (&s, 1));
|
||||
testRuntimeSpan(std::span<std::string, 1>(&s, 1));
|
||||
|
||||
std::span<int, 0> sp;
|
||||
testEmptySpan(sp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -38,6 +38,12 @@ void testRuntimeSpan(Span sp)
|
||||
assert(std::addressof(sp.front()) == sp.data());
|
||||
}
|
||||
|
||||
template <typename Span>
|
||||
void testEmptySpan(Span sp)
|
||||
{
|
||||
if (!sp.empty())
|
||||
[[maybe_unused]] auto res = sp.front();
|
||||
}
|
||||
|
||||
struct A{};
|
||||
constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
@ -71,5 +77,8 @@ int main(int, char**)
|
||||
testRuntimeSpan(std::span<std::string> (&s, 1));
|
||||
testRuntimeSpan(std::span<std::string, 1>(&s, 1));
|
||||
|
||||
std::span<int, 0> sp;
|
||||
testEmptySpan(sp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user