• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
13
14#include "base/strings/string_piece.h"
15
16namespace base {
17
18class Base {
19};
20
21class Derived : Base {
22};
23
24// A default constructed span must have an extent of 0 or dynamic_extent.
25void DefaultSpanWithNonZeroStaticExtentDisallowed() {
26  span<int, 1u> span;  // expected-error {{no matching constructor for initialization of 'span<int, 1U>'}}
27}
28
29// A span with static extent constructed from an array must match the size of
30// the array.
31void SpanFromArrayWithNonMatchingStaticExtentDisallowed() {
32  int array[] = {1, 2, 3};
33  span<int, 1u> span(array);  // expected-error {{no matching constructor for initialization of 'span<int, 1U>'}}
34}
35
36// A span with static extent constructed from another span must match the
37// extent.
38void SpanFromOtherSpanWithMismatchingExtentDisallowed() {
39  std::array<int, 3> array = {1, 2, 3};
40  span<int, 3u> span3(array);
41  span<int, 4u> span4(span3);  // expected-error {{no matching constructor for initialization of 'span<int, 4U>'}}
42}
43
44// Converting a dynamic span to a static span should not be allowed.
45void DynamicSpanToStaticSpanDisallowed() {
46  span<int> dynamic_span;
47  span<int, 3u> static_span = dynamic_span;  // expected-error-re {{no viable conversion from 'span<[...], (default) dynamic_extent aka {{.*}}>' to 'span<[...], 3>'}}
48}
49
50// Internally, this is represented as a pointer to pointers to Derived. An
51// implicit conversion to a pointer to pointers to Base must not be allowed.
52// If it were allowed, then something like this would be possible:
53//   Cat** cats = GetCats();
54//   Animals** animals = cats;
55//   animals[0] = new Dog();  // Uh oh!
56void DerivedToBaseConversionDisallowed() {
57  span<Derived*> derived_span;
58  span<Base*> base_span(derived_span);  // expected-error {{no matching constructor for initialization of 'span<Base *>'}}
59}
60
61// Similarly, converting a span<int*> to span<const int*> requires internally
62// converting T** to const T**. This is also disallowed, as it would allow code
63// to violate the contract of const.
64void PtrToConstPtrConversionDisallowed() {
65  span<int*> non_const_span;
66  span<const int*> const_span(non_const_span);  // expected-error {{no matching constructor for initialization of 'span<const int *>'}}
67}
68
69// A const container should not be convertible to a mutable span.
70void ConstContainerToMutableConversionDisallowed() {
71  const std::vector<int> v = {1, 2, 3};
72  span<int> span(v);  // expected-error {{no matching constructor for initialization of 'span<int>'}}
73}
74
75// A dynamic const container should not be implicitly convertible to a static span.
76void ImplicitConversionFromDynamicConstContainerToStaticSpanDisallowed() {
77  const std::vector<int> v = {1, 2, 3};
78  span<const int, 3u> span = v;  // expected-error {{no viable conversion from 'const std::vector<int>' to 'span<const int, 3U>'}}
79}
80
81// A dynamic mutable container should not be implicitly convertible to a static span.
82void ImplicitConversionFromDynamicMutableContainerToStaticSpanDisallowed() {
83  std::vector<int> v = {1, 2, 3};
84  span<int, 3u> span = v;  // expected-error {{no viable conversion from 'std::vector<int>' to 'span<int, 3U>'}}
85}
86
87// A std::set() should not satisfy the requirements for conversion to a span.
88void StdSetConversionDisallowed() {
89  std::set<int> set;
90  span<int> span1(set.begin(), 0u);                // expected-error {{no matching constructor for initialization of 'span<int>'}}
91  span<int> span2(set.begin(), set.end());         // expected-error {{no matching constructor for initialization of 'span<int>'}}
92  span<int> span3(set);                            // expected-error {{no matching constructor for initialization of 'span<int>'}}
93  auto span4 = make_span(set.begin(), 0u);         // expected-error@*:* {{no matching constructor for initialization of 'span<T>' (aka 'span<const int>')}}
94  auto span5 = make_span(set.begin(), set.end());  // expected-error@*:* {{no matching constructor for initialization of 'span<T>' (aka 'span<const int>')}}
95  auto span6 = make_span(set);                     // expected-error@*:* {{no matching function for call to 'data'}}
96}
97
98// Static views of spans with static extent must not exceed the size.
99void OutOfRangeSubviewsOnStaticSpan() {
100  std::array<int, 3> array = {1, 2, 3};
101  span<int, 3u> span(array);
102  auto first = span.first<4>();          // expected-error@*:* {{no matching member function for call to 'first'}}
103  auto last = span.last<4>();            // expected-error@*:* {{no matching member function for call to 'last'}}
104  auto subspan1 = span.subspan<4>();     // expected-error@*:* {{no matching member function for call to 'subspan'}}
105  auto subspan2 = span.subspan<0, 4>();  // expected-error@*:* {{no matching member function for call to 'subspan'}}
106}
107
108// Discarding the return value of empty() is not allowed.
109void DiscardReturnOfEmptyDisallowed() {
110  span<int> s;
111  s.empty();  // expected-error {{ignoring return value of function}}
112}
113
114// Getting elements of an empty span with static extent is not allowed.
115void RefsOnEmptyStaticSpanDisallowed() {
116  span<int, 0u> s;
117  s.front();  // expected-error@*:* {{invalid reference to function 'front': constraints not satisfied}}
118  s.back();   // expected-error@*:* {{invalid reference to function 'back': constraints not satisfied}}
119}
120
121// Calling swap on spans with different extents is not allowed.
122void SwapWithDifferentExtentsDisallowed() {
123  std::array<int, 3> array = {1, 2, 3};
124  span<int, 3u> static_span(array);
125  span<int> dynamic_span(array);
126  std::swap(static_span, dynamic_span);  // expected-error {{no matching function for call to 'swap'}}
127}
128
129// as_writable_bytes should not be possible for a const container.
130void AsWritableBytesWithConstContainerDisallowed() {
131  const std::vector<int> v = {1, 2, 3};
132  span<uint8_t> bytes = as_writable_bytes(make_span(v));  // expected-error {{no matching function for call to 'as_writable_bytes'}}
133}
134
135void ConstVectorDeducesAsConstSpan() {
136  const std::vector<int> v;
137  span<int> s = make_span(v);  // expected-error-re@*:* {{no viable conversion from 'span<{{.*}}, [...]>' to 'span<int, [...]>'}}
138}
139
140// make_span<N>() should CHECK whether N matches the actual size.
141void MakeSpanChecksSize() {
142  constexpr StringPiece str = "Foo";
143  constexpr auto made_span1 = make_span<2>(str.begin(), 3u);         // expected-error {{constexpr variable 'made_span1' must be initialized by a constant expression}}
144  constexpr auto made_span2 = make_span<2>(str.begin(), str.end());  // expected-error {{constexpr variable 'made_span2' must be initialized by a constant expression}}
145  constexpr auto made_span3 = make_span<2>(str);                     // expected-error {{constexpr variable 'made_span3' must be initialized by a constant expression}}
146}
147
148// EXTENT should not result in |dynamic_extent|, it should be a compile-time
149// error.
150void ExtentNoDynamicExtent() {
151  std::vector<uint8_t> vector;
152  constexpr size_t extent = EXTENT(vector);  // expected-error@*:* {{EXTENT should only be used for containers with a static extent}}
153}
154
155void Dangling() {
156  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}}
157  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}}
158}
159
160void NotSizeTSize() {
161  std::vector<int> vector = {1, 2, 3};
162  // Using distinct enum types causes distinct span template instantiations, so
163  // we get assertion failures below where we expect.
164  enum Length1 { kSize1 = -1 };
165  enum Length2 { kSize2 = -1 };
166  auto s1 = make_span(vector.data(), kSize1);  // expected-error@*:* {{The source type is out of range for the destination type}}
167  span s2(vector.data(), kSize2);              // expected-error@*:* {{The source type is out of range for the destination type}}
168}
169
170void FromVolatileArrayDisallowed() {
171  static volatile int array[] = {1, 2, 3};
172  span<int> s(array);  // expected-error {{no matching constructor for initialization of 'span<int>'}}
173}
174
175}  // namespace base
176