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