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