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