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