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