Debian Codegraph @

clang/test/AST/ByteCode/libcxx/minmax.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c -fexperimental-new-constant-interpreter -verify=expected,both %s
2 // RUN: %clang_cc1 -std=c++2c -verify=ref,both %s
3
4 /// This used to cause an assertion failure because we were deallocating a
5 /// dynamic block that was already dead.
6
7 namespace std {
8 inline namespace __1 {
9 template <class _Tp, _Tp __v> struct integral_constant {
10 static inline constexpr const _Tp value = __v;
11 };
12 typedef integral_constant<bool, true> true_type;
13 typedef integral_constant<bool, false> false_type;
14 template <bool _Val> using _BoolConstant = integral_constant<bool, _Val>;
15 template <class _Tp> using __remove_cv_t = __remove_cv(_Tp);
16 template <class _Tp> using remove_cv_t = __remove_cv_t<_Tp>;
17 template <class _Tp>
18 inline constexpr bool is_lvalue_reference_v = __is_lvalue_reference(_Tp);
19 template <class _Tp>
20 using __libcpp_remove_reference_t = __remove_reference_t(_Tp);
21 template <class _Tp>
22 constexpr _Tp &&forward(__libcpp_remove_reference_t<_Tp> &__t) noexcept;
23 template <bool _Bp, class _If, class _Then> struct conditional {
24 using type = _If;
25 };
26 template <bool _Bp, class _IfRes, class _ElseRes>
27 using conditional_t = typename conditional<_Bp, _IfRes, _ElseRes>::type;
28 template <class _Tp, class _Up>
29 using _IsSame = _BoolConstant<__is_same(_Tp, _Up)>;
30 using size_t = decltype(sizeof(int));
31 template <class _Tp> using __decay_t = __decay(_Tp);
32 template <class _Tp> using decay_t = __decay_t<_Tp>;
33 template <bool, class _Tp = void> struct enable_if;
34 template <class _Tp> struct enable_if<true, _Tp> {
35 typedef _Tp type;
36 };
37 template <bool _Bp, class _Tp = void>
38 using __enable_if_t = typename enable_if<_Bp, _Tp>::type;
39 template <class _Bp, class _Dp>
40 inline constexpr bool is_base_of_v = __is_base_of(_Bp, _Dp);
41 template <class _Tp> _Tp &&__declval(int);
42 template <class _Tp> decltype(std::__declval<_Tp>(0)) declval() noexcept;
43 template <class _Ret, class _Fp, class... _Args> struct __invokable_r {};
44 template <class _Fp, class... _Args>
45 using __is_invocable = __invokable_r<void, _Fp, _Args...>;
46 template <class _Func, class... _Args>
47 inline const bool __is_invocable_v = __is_invocable<_Func, _Args...>::value;
48 template <class _Func, class... _Args>
49 struct __invoke_result
50 : enable_if<__is_invocable_v<_Func, _Args...>,
51 typename __invokable_r<void, _Func, _Args...>::_Result> {};
52 template <class _Fn, class... _Args>
53 struct invoke_result : __invoke_result<_Fn, _Args...> {};
54 template <class _Tp, class... _Args>
55 inline constexpr bool is_constructible_v = __is_constructible(_Tp, _Args...);
56 template <class _Tp>
57 constexpr __libcpp_remove_reference_t<_Tp> &&move(_Tp &&__t) noexcept;
58 template <class _Tp> inline constexpr bool is_enum_v = __is_enum(_Tp);
59 template <class _From, class _To>
60 inline constexpr bool is_convertible_v = __is_convertible(_From, _To);
61 template <class _From, class _To>
62 concept convertible_to = is_convertible_v<_From, _To> &&
63 requires { static_cast<_To>(std::declval<_From>()); };
64 template <class _Tp, class _Up>
65 concept __same_as_impl = _IsSame<_Tp, _Up>::value;
66 template <class _Tp, class _Up>
67 concept same_as = __same_as_impl<_Tp, _Up> && __same_as_impl<_Up, _Tp>;
68 template <class _Tp> using __remove_cvref_t = __remove_cvref(_Tp);
69 template <class _Tp> using remove_cvref_t = __remove_cvref_t<_Tp>;
70 template <class _Tp>
71 using __make_const_lvalue_ref = const __libcpp_remove_reference_t<_Tp> &;
72 template <class _Lhs, class _Rhs>
73 concept assignable_from =
74 is_lvalue_reference_v<_Lhs> &&
75 requires(_Lhs __lhs, _Rhs &&__rhs) {
76 { __lhs = std::forward<_Rhs>(__rhs) } -> same_as<_Lhs>;
77 };
78 template <class _Tp>
79 struct is_nothrow_destructible
80 : integral_constant<bool, __is_nothrow_destructible(_Tp)> {};
81 template <class _Tp>
82 inline constexpr bool is_nothrow_destructible_v =
83 is_nothrow_destructible<_Tp>::value;
84 template <class _Tp>
85 concept destructible = is_nothrow_destructible_v<_Tp>;
86 template <class _Tp, class... _Args>
87 concept constructible_from =
88 destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
89 template <class _Tp>
90 concept __default_initializable = requires { ::new _Tp; };
91 template <class _Tp>
92 concept default_initializable = constructible_from<_Tp> && requires {
93 _Tp{};
94 } && __default_initializable<_Tp>;
95 template <class _Tp>
96 concept move_constructible =
97 constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
98 template <class _Tp> inline constexpr bool is_class_v = __is_class(_Tp);
99 template <class _Tp> inline constexpr bool is_union_v = __is_union(_Tp);
100 template <class _Tp>
101 concept __class_or_enum = is_class_v<_Tp> || is_union_v<_Tp> || is_enum_v<_Tp>;
102 namespace ranges {
103 namespace __swap {
104 template <class _Tp, class _Up>
105 concept __unqualified_swappable_with =
106 (__class_or_enum<remove_cvref_t<_Tp>> ||
107 __class_or_enum<remove_cvref_t<_Up>>) &&
108 requires(_Tp &&__t, _Up &&__u) {
109 swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));
110 };
111 template <class _Tp>
112 concept __exchangeable = !__unqualified_swappable_with<_Tp &, _Tp &> &&
113 move_constructible<_Tp> && assignable_from<_Tp &, _Tp>;
114 struct __fn {
115 template <__exchangeable _Tp>
116 constexpr void operator()(_Tp &__x, _Tp &__y) const;
117 };
118 } // namespace __swap
119 inline namespace __cpo {
120 inline constexpr auto swap = __swap::__fn{};
121 }
122 } // namespace ranges
123 template <class _Tp> inline constexpr bool is_object_v = __is_object(_Tp);
124 template <class _Tp>
125 concept movable = is_object_v<_Tp> && move_constructible<_Tp> &&
126 assignable_from<_Tp &, _Tp>;
127 template <class _Tp>
128 concept copyable =
129 assignable_from<_Tp &, const _Tp &> && assignable_from<_Tp &, const _Tp>;
130 template <class _Dp, class _Bp>
131 concept derived_from =
132 is_base_of_v<_Bp, _Dp> &&
133 is_convertible_v<const volatile _Dp *, const volatile _Bp *>;
134 template <class _Tp>
135 concept __boolean_testable_impl = convertible_to<_Tp, bool>;
136 template <class _Tp>
137 concept __boolean_testable =
138 __boolean_testable_impl<_Tp> && requires(_Tp &&__t) {
139 { !std::forward<_Tp>(__t) } -> __boolean_testable_impl;
140 };
141 template <class _Tp, class _Up>
142 concept __weakly_equality_comparable_with = requires(
143 __make_const_lvalue_ref<_Tp> __t, __make_const_lvalue_ref<_Up> __u) {
144 { __u != __t } -> __boolean_testable;
145 };
146 template <class _Tp>
147 concept semiregular = copyable<_Tp> && default_initializable<_Tp>;
148 template <template <class...> class _Templ, class... _Args,
149 class = _Templ<_Args...>>
150 true_type __sfinae_test_impl(int);
151 template <template <class...> class, class...>
152 false_type __sfinae_test_impl(...);
153 template <template <class...> class _Templ, class... _Args>
154 using _IsValidExpansion =
155 decltype(std::__sfinae_test_impl<_Templ, _Args...>(0));
156 template <class _Tp>
157 using __test_for_primary_template =
158 __enable_if_t<_IsSame<_Tp, typename _Tp::__primary_template>::value>;
159 template <class _Tp>
160 using __is_primary_template =
161 _IsValidExpansion<__test_for_primary_template, _Tp>;
162 template <class> struct iterator_traits;
163 template <class> struct __cond_value_type;
164 template <class _Tp>
165 requires is_object_v<_Tp>
166 struct __cond_value_type<_Tp> {
167 using value_type = remove_cv_t<_Tp>;
168 };
169 template <class _Tp>
170 concept __has_member_value_type = requires { typename _Tp::value_type; };
171 template <class> struct indirectly_readable_traits;
172 template <class _Tp>
173 struct indirectly_readable_traits<_Tp *> : __cond_value_type<_Tp> {};
174 template <__has_member_value_type _Tp>
175 struct indirectly_readable_traits<_Tp>
176 : __cond_value_type<typename _Tp::value_type> {};
177 template <bool> struct _OrImpl;
178 template <> struct _OrImpl<true> {
179 template <class _Res, class _First, class... _Rest>
180 using _Result =
181 typename _OrImpl<!bool(_First::value) && sizeof...(_Rest) != 0>::
182 template _Result<_First, _Rest...>;
183 };
184 template <> struct _OrImpl<false> {
185 template <class _Res, class...> using _Result = _Res;
186 };
187 template <class... _Args>
188 using _Or = typename _OrImpl<sizeof...(_Args) !=
189 0>::template _Result<false_type, _Args...>;
190 template <class _Tp> using __with_reference = _Tp &;
191 template <class _Tp>
192 concept __can_reference = requires { typename __with_reference<_Tp>; };
193 template <class _Tp>
194 concept __dereferenceable = requires(_Tp &__t) {
195 { *__t } -> __can_reference;
196 };
197 template <__dereferenceable _Tp>
198 using iter_reference_t = decltype(*std::declval<_Tp &>());
199 struct input_iterator_tag {};
200 struct forward_iterator_tag : public input_iterator_tag {};
201 struct bidirectional_iterator_tag : public forward_iterator_tag {};
202 struct random_access_iterator_tag : public bidirectional_iterator_tag {};
203 template <class _Iter> struct __iter_traits_cache;
204 template <class _Iter>
205 using _ITER_TRAITS = typename __iter_traits_cache<_Iter>::type;
206 struct __iter_concept_concept_test {
207 template <class _Iter>
208 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_concept;
209 };
210 struct __iter_concept_category_test {
211 template <class _Iter>
212 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_category;
213 };
214 struct __iter_concept_random_fallback {
215 template <class _Iter>
216 using _Apply =
217 __enable_if_t<__is_primary_template<iterator_traits<_Iter>>::value,
218 random_access_iterator_tag>;
219 };
220 template <class _Iter, class _Tester>
221 struct __test_iter_concept : _IsValidExpansion<_Tester::template _Apply, _Iter>,
222 _Tester {};
223 template <class _Iter> struct __iter_concept_cache {
224 using type = _Or<__test_iter_concept<_Iter, __iter_concept_concept_test>,
225 __test_iter_concept<_Iter, __iter_concept_category_test>,
226 __test_iter_concept<_Iter, __iter_concept_random_fallback>>;
227 };
228 template <class _Iter>
229 using _ITER_CONCEPT =
230 typename __iter_concept_cache<_Iter>::type::template _Apply<_Iter>;
231 template <class _Ip> struct iterator_traits {
232 using __primary_template = iterator_traits;
233 };
234 template <class _Ip>
235 using iter_value_t = typename conditional_t<
236 __is_primary_template<iterator_traits<remove_cvref_t<_Ip>>>::value,
237 indirectly_readable_traits<remove_cvref_t<_Ip>>,
238 iterator_traits<remove_cvref_t<_Ip>>>::value_type;
239 namespace ranges {
240 namespace __iter_move {
241 template <class _Tp>
242 concept __move_deref = requires(_Tp &&__t) {
243 requires is_lvalue_reference_v<decltype(*__t)>;
244 };
245 struct __fn {
246 template <class _Ip>
247 requires __move_deref<_Ip>
248 constexpr auto operator()(_Ip &&__i) const
249 -> decltype(std::move(*std::forward<_Ip>(__i))) {
250 return std::move(*std::forward<_Ip>(__i));
251 }
252 template <class _Ip>
253 constexpr auto operator()(_Ip &&__i) const
254 -> decltype(*std::forward<_Ip>(__i));
255 };
256 } // namespace __iter_move
257 inline namespace __cpo {
258 inline constexpr auto iter_move = __iter_move::__fn{};
259 }
260 } // namespace ranges
261 template <__dereferenceable _Tp>
262 requires requires(_Tp &__t) {
263 { ranges::iter_move(__t) } -> __can_reference;
264 }
265 using iter_rvalue_reference_t =
266 decltype(ranges::iter_move(std::declval<_Tp &>()));
267 template <class _Ptr, class = void> struct __pointer_traits_impl {};
268 template <class _Tp> inline constexpr bool is_pointer_v = __is_pointer(_Tp);
269 template <class _Ip>
270 concept input_or_output_iterator = requires(_Ip __i) {
271 { *__i } -> __can_reference;
272 };
273 template <class _Sp, class _Ip>
274 concept sentinel_for = semiregular<_Sp> && input_or_output_iterator<_Ip> &&
275 __weakly_equality_comparable_with<_Sp, _Ip>;
276 template <class _Ip>
277 concept input_iterator =
278 input_or_output_iterator<_Ip> && requires {
279 typename _ITER_CONCEPT<_Ip>;
280 } && derived_from<_ITER_CONCEPT<_Ip>, input_iterator_tag>;
281 namespace ranges {
282 namespace __begin {
283 struct __fn {
284 template <class _Tp> constexpr auto operator()(_Tp &&__t) const {
285 return static_cast<::std::__decay_t<decltype((__t.begin()))>>(__t.begin());
286 }
287 };
288 } // namespace __begin
289 inline namespace __cpo {
290 inline constexpr auto begin = __begin::__fn{};
291 }
292 template <class _Tp>
293 using iterator_t = decltype(ranges::begin(std::declval<_Tp &>()));
294 namespace __end {
295 struct __fn {
296 template <class _Tp> constexpr auto operator()(_Tp &&__t) const {}
297 };
298 } // namespace __end
299 inline namespace __cpo {
300 inline constexpr auto end = __end::__fn{};
301 }
302 } // namespace ranges
303 namespace ranges {
304 template <class _Tp>
305 concept range = requires(_Tp &__t) { ranges::end(__t); };
306 template <class _Tp>
307 concept input_range = range<_Tp> && input_iterator<iterator_t<_Tp>>;
308 template <range _Rp> using range_value_t = iter_value_t<iterator_t<_Rp>>;
309 } // namespace ranges
310 } // namespace __1
311 } // namespace std
312 constexpr void *operator new(std::size_t, void *__p) noexcept; // both-warning {{not defined}}
313 namespace std {
314 inline namespace __1 {
315 template <class _Tp, class... _Args,
316 class = decltype(::new (std::declval<void *>())
317 _Tp(std::declval<_Args>()...))>
318 constexpr _Tp *construct_at(_Tp *__location, _Args &&...__args) {
319 return ::new (static_cast<void *>(__location)) _Tp(std::forward<_Args>(__args)...); // both-note {{here}}
320 }
321 enum class __element_count : size_t;
322 template <class _Tp, class _Up>
323 constexpr _Tp *__constexpr_memmove(_Tp *__dest, _Up *__src,
324 __element_count __n) {
325 size_t __count = static_cast<size_t>(__n);
326 ::__builtin_memcpy(__dest, __src, __count * sizeof(_Tp));
327 return __dest;
328 };
329 namespace ranges {
330 template <class _From, class _To>
331 concept __convertible_to_non_slicing =
332 convertible_to<_From, _To>;
333 template <input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent = _Iter>
334 class subrange {
335 private:
336 _Iter __begin_ = _Iter();
337 _Sent __end_ = _Sent();
338
339 public:
340 constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter,
341 _Sent __sent)
342 : __begin_(std::move(__iter)), __end_(std::move(__sent)) {}
343 constexpr _Iter begin() { return std::move(__begin_); }
344 constexpr _Sent end() const;
345 };
346 template <input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent>
347 subrange(_Iter, _Sent) -> subrange<_Iter, _Sent>;
348 } // namespace ranges
349 template <class _Tp> class allocator {
350 public:
351 constexpr _Tp *allocate(size_t __n) {
352 return static_cast<_Tp *>(::operator new(__n * sizeof(_Tp)));
353 }
354 constexpr void deallocate(_Tp *__p, size_t __n) noexcept {
355 ::operator delete(__p);
356 }
357 };
358 template <class _CharT> struct char_traits;
359 template <class _CharT, class _Traits = char_traits<_CharT>,
360 class _Allocator = allocator<_CharT>>
361 class basic_string;
362 using string = basic_string<char>;
363 template <class _Iter>
364 class move_iterator {
365 private:
366 public:
367 using iterator_type = _Iter;
368 using value_type = iter_value_t<_Iter>;
369 using reference = iter_rvalue_reference_t<_Iter>;
370 constexpr explicit move_iterator(_Iter __i) : __current_(std::move(__i)) {}
371 constexpr move_iterator();
372 constexpr reference operator*() const {
373 return ranges::iter_move(__current_);
374 }
375 _Iter __current_;
376 };
377 namespace ranges {
378 template <class _T1> struct min_max_result {
379 _T1 min;
380 };
381 template <class _T1> using minmax_result = min_max_result<_T1>;
382 struct __minmax {
383 template <input_range _Range>
384 constexpr ranges::minmax_result<range_value_t<_Range>>
385 operator()(_Range &&__r) const {
386 auto __first = ranges::begin(__r);
387 using _ValueT = range_value_t<_Range>;
388 ranges::minmax_result<_ValueT> __result = {*__first};
389 return __result;
390 }
391 };
392 inline namespace __cpo {
393 inline constexpr auto minmax = __minmax{};
394 }
395 } // namespace ranges
396 template <> struct char_traits<char> {
397 using char_type = char;
398 static constexpr int compare(const char_type *__lhs, const char_type *__rhs,
399 size_t __count) noexcept {
400 return __builtin_memcmp(__lhs, __rhs, __count);
401 }
402 static inline size_t constexpr length(const char_type *__s) noexcept {
403 return __builtin_strlen(__s);
404 }
405 static inline constexpr char_type *
406 copy(char_type *__s1, const char_type *__s2, size_t __n) noexcept {
407 std::__constexpr_memmove(__s1, __s2, __element_count(__n));
408 return __s1;
409 }
410 };
411 template <class _CharT, class _Traits, class _Allocator> class basic_string {
412 using traits_type = _Traits;
413 using value_type = _CharT;
414 using allocator_type = _Allocator;
415
416 private:
417 struct __long {
418 struct {
419 unsigned __is_long_ : 1;
420 unsigned __cap_ : sizeof(unsigned) * 8 - 1;
421 };
422 unsigned __size_;
423 char *__data_;
424 };
425 struct __short {
426 struct {
427 unsigned char __is_long_ : 1;
428 };
429 };
430 union __rep {
431 __short __s;
432 __long __l;
433 };
434 __rep __rep_;
435 allocator_type __alloc_;
436
437 public:
438 constexpr basic_string(const basic_string &__str) : __alloc_(__str.__alloc_) {
439 __init_copy_ctor_external(__str.__rep_.__l.__data_,
440 __str.__rep_.__l.__size_);
441 }
442 constexpr basic_string(basic_string &&__str)
443 : __rep_(__str.__rep_), __alloc_(std::move(__str.__alloc_)) {
444 __str.__rep_ = __rep();
445 }
446 constexpr basic_string(const _CharT *__s) {
447 __init_copy_ctor_external(__s, traits_type::length(__s));
448 }
449 inline constexpr ~basic_string() {
450 if (__is_long())
451 __alloc_.deallocate(__rep_.__l.__data_, __rep_.__l.__cap_*2);
452 }
453 constexpr unsigned size() const noexcept { return __rep_.__l.__size_; }
454 constexpr const value_type *data() const noexcept { return __rep_.__l.__data_; }
455
456 private:
457 constexpr bool __is_long() const {
458 if (__builtin_constant_p(__rep_.__l.__is_long_)) {
459 return __rep_.__l.__is_long_;
460 }
461 return __rep_.__s.__is_long_;
462 }
463 static constexpr void __begin_lifetime(char *__begin, unsigned __n) {
464 for (unsigned __i = 0; __i != __n; ++__i)
465 std::construct_at(&__begin[__i]);
466 }
467 constexpr void __init_copy_ctor_external(const value_type *__s,
468 unsigned __sz);
469 };
470 template <class _CharT, class _Traits, class _Allocator>
471 constexpr void
472 basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
473 const value_type *__s, unsigned __sz) {
474 char *__p;
475 auto __allocation = __alloc_.allocate(__sz + 1);
476 __p = __allocation;
477 __begin_lifetime(__p, __sz + 1);
478 __rep_.__l.__data_ = __p;
479 __rep_.__l.__cap_ = (__sz + 1) / 2;
480 __rep_.__l.__is_long_ = true;
481 __rep_.__l.__size_ = __sz;
482 traits_type::copy(__p, __s, __sz + 1);
483 }
484 template <class _CharT, class _Traits, class _Allocator>
485 inline constexpr bool
486 operator==(const basic_string<_CharT, _Traits, _Allocator> &__lhs,
487 const basic_string<_CharT, _Traits, _Allocator> &__rhs) noexcept {
488 return _Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()) == 0;
489 }
490 } // namespace __1
491 } // namespace std
492 template <class It> class cpp20_input_iterator {
493 It it_;
494
495 public:
496 using value_type = std::iter_value_t<It>;
497 constexpr explicit cpp20_input_iterator(It it) : it_(it) {}
498 constexpr decltype(auto) operator*() const { return *it_; }
499 constexpr void operator++(int);
500 friend constexpr It base(const cpp20_input_iterator &i) { return i.it_; }
501 };
502 template <class It> class sentinel_wrapper {
503 public:
504 explicit sentinel_wrapper() = default;
505 constexpr explicit sentinel_wrapper(const It &it) : base_(base(it)) {}
506 constexpr bool operator==(const It &other) const;
507
508 private:
509 decltype(base(std::declval<It>())) base_;
510 };
511 constexpr bool test_range() {
512 const std::string str{
513 "this long string will be dynamically "
514 "allocatedasfajkshdfasdhjkfahjksdgfjkasdhkfgaksdgfhkasghjkdfgjhasdghjfgah"
515 "jksdgjfhkasghjkdfghjasgjdfghasdghjkfgajksdgfgajsdfghjkgashjkdfghjasjdhkf"
516 "gahjsdfghjkgashjdghjfkasghjkdfasgjkdhfgajkshdfgkjashdgfkjasghdfkjghasdkf"
517 "hgaskdjghfasdfasdfasdfasdfasdf"};
518 std::string a[] = {str};
519 auto range = std::ranges::subrange(
520 cpp20_input_iterator(std::move_iterator(a)),
521 sentinel_wrapper(cpp20_input_iterator(std::move_iterator(a + 1))));
522 auto ret = std::ranges::minmax(range);
523 if (ret.min != str)
524 __builtin_abort();
525 return true;
526 }
527 static_assert(test_range());