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