1// Copyright 2017 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// This is a "No Compile Test" suite. 6// http://dev.chromium.org/developers/testing/no-compile-tests 7 8#include "base/containers/span.h" 9 10#include <array> 11#include <set> 12#include <string_view> 13#include <vector> 14 15 16 17namespace base { 18 19class Base { 20}; 21 22class Derived : Base { 23}; 24 25// A default constructed span must have an extent of 0 or dynamic_extent. 26void DefaultSpanWithNonZeroStaticExtentDisallowed() { 27 span<int, 1u> span; // expected-error {{no matching constructor for initialization of 'span<int, 1U>'}} 28} 29 30// A span with static extent constructed from an array must match the size of 31// the array. 32void SpanFromArrayWithNonMatchingStaticExtentDisallowed() { 33 int array[] = {1, 2, 3}; 34 span<int, 1u> span(array); // expected-error {{no matching constructor for initialization of 'span<int, 1U>'}} 35} 36 37// A span with static extent constructed from another span must match the 38// extent. 39void SpanFromOtherSpanWithMismatchingExtentDisallowed() { 40 std::array<int, 3> array = {1, 2, 3}; 41 span<int, 3u> span3(array); 42 span<int, 4u> span4(span3); // expected-error {{no matching constructor for initialization of 'span<int, 4U>'}} 43} 44 45// Converting a dynamic span to a static span should not be allowed. 46void DynamicSpanToStaticSpanDisallowed() { 47 span<int> dynamic_span; 48 span<int, 3u> static_span = dynamic_span; // expected-error-re {{no viable conversion from 'span<[...], (default) dynamic_extent aka {{.*}}>' to 'span<[...], 3>'}} 49} 50 51// Internally, this is represented as a pointer to pointers to Derived. An 52// implicit conversion to a pointer to pointers to Base must not be allowed. 53// If it were allowed, then something like this would be possible: 54// Cat** cats = GetCats(); 55// Animals** animals = cats; 56// animals[0] = new Dog(); // Uh oh! 57void DerivedToBaseConversionDisallowed() { 58 span<Derived*> derived_span; 59 span<Base*> base_span(derived_span); // expected-error {{no matching constructor for initialization of 'span<Base *>'}} 60} 61 62// Similarly, converting a span<int*> to span<const int*> requires internally 63// converting T** to const T**. This is also disallowed, as it would allow code 64// to violate the contract of const. 65void PtrToConstPtrConversionDisallowed() { 66 span<int*> non_const_span; 67 span<const int*> const_span(non_const_span); // expected-error {{no matching constructor for initialization of 'span<const int *>'}} 68} 69 70// A const container should not be convertible to a mutable span. 71void ConstContainerToMutableConversionDisallowed() { 72 const std::vector<int> v = {1, 2, 3}; 73 span<int> span(v); // expected-error {{no matching constructor for initialization of 'span<int>'}} 74} 75 76// A dynamic const container should not be implicitly convertible to a static span. 77void ImplicitConversionFromDynamicConstContainerToStaticSpanDisallowed() { 78 const std::vector<int> v = {1, 2, 3}; 79 span<const int, 3u> span = v; // expected-error {{no viable conversion from 'const std::vector<int>' to 'span<const int, 3U>'}} 80} 81 82// A dynamic mutable container should not be implicitly convertible to a static span. 83void ImplicitConversionFromDynamicMutableContainerToStaticSpanDisallowed() { 84 std::vector<int> v = {1, 2, 3}; 85 span<int, 3u> span = v; // expected-error {{no viable conversion from 'std::vector<int>' to 'span<int, 3U>'}} 86} 87 88// A std::set() should not satisfy the requirements for conversion to a span. 89void StdSetConversionDisallowed() { 90 std::set<int> set; 91 span<int> span1(set.begin(), 0u); // expected-error {{no matching constructor for initialization of 'span<int>'}} 92 span<int> span2(set.begin(), set.end()); // expected-error {{no matching constructor for initialization of 'span<int>'}} 93 span<int> span3(set); // expected-error {{no matching constructor for initialization of 'span<int>'}} 94 auto span4 = make_span(set.begin(), 0u); // expected-error@*:* {{no matching constructor for initialization of 'span<T>' (aka 'span<const int>')}} 95 auto span5 = make_span(set.begin(), set.end()); // expected-error@*:* {{no matching constructor for initialization of 'span<T>' (aka 'span<const int>')}} 96 auto span6 = make_span(set); // expected-error@*:* {{no matching function for call to 'data'}} 97} 98 99// Static views of spans with static extent must not exceed the size. 100void OutOfRangeSubviewsOnStaticSpan() { 101 std::array<int, 3> array = {1, 2, 3}; 102 span<int, 3u> span(array); 103 auto first = span.first<4>(); // expected-error@*:* {{no matching member function for call to 'first'}} 104 auto last = span.last<4>(); // expected-error@*:* {{no matching member function for call to 'last'}} 105 auto subspan1 = span.subspan<4>(); // expected-error@*:* {{no matching member function for call to 'subspan'}} 106 auto subspan2 = span.subspan<0, 4>(); // expected-error@*:* {{no matching member function for call to 'subspan'}} 107} 108 109// Discarding the return value of empty() is not allowed. 110void DiscardReturnOfEmptyDisallowed() { 111 span<int> s; 112 s.empty(); // expected-error {{ignoring return value of function}} 113} 114 115// Getting elements of an empty span with static extent is not allowed. 116void RefsOnEmptyStaticSpanDisallowed() { 117 span<int, 0u> s; 118 s.front(); // expected-error@*:* {{invalid reference to function 'front': constraints not satisfied}} 119 s.back(); // expected-error@*:* {{invalid reference to function 'back': constraints not satisfied}} 120} 121 122// Calling swap on spans with different extents is not allowed. 123void SwapWithDifferentExtentsDisallowed() { 124 std::array<int, 3> array = {1, 2, 3}; 125 span<int, 3u> static_span(array); 126 span<int> dynamic_span(array); 127 std::swap(static_span, dynamic_span); // expected-error {{no matching function for call to 'swap'}} 128} 129 130// as_writable_bytes should not be possible for a const container. 131void AsWritableBytesWithConstContainerDisallowed() { 132 const std::vector<int> v = {1, 2, 3}; 133 span<uint8_t> bytes = as_writable_bytes(make_span(v)); // expected-error {{no matching function for call to 'as_writable_bytes'}} 134} 135 136void ConstVectorDeducesAsConstSpan() { 137 const std::vector<int> v; 138 span<int> s = make_span(v); // expected-error-re@*:* {{no viable conversion from 'span<{{.*}}, [...]>' to 'span<int, [...]>'}} 139} 140 141// make_span<N>() should CHECK whether N matches the actual size. 142void MakeSpanChecksSize() { 143 constexpr std::string_view str = "Foo"; 144 constexpr auto made_span1 = make_span<2>(str.begin(), 3u); // expected-error {{constexpr variable 'made_span1' must be initialized by a constant expression}} 145 constexpr auto made_span2 = make_span<2>(str.begin(), str.end()); // expected-error {{constexpr variable 'made_span2' must be initialized by a constant expression}} 146 constexpr auto made_span3 = make_span<2>(str); // expected-error {{constexpr variable 'made_span3' must be initialized by a constant expression}} 147} 148 149// EXTENT should not result in |dynamic_extent|, it should be a compile-time 150// error. 151void ExtentNoDynamicExtent() { 152 std::vector<uint8_t> vector; 153 constexpr size_t extent = EXTENT(vector); // expected-error@*:* {{EXTENT should only be used for containers with a static extent}} 154} 155 156void Dangling() { 157 span<const int, 3u> s1{std::array<int, 3>()}; // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 158 span<const int> s2{std::vector<int>({1, 2, 3})}; // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 159} 160 161void NotSizeTSize() { 162 std::vector<int> vector = {1, 2, 3}; 163 // Using distinct enum types causes distinct span template instantiations, so 164 // we get assertion failures below where we expect. 165 enum Length1 { kSize1 = -1 }; 166 enum Length2 { kSize2 = -1 }; 167 auto s1 = make_span(vector.data(), kSize1); // expected-error@*:* {{The source type is out of range for the destination type}} 168 span s2(vector.data(), kSize2); // expected-error@*:* {{The source type is out of range for the destination type}} 169} 170 171void BadConstConversionsWithStdSpan() { 172 int kData[] = {10, 11, 12}; 173 { 174 base::span<const int, 3u> fixed_base_span(kData); 175 std::span<int, 3u> s(fixed_base_span); // expected-error {{no matching constructor}} 176 } 177 { 178 std::span<const int, 3u> fixed_std_span(kData); 179 base::span<int, 3u> s(fixed_std_span); // expected-error {{no matching constructor}} 180 } 181} 182 183void FromVolatileArrayDisallowed() { 184 static volatile int array[] = {1, 2, 3}; 185 span<int> s(array); // expected-error {{no matching constructor for initialization of 'span<int>'}} 186} 187 188void FixedSizeCopyTooSmall() { 189 const int src[] = {1, 2, 3}; 190 int dst[2]; 191 base::span(dst).copy_from(base::make_span(src)); // expected-error@*:* {{no viable conversion}} 192 193 base::span(dst).copy_from(src); // expected-error@*:* {{no viable conversion}} 194} 195 196void FixedSizeSplitAtOutOfBounds() { 197 const int arr[] = {1, 2, 3}; 198 base::span(arr).split_at<4u>(); // expected-error@*:* {{no matching member function for call to 'split_at'}} 199} 200 201void FromRefNoSuchFunctionForIntLiteral() { 202 // Expectations of this test just capture the current behavior which is not 203 // necessarily desirable or required. This test expects that when we ask the 204 // compiler to deduce the template arguments for `span_from_ref` (the only 205 // difference from `FromRefLifetimeBoundErrorForIntLiteral` below) then it 206 // will fail to find a suitable function to invoke. 207 auto wont_work = span_from_ref(123); // expected-error@*:* {{no matching function for call to 'span_from_ref'}} 208} 209 210void FromRefLifetimeBoundErrorForIntLiteral() { 211 // Testing that `ABSL_ATTRIBUTE_LIFETIME_BOUND` works as intended. 212 [[maybe_unused]] auto wont_work = 213 span_from_ref<const int>(123); // expected-error@*:* {{temporary whose address is used as value of local variable 'wont_work' will be destroyed at the end of the full-expression}} 214} 215 216void FromRefLifetimeBoundErrorForTemporaryStringObject() { 217 // Testing that `ABSL_ATTRIBUTE_LIFETIME_BOUND` works as intended. 218 [[maybe_unused]] auto wont_work = 219 span_from_ref<const std::string>("temporary string"); // expected-error@*:* {{temporary whose address is used as value of local variable 'wont_work' will be destroyed at the end of the full-expression}} 220} 221 222void RvalueArrayLifetime() { 223 [[maybe_unused]] auto wont_work = 224 as_byte_span({1, 2}); // expected-error@*:* {{temporary whose address is used as value of local variable 'wont_work' will be destroyed at the end of the full-expression}} 225} 226 227void FromCStringThatIsntStaticLifetime() { 228 [[maybe_unused]] auto wont_work = 229 span_from_cstring({'a', 'b', '\0'}); // expected-error@*:* {{temporary whose address is used as value of local variable 'wont_work' will be destroyed at the end of the full-expression}} 230 231 [[maybe_unused]] auto wont_work2 = 232 byte_span_from_cstring({'a', 'b', '\0'}); // expected-error@*:* {{temporary whose address is used as value of local variable 'wont_work2' will be destroyed at the end of the full-expression}} 233} 234 235void CompareFixedSizeMismatch() { 236 const int arr[] = {1, 2, 3}; 237 const int arr2[] = {1, 2, 3, 4}; 238 (void)(span(arr) == arr2); // expected-error@*:* {{invalid operands to binary expression}} 239 (void)(span(arr) == span(arr2)); // expected-error@*:* {{invalid operands to binary expression}} 240} 241 242void CompareNotComparable() { 243 struct NoEq { int i; }; 244 static_assert(!std::equality_comparable<NoEq>); 245 246 const NoEq arr[] = {{1}, {2}, {3}}; 247 (void)(span(arr) == arr); // expected-error@*:* {{invalid operands to binary expression}} 248 (void)(span(arr) == span(arr)); // expected-error@*:* {{invalid operands to binary expression}} 249 250 struct SelfEq { 251 constexpr bool operator==(SelfEq s) const { return i == s.i; } 252 int i; 253 }; 254 static_assert(std::equality_comparable<SelfEq>); 255 static_assert(!std::equality_comparable_with<SelfEq, int>); 256 257 const SelfEq self_arr[] = {{1}, {2}, {3}}; 258 const int int_arr[] = {1, 2, 3}; 259 260 (void)(span(self_arr) == int_arr); // expected-error@*:* {{invalid operands to binary expression}} 261 (void)(span(self_arr) == span(int_arr)); // expected-error@*:* {{invalid operands to binary expression}} 262 263 // Span's operator== works on `const T` and thus won't be able to use the 264 // non-const operator here. We get this from equality_comparable which also 265 // requires it. 266 struct NonConstEq { 267 constexpr bool operator==(NonConstEq s) { return i == s.i; } 268 int i; 269 }; 270 const NonConstEq non_arr[] = {{1}, {2}, {3}}; 271 (void)(span(non_arr) == non_arr); // expected-error@*:* {{invalid operands to binary expression}} 272 (void)(span(non_arr) == span(non_arr)); // expected-error@*:* {{invalid operands to binary expression}} 273} 274 275void AsStringViewNotBytes() { 276 const int arr[] = {1, 2, 3}; 277 as_string_view(base::span(arr)); // expected-error@*:* {{no matching function for call to 'as_string_view'}} 278} 279 280} // namespace base 281