Debian Codegraph @

gcc/testsuite/rust/compile/iterators1.rs

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