]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_gcc/tests/run/fun_ptr.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / compiler / rustc_codegen_gcc / tests / run / fun_ptr.rs
CommitLineData
c295e0f8
XL
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"]
18pub trait Sized {}
19
20#[lang = "copy"]
21trait Copy {
22}
23
24impl Copy for isize {}
25impl Copy for usize {}
26impl Copy for i32 {}
27impl Copy for u8 {}
28impl Copy for i8 {}
29impl Copy for i16 {}
30
31#[lang = "receiver"]
32trait Receiver {
33}
34
35#[lang = "freeze"]
36pub(crate) unsafe auto trait Freeze {}
37
38mod 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"]
47pub trait Index<Idx: ?Sized> {
48 type Output: ?Sized;
49 fn index(&self, index: Idx) -> &Self::Output;
50}
51
52impl<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
60impl<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)]
70pub 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]
353b0b11 79pub fn panic(_msg: &'static str) -> ! {
c295e0f8
XL
80 unsafe {
81 libc::puts("Panicking\0" as *const str as *const u8);
82 intrinsics::abort();
83 }
84}
85
86#[lang = "panic_location"]
87struct PanicLocation {
88 file: &'static str,
89 line: u32,
90 column: u32,
91}
92
93#[lang = "panic_bounds_check"]
94#[track_caller]
95#[no_mangle]
96fn 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
103mod intrinsics {
104 extern "rust-intrinsic" {
353b0b11 105 #[rustc_safe_intrinsic]
c295e0f8
XL
106 pub fn abort() -> !;
107 }
108}
109
110#[lang = "add"]
111trait Add<RHS = Self> {
112 type Output;
113
114 fn add(self, rhs: RHS) -> Self::Output;
115}
116
117impl Add for u8 {
118 type Output = Self;
119
120 fn add(self, rhs: Self) -> Self {
121 self + rhs
122 }
123}
124
125impl Add for i8 {
126 type Output = Self;
127
128 fn add(self, rhs: Self) -> Self {
129 self + rhs
130 }
131}
132
133impl Add for i32 {
134 type Output = Self;
135
136 fn add(self, rhs: Self) -> Self {
137 self + rhs
138 }
139}
140
141impl Add for usize {
142 type Output = Self;
143
144 fn add(self, rhs: Self) -> Self {
145 self + rhs
146 }
147}
148
149impl Add for isize {
150 type Output = Self;
151
152 fn add(self, rhs: Self) -> Self {
153 self + rhs
154 }
155}
156
157#[lang = "sub"]
158pub trait Sub<RHS = Self> {
159 type Output;
160
161 fn sub(self, rhs: RHS) -> Self::Output;
162}
163
164impl Sub for usize {
165 type Output = Self;
166
167 fn sub(self, rhs: Self) -> Self {
168 self - rhs
169 }
170}
171
172impl Sub for isize {
173 type Output = Self;
174
175 fn sub(self, rhs: Self) -> Self {
176 self - rhs
177 }
178}
179
180impl Sub for u8 {
181 type Output = Self;
182
183 fn sub(self, rhs: Self) -> Self {
184 self - rhs
185 }
186}
187
188impl Sub for i8 {
189 type Output = Self;
190
191 fn sub(self, rhs: Self) -> Self {
192 self - rhs
193 }
194}
195
196impl Sub for i16 {
197 type Output = Self;
198
199 fn sub(self, rhs: Self) -> Self {
200 self - rhs
201 }
202}
203
204
205/*
206 * Code
207 */
208
209fn i16_as_i8(a: i16) -> i8 {
210 a as i8
211}
212
213fn call_func(func: fn(i16) -> i8, param: i16) -> i8 {
214 func(param)
215}
216
217#[start]
218fn main(argc: isize, _argv: *const *const u8) -> isize {
219 unsafe {
220 let result = call_func(i16_as_i8, argc as i16) as isize;
221 libc::printf(b"%ld\n\0" as *const u8 as *const i8, result);
222 }
223 0
224}