]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/tests/run/array.rs
Auto merge of #92419 - erikdesjardins:coldland, r=nagisa
[rust.git] / compiler / rustc_codegen_gcc / tests / run / array.rs
1 // Compiler:
2 //
3 // Run-time:
4 //   status: 0
5 //   stdout: 42
6 //     7
7 //     5
8 //     10
9
10 #![feature(arbitrary_self_types, auto_traits, lang_items, no_core, start, intrinsics)]
11
12 #![no_std]
13 #![no_core]
14
15 /*
16  * Core
17  */
18
19 // Because we don't have core yet.
20 #[lang = "sized"]
21 pub trait Sized {}
22
23 #[lang = "copy"]
24 trait Copy {
25 }
26
27 impl Copy for isize {}
28 impl Copy for usize {}
29 impl Copy for i32 {}
30 impl Copy for u8 {}
31 impl Copy for i8 {}
32 impl Copy for i16 {}
33
34 #[lang = "receiver"]
35 trait Receiver {
36 }
37
38 #[lang = "freeze"]
39 pub(crate) unsafe auto trait Freeze {}
40
41 mod libc {
42     #[link(name = "c")]
43     extern "C" {
44         pub fn printf(format: *const i8, ...) -> i32;
45         pub fn puts(s: *const u8) -> i32;
46     }
47 }
48
49 #[lang = "index"]
50 pub trait Index<Idx: ?Sized> {
51     type Output: ?Sized;
52     fn index(&self, index: Idx) -> &Self::Output;
53 }
54
55 impl<T> Index<usize> for [T; 3] {
56     type Output = T;
57
58     fn index(&self, index: usize) -> &Self::Output {
59         &self[index]
60     }
61 }
62
63 impl<T> Index<usize> for [T] {
64     type Output = T;
65
66     fn index(&self, index: usize) -> &Self::Output {
67         &self[index]
68     }
69 }
70
71 #[lang = "drop_in_place"]
72 #[allow(unconditional_recursion)]
73 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
74     // Code here does not matter - this is replaced by the
75     // real drop glue by the compiler.
76     drop_in_place(to_drop);
77 }
78
79 #[lang = "panic"]
80 #[track_caller]
81 #[no_mangle]
82 pub fn panic(_msg: &str) -> ! {
83     unsafe {
84         libc::puts("Panicking\0" as *const str as *const u8);
85         intrinsics::abort();
86     }
87 }
88
89 #[lang = "panic_location"]
90 struct PanicLocation {
91     file: &'static str,
92     line: u32,
93     column: u32,
94 }
95
96 #[lang = "panic_bounds_check"]
97 #[track_caller]
98 #[no_mangle]
99 fn panic_bounds_check(index: usize, len: usize) -> ! {
100     unsafe {
101         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);
102         intrinsics::abort();
103     }
104 }
105
106 mod intrinsics {
107     extern "rust-intrinsic" {
108         pub fn abort() -> !;
109     }
110 }
111
112 #[lang = "add"]
113 trait Add<RHS = Self> {
114     type Output;
115
116     fn add(self, rhs: RHS) -> Self::Output;
117 }
118
119 impl Add for u8 {
120     type Output = Self;
121
122     fn add(self, rhs: Self) -> Self {
123         self + rhs
124     }
125 }
126
127 impl Add for i8 {
128     type Output = Self;
129
130     fn add(self, rhs: Self) -> Self {
131         self + rhs
132     }
133 }
134
135 impl Add for i32 {
136     type Output = Self;
137
138     fn add(self, rhs: Self) -> Self {
139         self + rhs
140     }
141 }
142
143 impl Add for usize {
144     type Output = Self;
145
146     fn add(self, rhs: Self) -> Self {
147         self + rhs
148     }
149 }
150
151 impl Add for isize {
152     type Output = Self;
153
154     fn add(self, rhs: Self) -> Self {
155         self + rhs
156     }
157 }
158
159 #[lang = "sub"]
160 pub trait Sub<RHS = Self> {
161     type Output;
162
163     fn sub(self, rhs: RHS) -> Self::Output;
164 }
165
166 impl Sub for usize {
167     type Output = Self;
168
169     fn sub(self, rhs: Self) -> Self {
170         self - rhs
171     }
172 }
173
174 impl Sub for isize {
175     type Output = Self;
176
177     fn sub(self, rhs: Self) -> Self {
178         self - rhs
179     }
180 }
181
182 impl Sub for u8 {
183     type Output = Self;
184
185     fn sub(self, rhs: Self) -> Self {
186         self - rhs
187     }
188 }
189
190 impl Sub for i8 {
191     type Output = Self;
192
193     fn sub(self, rhs: Self) -> Self {
194         self - rhs
195     }
196 }
197
198 impl Sub for i16 {
199     type Output = Self;
200
201     fn sub(self, rhs: Self) -> Self {
202         self - rhs
203     }
204 }
205
206
207 /*
208  * Code
209  */
210
211 static mut ONE: usize = 1;
212
213 fn make_array() -> [u8; 3] {
214     [42, 10, 5]
215 }
216
217 #[start]
218 fn main(argc: isize, _argv: *const *const u8) -> isize {
219     let array = [42, 7, 5];
220     let array2 = make_array();
221     unsafe {
222         libc::printf(b"%ld\n\0" as *const u8 as *const i8, array[ONE - 1]);
223         libc::printf(b"%ld\n\0" as *const u8 as *const i8, array[ONE]);
224         libc::printf(b"%ld\n\0" as *const u8 as *const i8, array[ONE + 1]);
225
226         libc::printf(b"%d\n\0" as *const u8 as *const i8, array2[argc as usize] as u32);
227     }
228     0
229 }