span_unittest.nc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // 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. // This is a "No Compile Test" suite.
  5. // http://dev.chromium.org/developers/testing/no-compile-tests
  6. #include "base/containers/span.h"
  7. #include <array>
  8. #include <set>
  9. #include <vector>
  10. #include "base/strings/string_piece.h"
  11. namespace base {
  12. class Base {
  13. };
  14. class Derived : Base {
  15. };
  16. #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"]
  17. // A default constructed span must have an extent of 0 or dynamic_extent.
  18. void WontCompile() {
  19. span<int, 1> span;
  20. }
  21. #elif defined(NCTEST_SPAN_FROM_ARRAY_WITH_NON_MATCHING_STATIC_EXTENT_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int, 1>'"]
  22. // A span with static extent constructed from an array must match the size of
  23. // the array.
  24. void WontCompile() {
  25. int array[] = {1, 2, 3};
  26. span<int, 1> span(array);
  27. }
  28. #elif defined(NCTEST_SPAN_FROM_OTHER_SPAN_WITH_MISMATCHING_EXTENT_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int, 4>'"]
  29. // A span with static extent constructed from another span must match the
  30. // extent.
  31. void WontCompile() {
  32. std::array<int, 3> array = {1, 2, 3};
  33. span<int, 3> span3(array);
  34. span<int, 4> span4(span3);
  35. }
  36. #elif defined(NCTEST_DYNAMIC_SPAN_TO_STATIC_SPAN_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int, 3>'"]
  37. // Converting a dynamic span to a static span should not be allowed.
  38. void WontCompile() {
  39. span<int> dynamic_span;
  40. span<int, 3> static_span(dynamic_span);
  41. }
  42. #elif defined(NCTEST_DERIVED_TO_BASE_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<Base \*>'"]
  43. // Internally, this is represented as a pointer to pointers to Derived. An
  44. // implicit conversion to a pointer to pointers to Base must not be allowed.
  45. // If it were allowed, then something like this would be possible.
  46. // Cat** cats = GetCats();
  47. // Animals** animals = cats;
  48. // animals[0] = new Dog(); // Uhoh!
  49. void WontCompile() {
  50. span<Derived*> derived_span;
  51. span<Base*> base_span(derived_span);
  52. }
  53. #elif defined(NCTEST_PTR_TO_CONSTPTR_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<const int \*>'"]
  54. // Similarly, converting a span<int*> to span<const int*> requires internally
  55. // converting T** to const T**. This is also disallowed, as it would allow code
  56. // to violate the contract of const.
  57. void WontCompile() {
  58. span<int*> non_const_span;
  59. span<const int*> const_span(non_const_span);
  60. }
  61. #elif defined(NCTEST_CONST_CONTAINER_TO_MUTABLE_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
  62. // A const container should not be convertible to a mutable span.
  63. void WontCompile() {
  64. const std::vector<int> v = {1, 2, 3};
  65. span<int> span(v);
  66. }
  67. #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>'"]
  68. // A dynamic const container should not be implicitly convertible to a static span.
  69. void WontCompile() {
  70. const std::vector<int> v = {1, 2, 3};
  71. span<const int, 3> span = v;
  72. }
  73. #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>'"]
  74. // A dynamic mutable container should not be implicitly convertible to a static span.
  75. void WontCompile() {
  76. std::vector<int> v = {1, 2, 3};
  77. span<int, 3> span = v;
  78. }
  79. #elif defined(NCTEST_STD_SET_ITER_SIZE_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
  80. // A std::set() should not satisfy the requirements for conversion to a span.
  81. void WontCompile() {
  82. std::set<int> set;
  83. span<int> span(set.begin(), 0);
  84. }
  85. #elif defined(NCTEST_STD_SET_ITER_ITER_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
  86. // A std::set() should not satisfy the requirements for conversion to a span.
  87. void WontCompile() {
  88. std::set<int> set;
  89. span<int> span(set.begin(), set.end());
  90. }
  91. #elif defined(NCTEST_STD_SET_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<int>'"]
  92. // A std::set() should not satisfy the requirements for conversion to a span.
  93. void WontCompile() {
  94. std::set<int> set;
  95. span<int> span(set);
  96. }
  97. #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"]
  98. // Static first called on a span with static extent must not exceed the size.
  99. void WontCompile() {
  100. std::array<int, 3> array = {1, 2, 3};
  101. span<int, 3> span(array);
  102. auto first = span.first<4>();
  103. }
  104. #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"]
  105. // Static last called on a span with static extent must not exceed the size.
  106. void WontCompile() {
  107. std::array<int, 3> array = {1, 2, 3};
  108. span<int, 3> span(array);
  109. auto last = span.last<4>();
  110. }
  111. #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"]
  112. // Static subspan called on a span with static extent must not exceed the size.
  113. void WontCompile() {
  114. std::array<int, 3> array = {1, 2, 3};
  115. span<int, 3> span(array);
  116. auto subspan = span.subspan<4>();
  117. }
  118. #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"]
  119. // Static subspan called on a span with static extent must not exceed the size.
  120. void WontCompile() {
  121. std::array<int, 3> array = {1, 2, 3};
  122. span<int, 3> span(array);
  123. auto subspan = span.subspan<0, 4>();
  124. }
  125. #elif defined(NCTEST_DISCARD_RETURN_OF_EMPTY_DISALLOWED) // [r"ignoring return value of function"]
  126. // Discarding the return value of empty() is not allowed.
  127. void WontCompile() {
  128. span<int> s;
  129. s.empty();
  130. }
  131. #elif defined(NCTEST_FRONT_ON_EMPTY_STATIC_SPAN_DISALLOWED) // [r"Extent must not be 0"]
  132. // Front called on an empty span with static extent is not allowed.
  133. void WontCompile() {
  134. span<int, 0> s;
  135. s.front();
  136. }
  137. #elif defined(NCTEST_BACK_ON_EMPTY_STATIC_SPAN_DISALLOWED) // [r"Extent must not be 0"]
  138. // Back called on an empty span with static extent is not allowed.
  139. void WontCompile() {
  140. span<int, 0> s;
  141. s.back();
  142. }
  143. #elif defined(NCTEST_SWAP_WITH_DIFFERENT_EXTENTS_DISALLOWED) // [r"no matching function for call to 'swap'"]
  144. // Calling swap on spans with different extents is not allowed.
  145. void WontCompile() {
  146. std::array<int, 3> array = {1, 2, 3};
  147. span<int, 3> static_span(array);
  148. span<int> dynamic_span(array);
  149. std::swap(static_span, dynamic_span);
  150. }
  151. #elif defined(NCTEST_AS_WRITABLE_BYTES_WITH_CONST_CONTAINER_DISALLOWED) // [r"fatal error: no matching function for call to 'as_writable_bytes'"]
  152. // as_writable_bytes should not be possible for a const container.
  153. void WontCompile() {
  154. const std::vector<int> v = {1, 2, 3};
  155. span<uint8_t> bytes = as_writable_bytes(make_span(v));
  156. }
  157. #elif defined(NCTEST_MAKE_SPAN_FROM_SET_ITER_CONVERSION_DISALLOWED) // [r"fatal error: no matching constructor for initialization of 'span<T>'"]
  158. // A std::set() should not satisfy the requirements for conversion to a span.
  159. void WontCompile() {
  160. std::set<int> set;
  161. auto span = make_span(set.begin(), set.end());
  162. }
  163. #elif defined(NCTEST_MAKE_SPAN_FROM_SET_CONVERSION_DISALLOWED) // [r"fatal error: no matching function for call to 'data'"]
  164. // A std::set() should not satisfy the requirements for conversion to a span.
  165. void WontCompile() {
  166. std::set<int> set;
  167. auto span = make_span(set);
  168. }
  169. #elif defined(NCTEST_CONST_VECTOR_DEDUCES_AS_CONST_SPAN) // [r"fatal error: no viable conversion from 'span<(T|const int), \[...\]>' to 'span<int, \[...\]>'"]
  170. int WontCompile() {
  171. const std::vector<int> v;
  172. span<int> s = make_span(v);
  173. }
  174. #elif defined(NCTEST_STATIC_MAKE_SPAN_FROM_ITER_AND_SIZE_CHECKS) // [r"constexpr variable 'made_span' must be initialized by a constant expression"]
  175. // The static make_span<N>(begin, size) should CHECK whether N matches size.
  176. // This should result in compilation failures when evaluated at compile
  177. // time.
  178. int WontCompile() {
  179. constexpr StringPiece str = "Foo";
  180. // Intentional extent mismatch causing CHECK failure.
  181. constexpr auto made_span = make_span<2>(str.begin(), 3);
  182. }
  183. #elif defined(NCTEST_STATIC_MAKE_SPAN_FROM_ITERS_CHECKS_SIZE) // [r"constexpr variable 'made_span' must be initialized by a constant expression"]
  184. // The static make_span<N>(begin, end) should CHECK whether N matches end -
  185. // begin. This should result in compilation failures when evaluated at compile
  186. // time.
  187. int WontCompile() {
  188. constexpr StringPiece str = "Foo";
  189. // Intentional extent mismatch causing CHECK failure.
  190. constexpr auto made_span = make_span<2>(str.begin(), str.end());
  191. }
  192. #elif defined(NCTEST_STATIC_MAKE_SPAN_CHECKS_SIZE) // [r"constexpr variable 'made_span' must be initialized by a constant expression"]
  193. // The static make_span<N>(cont) should CHECK whether N matches size(cont). This
  194. // should result in compilation failures when evaluated at compile time.
  195. int WontCompile() {
  196. constexpr StringPiece str = "Foo";
  197. // Intentional extent mismatch causing CHECK failure.
  198. constexpr auto made_span = make_span<2>(str);
  199. }
  200. #elif defined(NCTEST_EXTENT_NO_DYNAMIC_EXTENT) // [r"EXTENT should only be used for containers with a static extent"]
  201. // EXTENT should not result in |dynamic_extent|, it should be a compile-time
  202. // error.
  203. void WontCompile() {
  204. std::vector<uint8_t> vector;
  205. static_assert(EXTENT(vector) == 0, "Should not compile");
  206. }
  207. #elif defined(NCTEST_DANGLING_STD_ARRAY) // [r"object backing the pointer will be destroyed at the end of the full-expression"]
  208. void WontCompile() {
  209. span<const int, 3> s{std::array<int, 3>()};
  210. (void)s;
  211. }
  212. #elif defined(NCTEST_DANGLING_CONTAINER) // [r"object backing the pointer will be destroyed at the end of the full-expression"]
  213. void WontCompile() {
  214. span<const int> s{std::vector<int>({1, 2, 3})};
  215. (void)s;
  216. }
  217. #endif
  218. } // namespace base