Debian Codegraph @

gcc/testsuite/rust/execute/torture/iter1.rs

raw
1 // { dg-output "1\r*\n2\r*\n" }
2 #![feature(intrinsics)]
3
4 pub use option::Option::{self, None, Some};
5 pub use result::Result::{self, Err, Ok};
6
7 extern "C" {
8 fn printf(s: *const i8, ...);
9 }
10
11 mod option {
12 pub enum Option<T> {
13 None,
14 Some(T),
15 }
16 }
17
18 mod result {
19 enum Result<T, E> {
20 Ok(T),
21 Err(E),
22 }
23 }
24
25 #[lang = "sized"]
26 pub trait Sized {}
27
28 #[lang = "clone"]
29 pub trait Clone: Sized {
30 fn clone(&self) -> Self;
31
32 fn clone_from(&mut self, source: &Self) {
33 *self = source.clone()
34 }
35 }
36
37 mod impls {
38 use super::Clone;
39
40 macro_rules! impl_clone {
41 ($($t:ty)*) => {
42 $(
43 impl Clone for $t {
44 fn clone(&self) -> Self {
45 *self
46 }
47 }
48 )*
49 }
50 }
51
52 impl_clone! {
53 usize u8 u16 u32 u64 // u128
54 isize i8 i16 i32 i64 // i128
55 f32 f64
56 bool char
57 }
58 }
59
60 #[lang = "copy"]
61 pub trait Copy: Clone {
62 // Empty.
63 }
64
65 mod copy_impls {
66 use super::Copy;
67
68 macro_rules! impl_copy {
69 ($($t:ty)*) => {
70 $(
71 impl Copy for $t {}
72 )*
73 }
74 }
75
76 impl_copy! {
77 usize u8 u16 u32 u64 // u128
78 isize i8 i16 i32 i64 // i128
79 f32 f64
80 bool char
81 }
82 }
83
84 mod intrinsics {
85 extern "rust-intrinsic" {
86 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
87 pub fn wrapping_add<T>(a: T, b: T) -> T;
88 pub fn wrapping_sub<T>(a: T, b: T) -> T;
89 pub fn rotate_left<T>(a: T, b: T) -> T;
90 pub fn rotate_right<T>(a: T, b: T) -> T;
91 pub fn offset<T>(ptr: *const T, count: isize) -> *const T;
92 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
93 pub fn move_val_init<T>(dst: *mut T, src: T);
94 pub fn uninit<T>() -> T;
95 }
96 }
97
98 mod ptr {
99 #[lang = "const_ptr"]
100 impl<T> *const T {
101 pub unsafe fn offset(self, count: isize) -> *const T {
102 intrinsics::offset(self, count)
103 }
104 }
105
106 #[lang = "mut_ptr"]
107 impl<T> *mut T {
108 pub unsafe fn offset(self, count: isize) -> *mut T {
109 intrinsics::offset(self, count) as *mut T
110 }
111 }
112
113 pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
114 let x = x as *mut u8;
115 let y = y as *mut u8;
116 let len = mem::size_of::<T>() * count;
117 swap_nonoverlapping_bytes(x, y, len)
118 }
119
120 pub unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) {
121 // For types smaller than the block optimization below,
122 // just swap directly to avoid pessimizing codegen.
123 if mem::size_of::<T>() < 32 {
124 let z = read(x);
125 intrinsics::copy_nonoverlapping(y, x, 1);
126 write(y, z);
127 } else {
128 swap_nonoverlapping(x, y, 1);
129 }
130 }
131
132 pub unsafe fn write<T>(dst: *mut T, src: T) {
133 intrinsics::move_val_init(&mut *dst, src)
134 }
135
136 pub unsafe fn read<T>(src: *const T) -> T {
137 let mut tmp: T = mem::uninitialized();
138 intrinsics::copy_nonoverlapping(src, &mut tmp, 1);
139 tmp
140 }
141
142 unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
143 struct Block(u64, u64, u64, u64);
144 struct UnalignedBlock(u64, u64, u64, u64);
145
146 let block_size = mem::size_of::<Block>();
147
148 // Loop through x & y, copying them `Block` at a time
149 // The optimizer should unroll the loop fully for most types
150 // N.B. We can't use a for loop as the `range` impl calls `mem::swap` recursively
151 let mut i: usize = 0;
152 while i + block_size <= len {
153 // Create some uninitialized memory as scratch space
154 // Declaring `t` here avoids aligning the stack when this loop is unused
155 let mut t: Block = mem::uninitialized();
156 let t = &mut t as *mut _ as *mut u8;
157 let x = x.offset(i as isize);
158 let y = y.offset(i as isize);
159
160 // Swap a block of bytes of x & y, using t as a temporary buffer
161 // This should be optimized into efficient SIMD operations where available
162 intrinsics::copy_nonoverlapping(x, t, block_size);
163 intrinsics::copy_nonoverlapping(y, x, block_size);
164 intrinsics::copy_nonoverlapping(t, y, block_size);
165 i += block_size;
166 }
167
168 if i < len {
169 // Swap any remaining bytes
170 let mut t: UnalignedBlock = mem::uninitialized();
171 let rem = len - i;
172
173 let t = &mut t as *mut _ as *mut u8;
174 let x = x.offset(i as isize);
175 let y = y.offset(i as isize);
176
177 intrinsics::copy_nonoverlapping(x, t, rem);
178 intrinsics::copy_nonoverlapping(y, x, rem);
179 intrinsics::copy_nonoverlapping(t, y, rem);
180 }
181 }
182 }
183
184 mod mem {
185 extern "rust-intrinsic" {
186 #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
187 pub fn transmute<T, U>(_: T) -> U;
188 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
189 pub fn size_of<T>() -> usize;
190 }
191
192 pub fn swap<T>(x: &mut T, y: &mut T) {
193 unsafe {
194 ptr::swap_nonoverlapping_one(x, y);
195 }
196 }
197
198 pub fn replace<T>(dest: &mut T, mut src: T) -> T {
199 swap(dest, &mut src);
200 src
201 }
202
203 pub unsafe fn uninitialized<T>() -> T {
204 intrinsics::uninit()
205 }
206 }
207
208 macro_rules! impl_uint {
209 ($($ty:ident = $lang:literal),*) => {
210 $(
211 impl $ty {
212 pub fn wrapping_add(self, rhs: Self) -> Self {
213 unsafe {
214 intrinsics::wrapping_add(self, rhs)
215 }
216 }
217
218 pub fn wrapping_sub(self, rhs: Self) -> Self {
219 unsafe {
220 intrinsics::wrapping_sub(self, rhs)
221 }
222 }
223
224 pub fn rotate_left(self, n: u32) -> Self {
225 unsafe {
226 intrinsics::rotate_left(self, n as Self)
227 }
228 }
229
230 pub fn rotate_right(self, n: u32) -> Self {
231 unsafe {
232 intrinsics::rotate_right(self, n as Self)
233 }
234 }
235
236 pub fn to_le(self) -> Self {
237 #[cfg(target_endian = "little")]
238 {
239 self
240 }
241 }
242
243 pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
244 Self::from_le(Self::from_ne_bytes(bytes))
245 }
246
247 pub const fn from_le(x: Self) -> Self {
248 #[cfg(target_endian = "little")]
249 {
250 x
251 }
252 }
253
254 pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
255 unsafe { mem::transmute(bytes) }
256 }
257
258 pub fn checked_add(self, rhs: Self) -> Option<Self> {
259 let (a, b) = self.overflowing_add(rhs);
260 if b {
261 Option::None
262 } else {
263 Option::Some(a)
264 }
265 }
266
267 pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
268 let (a, b) = unsafe { intrinsics::add_with_overflow(self as $ty, rhs as $ty) };
269 (a as Self, b)
270 }
271 }
272 )*
273 }
274 }
275
276 impl_uint!(
277 u8 = "u8",
278 u16 = "u16",
279 u32 = "u32",
280 u64 = "u64",
281 usize = "usize"
282 );
283
284 #[lang = "add"]
285 pub trait Add<RHS = Self> {
286 type Output;
287
288 fn add(self, rhs: RHS) -> Self::Output;
289 }
290 macro_rules! add_impl {
291 ($($t:ty)*) => ($(
292 impl Add for $t {
293 type Output = $t;
294
295 fn add(self, other: $t) -> $t { self + other }
296 }
297 )*)
298 }
299
300 add_impl! { usize u8 u16 u32 u64 /*isize i8 i16 i32 i64*/ f32 f64 }
301
302 #[lang = "sub"]
303 pub trait Sub<RHS = Self> {
304 type Output;
305
306 fn sub(self, rhs: RHS) -> Self::Output;
307 }
308 macro_rules! sub_impl {
309 ($($t:ty)*) => ($(
310 impl Sub for $t {
311 type Output = $t;
312
313 fn sub(self, other: $t) -> $t { self - other }
314 }
315 )*)
316 }
317
318 sub_impl! { usize u8 u16 u32 u64 /*isize i8 i16 i32 i64*/ f32 f64 }
319
320 #[lang = "Range"]
321 pub struct Range<Idx> {
322 pub start: Idx,
323 pub end: Idx,
324 }
325
326 pub trait TryFrom<T>: Sized {
327 /// The type returned in the event of a conversion error.
328 type Error;
329
330 /// Performs the conversion.
331 fn try_from(value: T) -> Result<Self, Self::Error>;
332 }
333
334 pub trait From<T>: Sized {
335 fn from(_: T) -> Self;
336 }
337
338 impl<T> From<T> for T {
339 fn from(t: T) -> T {
340 t
341 }
342 }
343
344 impl<T, U> TryFrom<U> for T
345 where
346 T: From<U>,
347 {
348 type Error = !;
349
350 fn try_from(value: U) -> Result<Self, Self::Error> {
351 Ok(T::from(value))
352 }
353 }
354
355 trait Step {
356 /// Returns the number of steps between two step objects. The count is
357 /// inclusive of `start` and exclusive of `end`.
358 ///
359 /// Returns `None` if it is not possible to calculate `steps_between`
360 /// without overflow.
361 fn steps_between(start: &Self, end: &Self) -> Option<usize>;
362
363 /// Replaces this step with `1`, returning itself
364 fn replace_one(&mut self) -> Self;
365
366 /// Replaces this step with `0`, returning itself
367 fn replace_zero(&mut self) -> Self;
368
369 /// Adds one to this step, returning the result
370 fn add_one(&self) -> Self;
371
372 /// Subtracts one to this step, returning the result
373 fn sub_one(&self) -> Self;
374
375 /// Add an usize, returning None on overflow
376 fn add_usize(&self, n: usize) -> Option<Self>;
377 }
378
379 // These are still macro-generated because the integer literals resolve to different types.
380 macro_rules! step_identical_methods {
381 () => {
382 #[inline]
383 fn replace_one(&mut self) -> Self {
384 mem::replace(self, 1)
385 }
386
387 #[inline]
388 fn replace_zero(&mut self) -> Self {
389 mem::replace(self, 0)
390 }
391
392 #[inline]
393 fn add_one(&self) -> Self {
394 Add::add(*self, 1)
395 }
396
397 #[inline]
398 fn sub_one(&self) -> Self {
399 Sub::sub(*self, 1)
400 }
401 };
402 }
403
404 macro_rules! step_impl_unsigned {
405 ($($t:ty)*) => ($(
406 impl Step for $t {
407 fn steps_between(start: &$t, end: &$t) -> Option<usize> {
408 if *start < *end {
409 // Note: We assume $t <= usize here
410 Option::Some((*end - *start) as usize)
411 } else {
412 Option::Some(0)
413 }
414 }
415
416 fn add_usize(&self, n: usize) -> Option<Self> {
417 match <$t>::try_from(n) {
418 Result::Ok(n_as_t) => self.checked_add(n_as_t),
419 Result::Err(_) => Option::None,
420 }
421 }
422
423 step_identical_methods!();
424 }
425 )*)
426 }
427 macro_rules! step_impl_signed {
428 ($( [$t:ty : $unsigned:ty] )*) => ($(
429 impl Step for $t {
430 #[inline]
431 #[allow(trivial_numeric_casts)]
432 fn steps_between(start: &$t, end: &$t) -> Option<usize> {
433 if *start < *end {
434 // Note: We assume $t <= isize here
435 // Use .wrapping_sub and cast to usize to compute the
436 // difference that may not fit inside the range of isize.
437 Option::Some((*end as isize).wrapping_sub(*start as isize) as usize)
438 } else {
439 Option::Some(0)
440 }
441 }
442
443 #[inline]
444 #[allow(unreachable_patterns)]
445 fn add_usize(&self, n: usize) -> Option<Self> {
446 match <$unsigned>::try_from(n) {
447 Result::Ok(n_as_unsigned) => {
448 // Wrapping in unsigned space handles cases like
449 // `-120_i8.add_usize(200) == Option::Some(80_i8)`,
450 // even though 200_usize is out of range for i8.
451 let wrapped = (*self as $unsigned).wrapping_add(n_as_unsigned) as $t;
452 if wrapped >= *self {
453 Option::Some(wrapped)
454 } else {
455 Option::None // Addition overflowed
456 }
457 }
458 Result::Err(_) => Option::None,
459 }
460 }
461
462 step_identical_methods!();
463 }
464 )*)
465 }
466
467 macro_rules! step_impl_no_between {
468 ($($t:ty)*) => ($(
469 impl Step for $t {
470 #[inline]
471 fn steps_between(_start: &Self, _end: &Self) -> Option<usize> {
472 Option::None
473 }
474
475 #[inline]
476 fn add_usize(&self, n: usize) -> Option<Self> {
477 self.checked_add(n as $t)
478 }
479
480 step_identical_methods!();
481 }
482 )*)
483 }
484
485 step_impl_unsigned!(usize);
486
487 pub trait Iterator {
488 type Item;
489
490 fn next(&mut self) -> Option<Self::Item>;
491 }
492
493 impl<A: Step> Iterator for Range<A> {
494 type Item = A;
495
496 fn next(&mut self) -> Option<A> {
497 if self.start < self.end {
498 // We check for overflow here, even though it can't actually
499 // happen. Adding this check does however help llvm vectorize loops
500 // for some ranges that don't get vectorized otherwise,
501 // and this won't actually result in an extra check in an optimized build.
502 match self.start.add_usize(1) {
503 Option::Some(mut n) => {
504 mem::swap(&mut n, &mut self.start);
505 Option::Some(n)
506 }
507 Option::None => Option::None,
508 }
509 } else {
510 Option::None
511 }
512 }
513 }
514
515 pub trait IntoIterator {
516 type Item;
517
518 type IntoIter: Iterator<Item = Self::Item>;
519
520 fn into_iter(self) -> Self::IntoIter;
521 }
522
523 impl<I: Iterator> IntoIterator for I {
524 type Item = I::Item;
525 type IntoIter = I;
526
527 fn into_iter(self) -> I {
528 self
529 }
530 }
531
532 pub fn main() -> i32 {
533 let a = 1..3;
534 let it = a.into_iter();
535 loop {
536 let n = it.next();
537 match (n) {
538 Option::Some(v) => unsafe {
539 let a = "%d\n\0";
540 let b = a as *const str;
541 let c = b as *const i8;
542
543 printf(c, v);
544 },
545 Option::None => {
546 break;
547 }
548 }
549 }
550
551 0
552 }