]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/tests/run/slice.rs
Auto merge of #92419 - erikdesjardins:coldland, r=nagisa
[rust.git] / compiler / rustc_codegen_gcc / tests / run / slice.rs
1 // Compiler:
2 //
3 // Run-time:
4 //   status: 0
5 //   stdout: 5
6
7 #![feature(arbitrary_self_types, auto_traits, lang_items, no_core, start, intrinsics)]
8
9 #![no_std]
10 #![no_core]
11
12 /*
13  * Core
14  */
15
16 // Because we don't have core yet.
17 #[lang = "sized"]
18 pub trait Sized {}
19
20 #[lang = "copy"]
21 trait Copy {
22 }
23
24 impl Copy for isize {}
25 impl Copy for usize {}
26 impl Copy for i32 {}
27 impl Copy for u32 {}
28
29 #[lang = "receiver"]
30 trait Receiver {
31 }
32
33 #[lang = "freeze"]
34 pub(crate) unsafe auto trait Freeze {}
35
36 mod libc {
37     #[link(name = "c")]
38     extern "C" {
39         pub fn printf(format: *const i8, ...) -> i32;
40     }
41 }
42
43 #[lang = "index"]
44 pub trait Index<Idx: ?Sized> {
45     type Output: ?Sized;
46     fn index(&self, index: Idx) -> &Self::Output;
47 }
48
49 impl<T> Index<usize> for [T; 3] {
50     type Output = T;
51
52     fn index(&self, index: usize) -> &Self::Output {
53         &self[index]
54     }
55 }
56
57 impl<T> Index<usize> for [T] {
58     type Output = T;
59
60     fn index(&self, index: usize) -> &Self::Output {
61         &self[index]
62     }
63 }
64
65 #[lang = "unsize"]
66 pub trait Unsize<T: ?Sized> {}
67
68 #[lang = "coerce_unsized"]
69 pub trait CoerceUnsized<T> {}
70
71 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
72 impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
73 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
74 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
75
76 #[lang = "drop_in_place"]
77 #[allow(unconditional_recursion)]
78 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
79     // Code here does not matter - this is replaced by the
80     // real drop glue by the compiler.
81     drop_in_place(to_drop);
82 }
83
84 #[lang = "panic_location"]
85 struct PanicLocation {
86     file: &'static str,
87     line: u32,
88     column: u32,
89 }
90
91 #[lang = "panic_bounds_check"]
92 #[track_caller]
93 #[no_mangle]
94 fn panic_bounds_check(index: usize, len: usize) -> ! {
95     unsafe {
96         libc::printf("index out of bounds: the len is %d but the index is %d\n\0" as *const str as *const i8, len, index);
97         intrinsics::abort();
98     }
99 }
100
101 mod intrinsics {
102     use super::Sized;
103
104     extern "rust-intrinsic" {
105         pub fn abort() -> !;
106     }
107 }
108
109 /*
110  * Code
111  */
112
113 static mut TWO: usize = 2;
114
115 fn index_slice(s: &[u32]) -> u32 {
116     unsafe {
117         s[TWO]
118     }
119 }
120
121 #[start]
122 fn main(mut argc: isize, _argv: *const *const u8) -> isize {
123     let array = [42, 7, 5];
124     unsafe {
125         libc::printf(b"%ld\n\0" as *const u8 as *const i8, index_slice(&array));
126     }
127     0
128 }