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