Debian Codegraph @

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