]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_gcc/tests/run/array.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_codegen_gcc / tests / run / array.rs
CommitLineData
c295e0f8
XL
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"]
21pub trait Sized {}
22
23#[lang = "copy"]
24trait Copy {
25}
26
27impl Copy for isize {}
28impl Copy for usize {}
29impl Copy for i32 {}
30impl Copy for u8 {}
31impl Copy for i8 {}
32impl Copy for i16 {}
33
34#[lang = "receiver"]
35trait Receiver {
36}
37
38#[lang = "freeze"]
39pub(crate) unsafe auto trait Freeze {}
40
41mod 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"]
50pub trait Index<Idx: ?Sized> {
51 type Output: ?Sized;
52 fn index(&self, index: Idx) -> &Self::Output;
53}
54
55impl<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
63impl<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)]
73pub 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]
82pub 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"]
90struct PanicLocation {
91 file: &'static str,
92 line: u32,
93 column: u32,
94}
95
96#[lang = "panic_bounds_check"]
97#[track_caller]
98#[no_mangle]
99fn 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
106mod intrinsics {
107 extern "rust-intrinsic" {
108 pub fn abort() -> !;
109 }
110}
111
112#[lang = "add"]
113trait Add<RHS = Self> {
114 type Output;
115
116 fn add(self, rhs: RHS) -> Self::Output;
117}
118
119impl Add for u8 {
120 type Output = Self;
121
122 fn add(self, rhs: Self) -> Self {
123 self + rhs
124 }
125}
126
127impl Add for i8 {
128 type Output = Self;
129
130 fn add(self, rhs: Self) -> Self {
131 self + rhs
132 }
133}
134
135impl Add for i32 {
136 type Output = Self;
137
138 fn add(self, rhs: Self) -> Self {
139 self + rhs
140 }
141}
142
143impl Add for usize {
144 type Output = Self;
145
146 fn add(self, rhs: Self) -> Self {
147 self + rhs
148 }
149}
150
151impl Add for isize {
152 type Output = Self;
153
154 fn add(self, rhs: Self) -> Self {
155 self + rhs
156 }
157}
158
159#[lang = "sub"]
160pub trait Sub<RHS = Self> {
161 type Output;
162
163 fn sub(self, rhs: RHS) -> Self::Output;
164}
165
166impl Sub for usize {
167 type Output = Self;
168
169 fn sub(self, rhs: Self) -> Self {
170 self - rhs
171 }
172}
173
174impl Sub for isize {
175 type Output = Self;
176
177 fn sub(self, rhs: Self) -> Self {
178 self - rhs
179 }
180}
181
182impl Sub for u8 {
183 type Output = Self;
184
185 fn sub(self, rhs: Self) -> Self {
186 self - rhs
187 }
188}
189
190impl Sub for i8 {
191 type Output = Self;
192
193 fn sub(self, rhs: Self) -> Self {
194 self - rhs
195 }
196}
197
198impl 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
211static mut ONE: usize = 1;
212
213fn make_array() -> [u8; 3] {
214 [42, 10, 5]
215}
216
217#[start]
218fn 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}