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