]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
Finish bumping stage0
[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 #[no_mangle]
10 unsafe extern "C" fn _Unwind_Resume() {
11     intrinsics::unreachable();
12 }
13
14 #[lang = "sized"]
15 pub trait Sized {}
16
17 #[lang = "destruct"]
18 pub trait Destruct {}
19
20 #[lang = "unsize"]
21 pub trait Unsize<T: ?Sized> {}
22
23 #[lang = "coerce_unsized"]
24 pub trait CoerceUnsized<T> {}
25
26 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
27 impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
28 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
29 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
30
31 #[lang = "dispatch_from_dyn"]
32 pub trait DispatchFromDyn<T> {}
33
34 // &T -> &U
35 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
36 // &mut T -> &mut U
37 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
38 // *const T -> *const U
39 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
40 // *mut T -> *mut U
41 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
42 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
43
44 #[lang = "receiver"]
45 pub trait Receiver {}
46
47 impl<T: ?Sized> Receiver for &T {}
48 impl<T: ?Sized> Receiver for &mut T {}
49 impl<T: ?Sized> Receiver for Box<T> {}
50
51 #[lang = "copy"]
52 pub unsafe trait Copy {}
53
54 unsafe impl Copy for bool {}
55 unsafe impl Copy for u8 {}
56 unsafe impl Copy for u16 {}
57 unsafe impl Copy for u32 {}
58 unsafe impl Copy for u64 {}
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
71 #[lang = "sync"]
72 pub unsafe trait Sync {}
73
74 unsafe impl Sync for bool {}
75 unsafe impl Sync for u8 {}
76 unsafe impl Sync for u16 {}
77 unsafe impl Sync for u32 {}
78 unsafe impl Sync for u64 {}
79 unsafe impl Sync for usize {}
80 unsafe impl Sync for i8 {}
81 unsafe impl Sync for i16 {}
82 unsafe impl Sync for i32 {}
83 unsafe impl Sync for isize {}
84 unsafe impl Sync for char {}
85 unsafe impl<'a, T: ?Sized> Sync for &'a T {}
86 unsafe impl Sync for [u8; 16] {}
87
88 #[lang = "freeze"]
89 unsafe auto trait Freeze {}
90
91 unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
92 unsafe impl<T: ?Sized> Freeze for *const T {}
93 unsafe impl<T: ?Sized> Freeze for *mut T {}
94 unsafe impl<T: ?Sized> Freeze for &T {}
95 unsafe impl<T: ?Sized> Freeze for &mut T {}
96
97 #[lang = "structural_peq"]
98 pub trait StructuralPartialEq {}
99
100 #[lang = "structural_teq"]
101 pub trait StructuralEq {}
102
103 #[lang = "not"]
104 pub trait Not {
105     type Output;
106
107     fn not(self) -> Self::Output;
108 }
109
110 impl Not for bool {
111     type Output = bool;
112
113     fn not(self) -> bool {
114         !self
115     }
116 }
117
118 #[lang = "mul"]
119 pub trait Mul<RHS = Self> {
120     type Output;
121
122     #[must_use]
123     fn mul(self, rhs: RHS) -> Self::Output;
124 }
125
126 impl Mul for u8 {
127     type Output = Self;
128
129     fn mul(self, rhs: Self) -> Self::Output {
130         self * rhs
131     }
132 }
133
134 impl Mul for usize {
135     type Output = Self;
136
137     fn mul(self, rhs: Self) -> Self::Output {
138         self * rhs
139     }
140 }
141
142 #[lang = "add"]
143 pub trait Add<RHS = Self> {
144     type Output;
145
146     fn add(self, rhs: RHS) -> Self::Output;
147 }
148
149 impl Add for u8 {
150     type Output = Self;
151
152     fn add(self, rhs: Self) -> Self {
153         self + rhs
154     }
155 }
156
157 impl Add for i8 {
158     type Output = Self;
159
160     fn add(self, rhs: Self) -> Self {
161         self + rhs
162     }
163 }
164
165 impl Add for usize {
166     type Output = Self;
167
168     fn add(self, rhs: Self) -> Self {
169         self + rhs
170     }
171 }
172
173 #[lang = "sub"]
174 pub trait Sub<RHS = Self> {
175     type Output;
176
177     fn sub(self, rhs: RHS) -> Self::Output;
178 }
179
180 impl Sub for usize {
181     type Output = Self;
182
183     fn sub(self, rhs: Self) -> Self {
184         self - rhs
185     }
186 }
187
188 impl Sub for u8 {
189     type Output = Self;
190
191     fn sub(self, rhs: Self) -> Self {
192         self - rhs
193     }
194 }
195
196 impl Sub for i8 {
197     type Output = Self;
198
199     fn sub(self, rhs: Self) -> Self {
200         self - rhs
201     }
202 }
203
204 impl Sub for i16 {
205     type Output = Self;
206
207     fn sub(self, rhs: Self) -> Self {
208         self - rhs
209     }
210 }
211
212 #[lang = "rem"]
213 pub trait Rem<RHS = Self> {
214     type Output;
215
216     fn rem(self, rhs: RHS) -> Self::Output;
217 }
218
219 impl Rem for usize {
220     type Output = Self;
221
222     fn rem(self, rhs: Self) -> Self {
223         self % rhs
224     }
225 }
226
227 #[lang = "bitor"]
228 pub trait BitOr<RHS = Self> {
229     type Output;
230
231     #[must_use]
232     fn bitor(self, rhs: RHS) -> Self::Output;
233 }
234
235 impl BitOr for bool {
236     type Output = bool;
237
238     fn bitor(self, rhs: bool) -> bool {
239         self | rhs
240     }
241 }
242
243 impl<'a> BitOr<bool> for &'a bool {
244     type Output = bool;
245
246     fn bitor(self, rhs: bool) -> bool {
247         *self | rhs
248     }
249 }
250
251 #[lang = "eq"]
252 pub trait PartialEq<Rhs: ?Sized = Self> {
253     fn eq(&self, other: &Rhs) -> bool;
254     fn ne(&self, other: &Rhs) -> bool;
255 }
256
257 impl PartialEq for u8 {
258     fn eq(&self, other: &u8) -> bool {
259         (*self) == (*other)
260     }
261     fn ne(&self, other: &u8) -> bool {
262         (*self) != (*other)
263     }
264 }
265
266 impl PartialEq for u16 {
267     fn eq(&self, other: &u16) -> bool {
268         (*self) == (*other)
269     }
270     fn ne(&self, other: &u16) -> bool {
271         (*self) != (*other)
272     }
273 }
274
275 impl PartialEq for u32 {
276     fn eq(&self, other: &u32) -> bool {
277         (*self) == (*other)
278     }
279     fn ne(&self, other: &u32) -> bool {
280         (*self) != (*other)
281     }
282 }
283
284
285 impl PartialEq for u64 {
286     fn eq(&self, other: &u64) -> bool {
287         (*self) == (*other)
288     }
289     fn ne(&self, other: &u64) -> bool {
290         (*self) != (*other)
291     }
292 }
293
294 impl PartialEq for usize {
295     fn eq(&self, other: &usize) -> bool {
296         (*self) == (*other)
297     }
298     fn ne(&self, other: &usize) -> bool {
299         (*self) != (*other)
300     }
301 }
302
303 impl PartialEq for i8 {
304     fn eq(&self, other: &i8) -> bool {
305         (*self) == (*other)
306     }
307     fn ne(&self, other: &i8) -> bool {
308         (*self) != (*other)
309     }
310 }
311
312 impl PartialEq for i32 {
313     fn eq(&self, other: &i32) -> bool {
314         (*self) == (*other)
315     }
316     fn ne(&self, other: &i32) -> bool {
317         (*self) != (*other)
318     }
319 }
320
321 impl PartialEq for isize {
322     fn eq(&self, other: &isize) -> bool {
323         (*self) == (*other)
324     }
325     fn ne(&self, other: &isize) -> bool {
326         (*self) != (*other)
327     }
328 }
329
330 impl PartialEq for char {
331     fn eq(&self, other: &char) -> bool {
332         (*self) == (*other)
333     }
334     fn ne(&self, other: &char) -> bool {
335         (*self) != (*other)
336     }
337 }
338
339 impl<T: ?Sized> PartialEq for *const T {
340     fn eq(&self, other: &*const T) -> bool {
341         *self == *other
342     }
343     fn ne(&self, other: &*const T) -> bool {
344         *self != *other
345     }
346 }
347
348 #[lang = "neg"]
349 pub trait Neg {
350     type Output;
351
352     fn neg(self) -> Self::Output;
353 }
354
355 impl Neg for i8 {
356     type Output = i8;
357
358     fn neg(self) -> i8 {
359         -self
360     }
361 }
362
363 impl Neg for i16 {
364     type Output = i16;
365
366     fn neg(self) -> i16 {
367         self
368     }
369 }
370
371 impl Neg for isize {
372     type Output = isize;
373
374     fn neg(self) -> isize {
375         -self
376     }
377 }
378
379 impl Neg for f32 {
380     type Output = f32;
381
382     fn neg(self) -> f32 {
383         -self
384     }
385 }
386
387 pub enum Option<T> {
388     Some(T),
389     None,
390 }
391
392 pub use Option::*;
393
394 #[lang = "phantom_data"]
395 pub struct PhantomData<T: ?Sized>;
396
397 #[lang = "fn_once"]
398 #[rustc_paren_sugar]
399 pub trait FnOnce<Args> {
400     #[lang = "fn_once_output"]
401     type Output;
402
403     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
404 }
405
406 #[lang = "fn_mut"]
407 #[rustc_paren_sugar]
408 pub trait FnMut<Args>: FnOnce<Args> {
409     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
410 }
411
412 #[lang = "panic"]
413 #[track_caller]
414 pub fn panic(_msg: &str) -> ! {
415     unsafe {
416         libc::puts("Panicking\n\0" as *const str as *const u8);
417         intrinsics::abort();
418     }
419 }
420
421 #[lang = "panic_bounds_check"]
422 #[track_caller]
423 fn panic_bounds_check(index: usize, len: usize) -> ! {
424     unsafe {
425         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);
426         intrinsics::abort();
427     }
428 }
429
430 #[lang = "eh_personality"]
431 fn eh_personality() -> ! {
432     loop {}
433 }
434
435 #[lang = "drop_in_place"]
436 #[allow(unconditional_recursion)]
437 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
438     // Code here does not matter - this is replaced by the
439     // real drop glue by the compiler.
440     drop_in_place(to_drop);
441 }
442
443 #[lang = "deref"]
444 pub trait Deref {
445     type Target: ?Sized;
446
447     fn deref(&self) -> &Self::Target;
448 }
449
450 pub trait Allocator {
451 }
452
453 pub struct Global;
454
455 impl Allocator for Global {}
456
457 #[lang = "owned_box"]
458 pub struct Box<
459     T: ?Sized,
460     A: Allocator = Global,
461 >(*mut T, A);
462
463 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
464
465 impl<T: ?Sized, A: Allocator> Drop for Box<T, A> {
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, A: Allocator>(ptr: *mut T, alloc: A) {
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         pub fn unreachable() -> !;
522     }
523 }
524
525 pub mod libc {
526     #[link(name = "c")]
527     extern "C" {
528         pub fn puts(s: *const u8) -> 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 pub static A_STATIC: u8 = 42;
585
586 #[lang = "panic_location"]
587 struct PanicLocation {
588     file: &'static str,
589     line: u32,
590     column: u32,
591 }
592
593 #[no_mangle]
594 pub fn get_tls() -> u8 {
595     #[thread_local]
596     static A: u8 = 42;
597
598     A
599 }