blob: 6f459af3d75de2139696824a9f587cb856a88a71 [file] [log] [blame]
Hans Wennborge5d14a82019-01-14 11:31:241// Copyright 2017 The Chromium Authors. All rights reserved.
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
Jan Wilken Dörrieb28b5902019-11-04 23:19:4314#include "base/strings/string_piece.h"
15
Hans Wennborge5d14a82019-01-14 11:31:2416namespace base {
17
18class Base {
19};
20
21class Derived : Base {
22};
23
24#if defined(NCTEST_DEFAULT_SPAN_WITH_NON_ZERO_STATIC_EXTENT_DISALLOWED) // [r"fatal error: static_assert failed due to requirement '1UL == dynamic_extent || 1UL == 0' \"Invalid Extent\""]
25
26// A default constructed span must have an extent of 0 or dynamic_extent.
27void WontCompile() {
28 span<int, 1> span;
29}
30
31#elif defined(NCTEST_SPAN_FROM_ARRAY_WITH_NON_MATCHING_STATIC_EXTENT_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int, 1>'"]
32
33// A span with static extent constructed from an array must match the size of
34// the array.
35void WontCompile() {
36 int array[] = {1, 2, 3};
37 span<int, 1> span(array);
38}
39
Hans Wennborge5d14a82019-01-14 11:31:2440#elif defined(NCTEST_SPAN_FROM_OTHER_SPAN_WITH_MISMATCHING_EXTENT_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int, 4>'"]
41
42// A span with static extent constructed from another span must match the
43// extent.
44void WontCompile() {
45 std::array<int, 3> array = {1, 2, 3};
46 span<int, 3> span3(array);
47 span<int, 4> span4(span3);
48}
49
50#elif defined(NCTEST_DYNAMIC_SPAN_TO_STATIC_SPAN_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int, 3>'"]
51
52// Converting a dynamic span to a static span should not be allowed.
53void WontCompile() {
54 span<int> dynamic_span;
55 span<int, 3> static_span(dynamic_span);
56}
57
58#elif defined(NCTEST_DERIVED_TO_BASE_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<base::Base \*>'"]
59
60// Internally, this is represented as a pointer to pointers to Derived. An
61// implicit conversion to a pointer to pointers to Base must not be allowed.
62// If it were allowed, then something like this would be possible.
63// Cat** cats = GetCats();
64// Animals** animals = cats;
65// animals[0] = new Dog(); // Uhoh!
66void WontCompile() {
67 span<Derived*> derived_span;
68 span<Base*> base_span(derived_span);
69}
70
71#elif defined(NCTEST_PTR_TO_CONSTPTR_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<const int \*>'"]
72
73// Similarly, converting a span<int*> to span<const int*> requires internally
74// converting T** to const T**. This is also disallowed, as it would allow code
75// to violate the contract of const.
76void WontCompile() {
77 span<int*> non_const_span;
78 span<const int*> const_span(non_const_span);
79}
80
81#elif defined(NCTEST_CONST_CONTAINER_TO_MUTABLE_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
82
83// A const container should not be convertible to a mutable span.
84void WontCompile() {
85 const std::vector<int> v = {1, 2, 3};
86 span<int> span(v);
87}
88
89#elif defined(NCTEST_IMPLICIT_CONVERSION_FROM_DYNAMIC_CONST_CONTAINER_TO_STATIC_SPAN_DISALLOWED) // [r"fatal error: no viable conversion from 'const std::vector<int>' to 'span<const int, 3>'"]
90
91// A dynamic const container should not be implicitly convertible to a static span.
92void WontCompile() {
93 const std::vector<int> v = {1, 2, 3};
94 span<const int, 3> span = v;
95}
96
97#elif defined(NCTEST_IMPLICIT_CONVERSION_FROM_DYNAMIC_MUTABLE_CONTAINER_TO_STATIC_SPAN_DISALLOWED) // [r"fatal error: no viable conversion from 'std::vector<int>' to 'span<int, 3>'"]
98
99// A dynamic mutable container should not be implicitly convertible to a static span.
100void WontCompile() {
101 std::vector<int> v = {1, 2, 3};
102 span<int, 3> span = v;
103}
104
Jan Wilken Dörrie9269f9eb2020-10-13 12:56:25105#elif defined(NCTEST_STD_SET_ITER_SIZE_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
106
107// A std::set() should not satisfy the requirements for conversion to a span.
108void WontCompile() {
109 std::set<int> set;
110 span<int> span(set.begin(), 0);
111}
112
113#elif defined(NCTEST_STD_SET_ITER_ITER_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
114
115// A std::set() should not satisfy the requirements for conversion to a span.
116void WontCompile() {
117 std::set<int> set;
118 span<int> span(set.begin(), set.end());
119}
120
Hans Wennborge5d14a82019-01-14 11:31:24121#elif defined(NCTEST_STD_SET_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
122
123// A std::set() should not satisfy the requirements for conversion to a span.
124void WontCompile() {
125 std::set<int> set;
126 span<int> span(set);
127}
128
129#elif defined(NCTEST_STATIC_FRONT_WITH_EXCEEDING_COUNT_DISALLOWED) // [r" fatal error: static_assert failed due to requirement '3UL == dynamic_extent || 4UL <= 3UL' \"Count must not exceed Extent\""]
130
131// Static first called on a span with static extent must not exceed the size.
132void WontCompile() {
133 std::array<int, 3> array = {1, 2, 3};
134 span<int, 3> span(array);
135 auto first = span.first<4>();
136}
137
138#elif defined(NCTEST_STATIC_LAST_WITH_EXCEEDING_COUNT_DISALLOWED) // [r"fatal error: static_assert failed due to requirement '3UL == dynamic_extent || 4UL <= 3UL' \"Count must not exceed Extent\""]
139
140// Static last called on a span with static extent must not exceed the size.
141void WontCompile() {
142 std::array<int, 3> array = {1, 2, 3};
143 span<int, 3> span(array);
144 auto last = span.last<4>();
145}
146
147#elif defined(NCTEST_STATIC_SUBSPAN_WITH_EXCEEDING_OFFSET_DISALLOWED) // [r"fatal error: static_assert failed due to requirement '3UL == dynamic_extent || 4UL <= 3UL' \"Count must not exceed Extent\""]
148
149// Static subspan called on a span with static extent must not exceed the size.
150void WontCompile() {
151 std::array<int, 3> array = {1, 2, 3};
152 span<int, 3> span(array);
153 auto subspan = span.subspan<4>();
154}
155
156#elif defined(NCTEST_STATIC_SUBSPAN_WITH_EXCEEDING_COUNT_DISALLOWED) // [r"fatal error: static_assert failed due to requirement '3UL == dynamic_extent || 4UL == dynamic_extent || 4UL <= 3UL - 0UL' \"Count must not exceed Extent - Offset\""]
157
158// Static subspan called on a span with static extent must not exceed the size.
159void WontCompile() {
160 std::array<int, 3> array = {1, 2, 3};
161 span<int, 3> span(array);
162 auto subspan = span.subspan<0, 4>();
163}
164
jdoerrieba8aa5f2019-04-02 07:32:27165#elif defined(NCTEST_DISCARD_RETURN_OF_EMPTY_DISALLOWED) // [r"ignoring return value of function"]
166
167// Discarding the return value of empty() is not allowed.
168void WontCompile() {
169 span<int> s;
170 s.empty();
171}
172
173#elif defined(NCTEST_FRONT_ON_EMPTY_STATIC_SPAN_DISALLOWED) // [r"Extent must not be 0"]
174
175// Front called on an empty span with static extent is not allowed.
176void WontCompile() {
177 span<int, 0> s;
178 s.front();
179}
180
181#elif defined(NCTEST_BACK_ON_EMPTY_STATIC_SPAN_DISALLOWED) // [r"Extent must not be 0"]
182
183// Back called on an empty span with static extent is not allowed.
184void WontCompile() {
185 span<int, 0> s;
186 s.back();
187}
188
189#elif defined(NCTEST_SWAP_WITH_DIFFERENT_EXTENTS_DISALLOWED) // [r"no matching function for call to 'swap'"]
190
191// Calling swap on spans with different extents is not allowed.
192void WontCompile() {
193 std::array<int, 3> array = {1, 2, 3};
194 span<int, 3> static_span(array);
195 span<int> dynamic_span(array);
196 std::swap(static_span, dynamic_span);
197}
198
Hans Wennborge5d14a82019-01-14 11:31:24199#elif defined(NCTEST_AS_WRITABLE_BYTES_WITH_CONST_CONTAINER_DISALLOWED) // [r"fatal error: no matching function for call to 'as_writable_bytes'"]
200
201// as_writable_bytes should not be possible for a const container.
202void WontCompile() {
203 const std::vector<int> v = {1, 2, 3};
204 span<uint8_t> bytes = as_writable_bytes(make_span(v));
205}
206
Jan Wilken Dörrie9269f9eb2020-10-13 12:56:25207#elif defined(NCTEST_MAKE_SPAN_FROM_SET_ITER_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<T>'"]
208// A std::set() should not satisfy the requirements for conversion to a span.
209void WontCompile() {
210 std::set<int> set;
211 auto span = make_span(set.begin(), set.end());
212}
213
Jan Wilken Dörrieb28b5902019-11-04 23:19:43214#elif defined(NCTEST_MAKE_SPAN_FROM_SET_CONVERSION_DISALLOWED) // [r"fatal error: no matching function for call to 'data'"]
Hans Wennborge5d14a82019-01-14 11:31:24215
216// A std::set() should not satisfy the requirements for conversion to a span.
217void WontCompile() {
218 std::set<int> set;
219 auto span = make_span(set);
220}
221
Jan Wilken Dörrieb28b5902019-11-04 23:19:43222#elif defined(NCTEST_CONST_VECTOR_DEDUCES_AS_CONST_SPAN) // [r"fatal error: no viable conversion from 'span<const int, \[...\]>' to 'span<int, \[...\]>'"]
Daniel Cheng66a87fb22019-04-06 01:08:26223
224int WontCompile() {
225 const std::vector<int> v;
226 span<int> s = make_span(v);
227}
228
Jan Wilken Dörrie9269f9eb2020-10-13 12:56:25229#elif defined(NCTEST_STATIC_MAKE_SPAN_FROM_ITER_AND_SIZE_CHECKS) // [r"constexpr variable 'made_span' must be initialized by a constant expression"]
230
231// The static make_span<N>(begin, size) should CHECK whether N matches size.
232// This should result in compilation failures when evaluated at compile
233// time.
234int WontCompile() {
235 constexpr StringPiece str = "Foo";
236 // Intentional extent mismatch causing CHECK failure.
237 constexpr auto made_span = make_span<2>(str.begin(), 3);
238}
239
240#elif defined(NCTEST_STATIC_MAKE_SPAN_FROM_ITERS_CHECKS_SIZE) // [r"constexpr variable 'made_span' must be initialized by a constant expression"]
241
242// The static make_span<N>(begin, end) should CHECK whether N matches end -
243// begin. This should result in compilation failures when evaluated at compile
244// time.
245int WontCompile() {
246 constexpr StringPiece str = "Foo";
247 // Intentional extent mismatch causing CHECK failure.
248 constexpr auto made_span = make_span<2>(str.begin(), str.end());
249}
250
Jan Wilken Dörrieb28b5902019-11-04 23:19:43251#elif defined(NCTEST_STATIC_MAKE_SPAN_CHECKS_SIZE) // [r"constexpr variable 'made_span' must be initialized by a constant expression"]
252
253// The static make_span<N>(cont) should CHECK whether N matches size(cont). This
254// should result in compilation failures when evaluated at compile time.
255int WontCompile() {
256 constexpr StringPiece str = "Foo";
257 // Intentional extent mismatch causing CHECK failure.
258 constexpr auto made_span = make_span<2>(str);
259}
260
Adam Langley9d8b7832020-08-26 22:18:09261#elif defined(NCTEST_EXTENT_NO_DYNAMIC_EXTENT) // [r"EXTENT should only be used for containers with a static extent"]
262
263// EXTENT should not result in |dynamic_extent|, it should be a compile-time
264// error.
265void WontCompile() {
266 std::vector<uint8_t> vector;
267 static_assert(EXTENT(vector) == 0, "Should not compile");
268}
269
Hans Wennborge5d14a82019-01-14 11:31:24270#endif
271
272} // namespace base