]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/example/mini_core.rs
Rollup merge of #89920 - hudson-ayers:location-detail-control, r=davidtwco
[rust.git] / compiler / rustc_codegen_gcc / example / mini_core.rs
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]
10 unsafe extern "C" fn _Unwind_Resume() {
11     intrinsics::unreachable();
12 }
13
14 #[lang = "sized"]
15 pub trait Sized {}
16
17 #[lang = "unsize"]
18 pub trait Unsize<T: ?Sized> {}
19
20 #[lang = "coerce_unsized"]
21 pub trait CoerceUnsized<T> {}
22
23 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
24 impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
25 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
26 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
27
28 #[lang = "dispatch_from_dyn"]
29 pub trait DispatchFromDyn<T> {}
30
31 // &T -> &U
32 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
33 // &mut T -> &mut U
34 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
35 // *const T -> *const U
36 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
37 // *mut T -> *mut U
38 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
39 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
40
41 #[lang = "receiver"]
42 pub trait Receiver {}
43
44 impl<T: ?Sized> Receiver for &T {}
45 impl<T: ?Sized> Receiver for &mut T {}
46 impl<T: ?Sized> Receiver for Box<T> {}
47
48 #[lang = "copy"]
49 pub unsafe trait Copy {}
50
51 unsafe impl Copy for bool {}
52 unsafe impl Copy for u8 {}
53 unsafe impl Copy for u16 {}
54 unsafe impl Copy for u32 {}
55 unsafe impl Copy for u64 {}
56 unsafe impl Copy for usize {}
57 unsafe impl Copy for i8 {}
58 unsafe impl Copy for i16 {}
59 unsafe impl Copy for i32 {}
60 unsafe impl Copy for isize {}
61 unsafe impl Copy for f32 {}
62 unsafe impl Copy for char {}
63 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
64 unsafe impl<T: ?Sized> Copy for *const T {}
65 unsafe impl<T: ?Sized> Copy for *mut T {}
66
67 #[lang = "sync"]
68 pub unsafe trait Sync {}
69
70 unsafe impl Sync for bool {}
71 unsafe impl Sync for u8 {}
72 unsafe impl Sync for u16 {}
73 unsafe impl Sync for u32 {}
74 unsafe impl Sync for u64 {}
75 unsafe impl Sync for usize {}
76 unsafe impl Sync for i8 {}
77 unsafe impl Sync for i16 {}
78 unsafe impl Sync for i32 {}
79 unsafe impl Sync for isize {}
80 unsafe impl Sync for char {}
81 unsafe impl<'a, T: ?Sized> Sync for &'a T {}
82 unsafe impl Sync for [u8; 16] {}
83
84 #[lang = "freeze"]
85 unsafe auto trait Freeze {}
86
87 unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
88 unsafe impl<T: ?Sized> Freeze for *const T {}
89 unsafe impl<T: ?Sized> Freeze for *mut T {}
90 unsafe impl<T: ?Sized> Freeze for &T {}
91 unsafe impl<T: ?Sized> Freeze for &mut T {}
92
93 #[lang = "structural_peq"]
94 pub trait StructuralPartialEq {}
95
96 #[lang = "structural_teq"]
97 pub trait StructuralEq {}
98
99 #[lang = "not"]
100 pub trait Not {
101     type Output;
102
103     fn not(self) -> Self::Output;
104 }
105
106 impl Not for bool {
107     type Output = bool;
108
109     fn not(self) -> bool {
110         !self
111     }
112 }
113
114 #[lang = "mul"]
115 pub trait Mul<RHS = Self> {
116     type Output;
117
118     #[must_use]
119     fn mul(self, rhs: RHS) -> Self::Output;
120 }
121
122 impl Mul for u8 {
123     type Output = Self;
124
125     fn mul(self, rhs: Self) -> Self::Output {
126         self * rhs
127     }
128 }
129
130 impl 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"]
139 pub trait Add<RHS = Self> {
140     type Output;
141
142     fn add(self, rhs: RHS) -> Self::Output;
143 }
144
145 impl Add for u8 {
146     type Output = Self;
147
148     fn add(self, rhs: Self) -> Self {
149         self + rhs
150     }
151 }
152
153 impl Add for i8 {
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 #[lang = "sub"]
170 pub trait Sub<RHS = Self> {
171     type Output;
172
173     fn sub(self, rhs: RHS) -> Self::Output;
174 }
175
176 impl Sub for usize {
177     type Output = Self;
178
179     fn sub(self, rhs: Self) -> Self {
180         self - rhs
181     }
182 }
183
184 impl Sub for u8 {
185     type Output = Self;
186
187     fn sub(self, rhs: Self) -> Self {
188         self - rhs
189     }
190 }
191
192 impl Sub for i8 {
193     type Output = Self;
194
195     fn sub(self, rhs: Self) -> Self {
196         self - rhs
197     }
198 }
199
200 impl Sub for i16 {
201     type Output = Self;
202
203     fn sub(self, rhs: Self) -> Self {
204         self - rhs
205     }
206 }
207
208 #[lang = "rem"]
209 pub trait Rem<RHS = Self> {
210     type Output;
211
212     fn rem(self, rhs: RHS) -> Self::Output;
213 }
214
215 impl Rem for usize {
216     type Output = Self;
217
218     fn rem(self, rhs: Self) -> Self {
219         self % rhs
220     }
221 }
222
223 #[lang = "bitor"]
224 pub trait BitOr<RHS = Self> {
225     type Output;
226
227     #[must_use]
228     fn bitor(self, rhs: RHS) -> Self::Output;
229 }
230
231 impl BitOr for bool {
232     type Output = bool;
233
234     fn bitor(self, rhs: bool) -> bool {
235         self | rhs
236     }
237 }
238
239 impl<'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"]
248 pub trait PartialEq<Rhs: ?Sized = Self> {
249     fn eq(&self, other: &Rhs) -> bool;
250     fn ne(&self, other: &Rhs) -> bool;
251 }
252
253 impl 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
262 impl 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
271 impl 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
281 impl 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
290 impl 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
299 impl 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
308 impl 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
317 impl 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
326 impl 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
335 impl<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"]
345 pub trait Neg {
346     type Output;
347
348     fn neg(self) -> Self::Output;
349 }
350
351 impl Neg for i8 {
352     type Output = i8;
353
354     fn neg(self) -> i8 {
355         -self
356     }
357 }
358
359 impl Neg for i16 {
360     type Output = i16;
361
362     fn neg(self) -> i16 {
363         self
364     }
365 }
366
367 impl Neg for isize {
368     type Output = isize;
369
370     fn neg(self) -> isize {
371         -self
372     }
373 }
374
375 impl Neg for f32 {
376     type Output = f32;
377
378     fn neg(self) -> f32 {
379         -self
380     }
381 }
382
383 pub enum Option<T> {
384     Some(T),
385     None,
386 }
387
388 pub use Option::*;
389
390 #[lang = "phantom_data"]
391 pub struct PhantomData<T: ?Sized>;
392
393 #[lang = "fn_once"]
394 #[rustc_paren_sugar]
395 pub 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]
404 pub 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]
410 pub 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]
419 fn 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"]
427 fn eh_personality() -> ! {
428     loop {}
429 }
430
431 #[lang = "drop_in_place"]
432 #[allow(unconditional_recursion)]
433 pub 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"]
440 pub trait Deref {
441     type Target: ?Sized;
442
443     fn deref(&self) -> &Self::Target;
444 }
445
446 #[lang = "owned_box"]
447 pub struct Box<T: ?Sized>(*mut T);
448
449 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
450
451 impl<T: ?Sized> Drop for Box<T> {
452     fn drop(&mut self) {
453         // drop is currently performed by compiler.
454     }
455 }
456
457 impl<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"]
466 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
467     libc::malloc(size)
468 }
469
470 #[lang = "box_free"]
471 unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
472     libc::free(ptr as *mut u8);
473 }
474
475 #[lang = "drop"]
476 pub trait Drop {
477     fn drop(&mut self);
478 }
479
480 #[lang = "manually_drop"]
481 #[repr(transparent)]
482 pub struct ManuallyDrop<T: ?Sized> {
483     pub value: T,
484 }
485
486 #[lang = "maybe_uninit"]
487 #[repr(transparent)]
488 pub union MaybeUninit<T> {
489     pub uninit: (),
490     pub value: ManuallyDrop<T>,
491 }
492
493 pub 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
511 pub 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"]
525 pub trait Index<Idx: ?Sized> {
526     type Output: ?Sized;
527     fn index(&self, index: Idx) -> &Self::Output;
528 }
529
530 impl<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
538 impl<T> Index<usize> for [T] {
539     type Output = T;
540
541     fn index(&self, index: usize) -> &Self::Output {
542         &self[index]
543     }
544 }
545
546 extern {
547     type VaListImpl;
548 }
549
550 #[lang = "va_list"]
551 #[repr(transparent)]
552 pub struct VaList<'a>(&'a mut VaListImpl);
553
554 #[rustc_builtin_macro]
555 #[rustc_macro_transparency = "semitransparent"]
556 pub macro stringify($($t:tt)*) { /* compiler built-in */ }
557
558 #[rustc_builtin_macro]
559 #[rustc_macro_transparency = "semitransparent"]
560 pub macro file() { /* compiler built-in */ }
561
562 #[rustc_builtin_macro]
563 #[rustc_macro_transparency = "semitransparent"]
564 pub macro line() { /* compiler built-in */ }
565
566 #[rustc_builtin_macro]
567 #[rustc_macro_transparency = "semitransparent"]
568 pub macro cfg() { /* compiler built-in */ }
569
570 pub static A_STATIC: u8 = 42;
571
572 #[lang = "panic_location"]
573 struct PanicLocation {
574     file: &'static str,
575     line: u32,
576     column: u32,
577 }
578
579 #[no_mangle]
580 pub fn get_tls() -> u8 {
581     #[thread_local]
582     static A: u8 = 42;
583
584     A
585 }