]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_gcc/example/mini_core.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_codegen_gcc / example / mini_core.rs
CommitLineData
c295e0f8
XL
1#![feature(
2 no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types,
3 untagged_unions, decl_macro, rustc_attrs, transparent_unions, auto_traits,
4 thread_local
5)]
6#![no_core]
7#![allow(dead_code)]
8
9#[no_mangle]
10unsafe extern "C" fn _Unwind_Resume() {
11 intrinsics::unreachable();
12}
13
14#[lang = "sized"]
15pub trait Sized {}
16
17#[lang = "unsize"]
18pub trait Unsize<T: ?Sized> {}
19
20#[lang = "coerce_unsized"]
21pub trait CoerceUnsized<T> {}
22
23impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
24impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
25impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
26impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
27
28#[lang = "dispatch_from_dyn"]
29pub trait DispatchFromDyn<T> {}
30
31// &T -> &U
32impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
33// &mut T -> &mut U
34impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
35// *const T -> *const U
36impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
37// *mut T -> *mut U
38impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
39impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
40
41#[lang = "receiver"]
42pub trait Receiver {}
43
44impl<T: ?Sized> Receiver for &T {}
45impl<T: ?Sized> Receiver for &mut T {}
46impl<T: ?Sized> Receiver for Box<T> {}
47
48#[lang = "copy"]
49pub unsafe trait Copy {}
50
51unsafe impl Copy for bool {}
52unsafe impl Copy for u8 {}
53unsafe impl Copy for u16 {}
54unsafe impl Copy for u32 {}
55unsafe impl Copy for u64 {}
56unsafe impl Copy for usize {}
57unsafe impl Copy for i8 {}
58unsafe impl Copy for i16 {}
59unsafe impl Copy for i32 {}
60unsafe impl Copy for isize {}
61unsafe impl Copy for f32 {}
62unsafe impl Copy for char {}
63unsafe impl<'a, T: ?Sized> Copy for &'a T {}
64unsafe impl<T: ?Sized> Copy for *const T {}
65unsafe impl<T: ?Sized> Copy for *mut T {}
66
67#[lang = "sync"]
68pub unsafe trait Sync {}
69
70unsafe impl Sync for bool {}
71unsafe impl Sync for u8 {}
72unsafe impl Sync for u16 {}
73unsafe impl Sync for u32 {}
74unsafe impl Sync for u64 {}
75unsafe impl Sync for usize {}
76unsafe impl Sync for i8 {}
77unsafe impl Sync for i16 {}
78unsafe impl Sync for i32 {}
79unsafe impl Sync for isize {}
80unsafe impl Sync for char {}
81unsafe impl<'a, T: ?Sized> Sync for &'a T {}
82unsafe impl Sync for [u8; 16] {}
83
84#[lang = "freeze"]
85unsafe auto trait Freeze {}
86
87unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
88unsafe impl<T: ?Sized> Freeze for *const T {}
89unsafe impl<T: ?Sized> Freeze for *mut T {}
90unsafe impl<T: ?Sized> Freeze for &T {}
91unsafe impl<T: ?Sized> Freeze for &mut T {}
92
93#[lang = "structural_peq"]
94pub trait StructuralPartialEq {}
95
96#[lang = "structural_teq"]
97pub trait StructuralEq {}
98
99#[lang = "not"]
100pub trait Not {
101 type Output;
102
103 fn not(self) -> Self::Output;
104}
105
106impl Not for bool {
107 type Output = bool;
108
109 fn not(self) -> bool {
110 !self
111 }
112}
113
114#[lang = "mul"]
115pub trait Mul<RHS = Self> {
116 type Output;
117
118 #[must_use]
119 fn mul(self, rhs: RHS) -> Self::Output;
120}
121
122impl Mul for u8 {
123 type Output = Self;
124
125 fn mul(self, rhs: Self) -> Self::Output {
126 self * rhs
127 }
128}
129
130impl Mul for usize {
131 type Output = Self;
132
133 fn mul(self, rhs: Self) -> Self::Output {
134 self * rhs
135 }
136}
137
138#[lang = "add"]
139pub trait Add<RHS = Self> {
140 type Output;
141
142 fn add(self, rhs: RHS) -> Self::Output;
143}
144
145impl Add for u8 {
146 type Output = Self;
147
148 fn add(self, rhs: Self) -> Self {
149 self + rhs
150 }
151}
152
153impl Add for i8 {
154 type Output = Self;
155
156 fn add(self, rhs: Self) -> Self {
157 self + rhs
158 }
159}
160
161impl Add for usize {
162 type Output = Self;
163
164 fn add(self, rhs: Self) -> Self {
165 self + rhs
166 }
167}
168
169#[lang = "sub"]
170pub trait Sub<RHS = Self> {
171 type Output;
172
173 fn sub(self, rhs: RHS) -> Self::Output;
174}
175
176impl Sub for usize {
177 type Output = Self;
178
179 fn sub(self, rhs: Self) -> Self {
180 self - rhs
181 }
182}
183
184impl Sub for u8 {
185 type Output = Self;
186
187 fn sub(self, rhs: Self) -> Self {
188 self - rhs
189 }
190}
191
192impl Sub for i8 {
193 type Output = Self;
194
195 fn sub(self, rhs: Self) -> Self {
196 self - rhs
197 }
198}
199
200impl Sub for i16 {
201 type Output = Self;
202
203 fn sub(self, rhs: Self) -> Self {
204 self - rhs
205 }
206}
207
208#[lang = "rem"]
209pub trait Rem<RHS = Self> {
210 type Output;
211
212 fn rem(self, rhs: RHS) -> Self::Output;
213}
214
215impl Rem for usize {
216 type Output = Self;
217
218 fn rem(self, rhs: Self) -> Self {
219 self % rhs
220 }
221}
222
223#[lang = "bitor"]
224pub trait BitOr<RHS = Self> {
225 type Output;
226
227 #[must_use]
228 fn bitor(self, rhs: RHS) -> Self::Output;
229}
230
231impl BitOr for bool {
232 type Output = bool;
233
234 fn bitor(self, rhs: bool) -> bool {
235 self | rhs
236 }
237}
238
239impl<'a> BitOr<bool> for &'a bool {
240 type Output = bool;
241
242 fn bitor(self, rhs: bool) -> bool {
243 *self | rhs
244 }
245}
246
247#[lang = "eq"]
248pub trait PartialEq<Rhs: ?Sized = Self> {
249 fn eq(&self, other: &Rhs) -> bool;
250 fn ne(&self, other: &Rhs) -> bool;
251}
252
253impl PartialEq for u8 {
254 fn eq(&self, other: &u8) -> bool {
255 (*self) == (*other)
256 }
257 fn ne(&self, other: &u8) -> bool {
258 (*self) != (*other)
259 }
260}
261
262impl PartialEq for u16 {
263 fn eq(&self, other: &u16) -> bool {
264 (*self) == (*other)
265 }
266 fn ne(&self, other: &u16) -> bool {
267 (*self) != (*other)
268 }
269}
270
271impl PartialEq for u32 {
272 fn eq(&self, other: &u32) -> bool {
273 (*self) == (*other)
274 }
275 fn ne(&self, other: &u32) -> bool {
276 (*self) != (*other)
277 }
278}
279
280
281impl PartialEq for u64 {
282 fn eq(&self, other: &u64) -> bool {
283 (*self) == (*other)
284 }
285 fn ne(&self, other: &u64) -> bool {
286 (*self) != (*other)
287 }
288}
289
290impl PartialEq for usize {
291 fn eq(&self, other: &usize) -> bool {
292 (*self) == (*other)
293 }
294 fn ne(&self, other: &usize) -> bool {
295 (*self) != (*other)
296 }
297}
298
299impl PartialEq for i8 {
300 fn eq(&self, other: &i8) -> bool {
301 (*self) == (*other)
302 }
303 fn ne(&self, other: &i8) -> bool {
304 (*self) != (*other)
305 }
306}
307
308impl PartialEq for i32 {
309 fn eq(&self, other: &i32) -> bool {
310 (*self) == (*other)
311 }
312 fn ne(&self, other: &i32) -> bool {
313 (*self) != (*other)
314 }
315}
316
317impl PartialEq for isize {
318 fn eq(&self, other: &isize) -> bool {
319 (*self) == (*other)
320 }
321 fn ne(&self, other: &isize) -> bool {
322 (*self) != (*other)
323 }
324}
325
326impl PartialEq for char {
327 fn eq(&self, other: &char) -> bool {
328 (*self) == (*other)
329 }
330 fn ne(&self, other: &char) -> bool {
331 (*self) != (*other)
332 }
333}
334
335impl<T: ?Sized> PartialEq for *const T {
336 fn eq(&self, other: &*const T) -> bool {
337 *self == *other
338 }
339 fn ne(&self, other: &*const T) -> bool {
340 *self != *other
341 }
342}
343
344#[lang = "neg"]
345pub trait Neg {
346 type Output;
347
348 fn neg(self) -> Self::Output;
349}
350
351impl Neg for i8 {
352 type Output = i8;
353
354 fn neg(self) -> i8 {
355 -self
356 }
357}
358
359impl Neg for i16 {
360 type Output = i16;
361
362 fn neg(self) -> i16 {
363 self
364 }
365}
366
367impl Neg for isize {
368 type Output = isize;
369
370 fn neg(self) -> isize {
371 -self
372 }
373}
374
375impl Neg for f32 {
376 type Output = f32;
377
378 fn neg(self) -> f32 {
379 -self
380 }
381}
382
383pub enum Option<T> {
384 Some(T),
385 None,
386}
387
388pub use Option::*;
389
390#[lang = "phantom_data"]
391pub struct PhantomData<T: ?Sized>;
392
393#[lang = "fn_once"]
394#[rustc_paren_sugar]
395pub trait FnOnce<Args> {
396 #[lang = "fn_once_output"]
397 type Output;
398
399 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
400}
401
402#[lang = "fn_mut"]
403#[rustc_paren_sugar]
404pub trait FnMut<Args>: FnOnce<Args> {
405 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
406}
407
408#[lang = "panic"]
409#[track_caller]
410pub fn panic(_msg: &str) -> ! {
411 unsafe {
412 libc::puts("Panicking\n\0" as *const str as *const u8);
413 intrinsics::abort();
414 }
415}
416
417#[lang = "panic_bounds_check"]
418#[track_caller]
419fn panic_bounds_check(index: usize, len: usize) -> ! {
420 unsafe {
421 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);
422 intrinsics::abort();
423 }
424}
425
426#[lang = "eh_personality"]
427fn eh_personality() -> ! {
428 loop {}
429}
430
431#[lang = "drop_in_place"]
432#[allow(unconditional_recursion)]
433pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
434 // Code here does not matter - this is replaced by the
435 // real drop glue by the compiler.
436 drop_in_place(to_drop);
437}
438
439#[lang = "deref"]
440pub trait Deref {
441 type Target: ?Sized;
442
443 fn deref(&self) -> &Self::Target;
444}
445
446#[lang = "owned_box"]
447pub struct Box<T: ?Sized>(*mut T);
448
449impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
450
451impl<T: ?Sized> Drop for Box<T> {
452 fn drop(&mut self) {
453 // drop is currently performed by compiler.
454 }
455}
456
457impl<T> Deref for Box<T> {
458 type Target = T;
459
460 fn deref(&self) -> &Self::Target {
461 &**self
462 }
463}
464
465#[lang = "exchange_malloc"]
466unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
467 libc::malloc(size)
468}
469
470#[lang = "box_free"]
471unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
472 libc::free(ptr as *mut u8);
473}
474
475#[lang = "drop"]
476pub trait Drop {
477 fn drop(&mut self);
478}
479
480#[lang = "manually_drop"]
481#[repr(transparent)]
482pub struct ManuallyDrop<T: ?Sized> {
483 pub value: T,
484}
485
486#[lang = "maybe_uninit"]
487#[repr(transparent)]
488pub union MaybeUninit<T> {
489 pub uninit: (),
490 pub value: ManuallyDrop<T>,
491}
492
493pub mod intrinsics {
494 extern "rust-intrinsic" {
495 pub fn abort() -> !;
496 pub fn size_of<T>() -> usize;
497 pub fn size_of_val<T: ?::Sized>(val: *const T) -> usize;
498 pub fn min_align_of<T>() -> usize;
499 pub fn min_align_of_val<T: ?::Sized>(val: *const T) -> usize;
500 pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
501 pub fn transmute<T, U>(e: T) -> U;
502 pub fn ctlz_nonzero<T>(x: T) -> T;
503 pub fn needs_drop<T>() -> bool;
504 pub fn bitreverse<T>(x: T) -> T;
505 pub fn bswap<T>(x: T) -> T;
506 pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
507 pub fn unreachable() -> !;
508 }
509}
510
511pub mod libc {
512 #[link(name = "c")]
513 extern "C" {
514 pub fn puts(s: *const u8) -> i32;
515 pub fn printf(format: *const i8, ...) -> i32;
516 pub fn malloc(size: usize) -> *mut u8;
517 pub fn free(ptr: *mut u8);
518 pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
519 pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
520 pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
521 }
522}
523
524#[lang = "index"]
525pub trait Index<Idx: ?Sized> {
526 type Output: ?Sized;
527 fn index(&self, index: Idx) -> &Self::Output;
528}
529
530impl<T> Index<usize> for [T; 3] {
531 type Output = T;
532
533 fn index(&self, index: usize) -> &Self::Output {
534 &self[index]
535 }
536}
537
538impl<T> Index<usize> for [T] {
539 type Output = T;
540
541 fn index(&self, index: usize) -> &Self::Output {
542 &self[index]
543 }
544}
545
546extern {
547 type VaListImpl;
548}
549
550#[lang = "va_list"]
551#[repr(transparent)]
552pub struct VaList<'a>(&'a mut VaListImpl);
553
554#[rustc_builtin_macro]
555#[rustc_macro_transparency = "semitransparent"]
556pub macro stringify($($t:tt)*) { /* compiler built-in */ }
557
558#[rustc_builtin_macro]
559#[rustc_macro_transparency = "semitransparent"]
560pub macro file() { /* compiler built-in */ }
561
562#[rustc_builtin_macro]
563#[rustc_macro_transparency = "semitransparent"]
564pub macro line() { /* compiler built-in */ }
565
566#[rustc_builtin_macro]
567#[rustc_macro_transparency = "semitransparent"]
568pub macro cfg() { /* compiler built-in */ }
569
570pub static A_STATIC: u8 = 42;
571
572#[lang = "panic_location"]
573struct PanicLocation {
574 file: &'static str,
575 line: u32,
576 column: u32,
577}
578
579#[no_mangle]
580pub fn get_tls() -> u8 {
581 #[thread_local]
582 static A: u8 = 42;
583
584 A
585}