]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_gcc/tests/run/array.rs
New upstream version 1.70.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]
353b0b11 82pub fn panic(_msg: &'static str) -> ! {
c295e0f8
XL
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" {
353b0b11 108 #[rustc_safe_intrinsic]
c295e0f8
XL
109 pub fn abort() -> !;
110 }
111}
112
113#[lang = "add"]
114trait Add<RHS = Self> {
115 type Output;
116
117 fn add(self, rhs: RHS) -> Self::Output;
118}
119
120impl Add for u8 {
121 type Output = Self;
122
123 fn add(self, rhs: Self) -> Self {
124 self + rhs
125 }
126}
127
128impl Add for i8 {
129 type Output = Self;
130
131 fn add(self, rhs: Self) -> Self {
132 self + rhs
133 }
134}
135
136impl Add for i32 {
137 type Output = Self;
138
139 fn add(self, rhs: Self) -> Self {
140 self + rhs
141 }
142}
143
144impl Add for usize {
145 type Output = Self;
146
147 fn add(self, rhs: Self) -> Self {
148 self + rhs
149 }
150}
151
152impl Add for isize {
153 type Output = Self;
154
155 fn add(self, rhs: Self) -> Self {
156 self + rhs
157 }
158}
159
160#[lang = "sub"]
161pub trait Sub<RHS = Self> {
162 type Output;
163
164 fn sub(self, rhs: RHS) -> Self::Output;
165}
166
167impl Sub for usize {
168 type Output = Self;
169
170 fn sub(self, rhs: Self) -> Self {
171 self - rhs
172 }
173}
174
175impl Sub for isize {
176 type Output = Self;
177
178 fn sub(self, rhs: Self) -> Self {
179 self - rhs
180 }
181}
182
183impl Sub for u8 {
184 type Output = Self;
185
186 fn sub(self, rhs: Self) -> Self {
187 self - rhs
188 }
189}
190
191impl Sub for i8 {
192 type Output = Self;
193
194 fn sub(self, rhs: Self) -> Self {
195 self - rhs
196 }
197}
198
199impl Sub for i16 {
200 type Output = Self;
201
202 fn sub(self, rhs: Self) -> Self {
203 self - rhs
204 }
205}
206
207
208/*
209 * Code
210 */
211
212static mut ONE: usize = 1;
213
214fn make_array() -> [u8; 3] {
215 [42, 10, 5]
216}
217
218#[start]
219fn main(argc: isize, _argv: *const *const u8) -> isize {
220 let array = [42, 7, 5];
221 let array2 = make_array();
222 unsafe {
223 libc::printf(b"%ld\n\0" as *const u8 as *const i8, array[ONE - 1]);
224 libc::printf(b"%ld\n\0" as *const u8 as *const i8, array[ONE]);
225 libc::printf(b"%ld\n\0" as *const u8 as *const i8, array[ONE + 1]);
226
227 libc::printf(b"%d\n\0" as *const u8 as *const i8, array2[argc as usize] as u32);
228 }
229 0
230}