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