]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
Merge pull request #1100 from mominul/trans->codegen
[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,
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 = "neg"]
369 pub trait Neg {
370     type Output;
371
372     fn neg(self) -> Self::Output;
373 }
374
375 impl Neg for i8 {
376     type Output = i8;
377
378     fn neg(self) -> i8 {
379         -self
380     }
381 }
382
383 impl Neg for i16 {
384     type Output = i16;
385
386     fn neg(self) -> i16 {
387         self
388     }
389 }
390
391 impl Neg for isize {
392     type Output = isize;
393
394     fn neg(self) -> isize {
395         -self
396     }
397 }
398
399 impl Neg for f32 {
400     type Output = f32;
401
402     fn neg(self) -> f32 {
403         -self
404     }
405 }
406
407 pub enum Option<T> {
408     Some(T),
409     None,
410 }
411
412 pub use Option::*;
413
414 #[lang = "phantom_data"]
415 pub struct PhantomData<T: ?Sized>;
416
417 #[lang = "fn_once"]
418 #[rustc_paren_sugar]
419 pub trait FnOnce<Args> {
420     #[lang = "fn_once_output"]
421     type Output;
422
423     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
424 }
425
426 #[lang = "fn_mut"]
427 #[rustc_paren_sugar]
428 pub trait FnMut<Args>: FnOnce<Args> {
429     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
430 }
431
432 #[lang = "panic"]
433 #[track_caller]
434 pub fn panic(_msg: &str) -> ! {
435     unsafe {
436         libc::puts("Panicking\n\0" as *const str as *const i8);
437         intrinsics::abort();
438     }
439 }
440
441 #[lang = "panic_bounds_check"]
442 #[track_caller]
443 fn panic_bounds_check(index: usize, len: usize) -> ! {
444     unsafe {
445         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);
446         intrinsics::abort();
447     }
448 }
449
450 #[lang = "eh_personality"]
451 fn eh_personality() -> ! {
452     loop {}
453 }
454
455 #[lang = "drop_in_place"]
456 #[allow(unconditional_recursion)]
457 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
458     // Code here does not matter - this is replaced by the
459     // real drop glue by the compiler.
460     drop_in_place(to_drop);
461 }
462
463 #[lang = "deref"]
464 pub trait Deref {
465     type Target: ?Sized;
466
467     fn deref(&self) -> &Self::Target;
468 }
469
470 #[lang = "owned_box"]
471 pub struct Box<T: ?Sized>(*mut T);
472
473 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
474
475 impl<T: ?Sized> Drop for Box<T> {
476     fn drop(&mut self) {
477         // drop is currently performed by compiler.
478     }
479 }
480
481 impl<T> Deref for Box<T> {
482     type Target = T;
483
484     fn deref(&self) -> &Self::Target {
485         &**self
486     }
487 }
488
489 #[lang = "exchange_malloc"]
490 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
491     libc::malloc(size)
492 }
493
494 #[lang = "box_free"]
495 unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
496     libc::free(ptr as *mut u8);
497 }
498
499 #[lang = "drop"]
500 pub trait Drop {
501     fn drop(&mut self);
502 }
503
504 #[lang = "manually_drop"]
505 #[repr(transparent)]
506 pub struct ManuallyDrop<T: ?Sized> {
507     pub value: T,
508 }
509
510 #[lang = "maybe_uninit"]
511 #[repr(transparent)]
512 pub union MaybeUninit<T> {
513     pub uninit: (),
514     pub value: ManuallyDrop<T>,
515 }
516
517 pub mod intrinsics {
518     extern "rust-intrinsic" {
519         pub fn abort() -> !;
520         pub fn size_of<T>() -> usize;
521         pub fn size_of_val<T: ?::Sized>(val: *const T) -> usize;
522         pub fn min_align_of<T>() -> usize;
523         pub fn min_align_of_val<T: ?::Sized>(val: *const T) -> usize;
524         pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
525         pub fn transmute<T, U>(e: T) -> U;
526         pub fn ctlz_nonzero<T>(x: T) -> T;
527         pub fn needs_drop<T>() -> bool;
528         pub fn bitreverse<T>(x: T) -> T;
529         pub fn bswap<T>(x: T) -> T;
530         pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
531     }
532 }
533
534 pub mod libc {
535     #[cfg_attr(not(windows), link(name = "c"))]
536     #[cfg_attr(windows, link(name = "msvcrt"))]
537     extern "C" {
538         pub fn puts(s: *const i8) -> i32;
539         pub fn printf(format: *const i8, ...) -> i32;
540         pub fn malloc(size: usize) -> *mut u8;
541         pub fn free(ptr: *mut u8);
542         pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
543         pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
544         pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
545     }
546 }
547
548 #[lang = "index"]
549 pub trait Index<Idx: ?Sized> {
550     type Output: ?Sized;
551     fn index(&self, index: Idx) -> &Self::Output;
552 }
553
554 impl<T> Index<usize> for [T; 3] {
555     type Output = T;
556
557     fn index(&self, index: usize) -> &Self::Output {
558         &self[index]
559     }
560 }
561
562 impl<T> Index<usize> for [T] {
563     type Output = T;
564
565     fn index(&self, index: usize) -> &Self::Output {
566         &self[index]
567     }
568 }
569
570 extern {
571     type VaListImpl;
572 }
573
574 #[lang = "va_list"]
575 #[repr(transparent)]
576 pub struct VaList<'a>(&'a mut VaListImpl);
577
578 #[rustc_builtin_macro]
579 #[rustc_macro_transparency = "semitransparent"]
580 pub macro stringify($($t:tt)*) { /* compiler built-in */ }
581
582 #[rustc_builtin_macro]
583 #[rustc_macro_transparency = "semitransparent"]
584 pub macro file() { /* compiler built-in */ }
585
586 #[rustc_builtin_macro]
587 #[rustc_macro_transparency = "semitransparent"]
588 pub macro line() { /* compiler built-in */ }
589
590 #[rustc_builtin_macro]
591 #[rustc_macro_transparency = "semitransparent"]
592 pub macro cfg() { /* compiler built-in */ }
593
594 #[rustc_builtin_macro]
595 #[rustc_macro_transparency = "semitransparent"]
596 pub macro global_asm() { /* compiler built-in */ }
597
598 pub static A_STATIC: u8 = 42;
599
600 #[lang = "panic_location"]
601 struct PanicLocation {
602     file: &'static str,
603     line: u32,
604     column: u32,
605 }
606
607 #[no_mangle]
608 pub fn get_tls() -> u8 {
609     #[thread_local]
610     static A: u8 = 42;
611
612     A
613 }