]> git.lizzy.rs Git - rust.git/blob - example/mini_core_hello_world.rs
Rustup to rustc 1.38.0-nightly (4560cb830 2019-07-28)
[rust.git] / example / mini_core_hello_world.rs
1 // Adapted from https://github.com/sunfishcode/mir2cranelift/blob/master/rust-examples/nocore-hello-world.rs
2
3 #![feature(no_core, unboxed_closures, start, lang_items, box_syntax, slice_patterns, never_type, linkage)]
4 #![no_core]
5 #![allow(dead_code)]
6
7 extern crate mini_core;
8
9 use mini_core::*;
10 use mini_core::libc::*;
11
12 unsafe extern "C" fn my_puts(s: *const u8) {
13     puts(s);
14 }
15
16 #[lang = "termination"]
17 trait Termination {
18     fn report(self) -> i32;
19 }
20
21 impl Termination for () {
22     fn report(self) -> i32 {
23         unsafe {
24             NUM = 6 * 7 + 1 + (1u8 == 1u8) as u8; // 44
25             *NUM_REF as i32
26         }
27     }
28 }
29
30 trait SomeTrait {
31     fn object_safe(&self);
32 }
33
34 impl SomeTrait for &'static str {
35     fn object_safe(&self) {
36         unsafe {
37             puts(*self as *const str as *const u8);
38         }
39     }
40 }
41
42 struct NoisyDrop {
43     text: &'static str,
44     inner: NoisyDropInner,
45 }
46
47 struct NoisyDropInner;
48
49 impl Drop for NoisyDrop {
50     fn drop(&mut self) {
51         unsafe {
52             puts(self.text as *const str as *const u8);
53         }
54     }
55 }
56
57 impl Drop for NoisyDropInner {
58     fn drop(&mut self) {
59         unsafe {
60             puts("Inner got dropped!\0" as *const str as *const u8);
61         }
62     }
63 }
64
65 impl SomeTrait for NoisyDrop {
66     fn object_safe(&self) {}
67 }
68
69 enum Ordering {
70     Less = -1,
71     Equal = 0,
72     Greater = 1,
73 }
74
75 #[lang = "start"]
76 fn start<T: Termination + 'static>(
77     main: fn() -> T,
78     argc: isize,
79     argv: *const *const u8,
80 ) -> isize {
81     if argc == 3 {
82         unsafe { puts(*argv); }
83         unsafe { puts(*((argv as usize + intrinsics::size_of::<*const u8>()) as *const *const u8)); }
84         unsafe { puts(*((argv as usize + 2 * intrinsics::size_of::<*const u8>()) as *const *const u8)); }
85     }
86
87     main().report();
88     0
89 }
90
91 static mut NUM: u8 = 6 * 7;
92 static NUM_REF: &'static u8 = unsafe { &NUM };
93
94 macro_rules! assert {
95     ($e:expr) => {
96         if !$e {
97             panic(&(stringify!(! $e), file!(), line!(), 0));
98         }
99     };
100 }
101
102 macro_rules! assert_eq {
103     ($l:expr, $r: expr) => {
104         if $l != $r {
105             panic(&(stringify!($l != $r), file!(), line!(), 0));
106         }
107     }
108 }
109
110 struct Unique<T: ?Sized> {
111     pointer: *const T,
112     _marker: PhantomData<T>,
113 }
114
115 impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
116
117 fn take_f32(_f: f32) {}
118 fn take_unique(_u: Unique<()>) {}
119
120 fn main() {
121     take_unique(Unique {
122         pointer: 0 as *const (),
123         _marker: PhantomData,
124     });
125     take_f32(0.1);
126
127     //return;
128
129     unsafe {
130         printf("Hello %s\n\0" as *const str as *const i8, "printf\0" as *const str as *const i8);
131
132         let hello: &[u8] = b"Hello\0" as &[u8; 6];
133         let ptr: *const u8 = hello as *const [u8] as *const u8;
134         puts(ptr);
135
136         let world: Box<&str> = box "World!\0";
137         puts(*world as *const str as *const u8);
138         world as Box<SomeTrait>;
139
140         assert_eq!(intrinsics::bitreverse(0b10101000u8), 0b00010101u8);
141
142         assert_eq!(intrinsics::bswap(0xabu8), 0xabu8);
143         assert_eq!(intrinsics::bswap(0xddccu16), 0xccddu16);
144         assert_eq!(intrinsics::bswap(0xffee_ddccu32), 0xccdd_eeffu32);
145         assert_eq!(intrinsics::bswap(0x1234_5678_ffee_ddccu64), 0xccdd_eeff_7856_3412u64);
146
147         assert_eq!(intrinsics::size_of_val(hello) as u8, 6);
148
149         let chars = &['C', 'h', 'a', 'r', 's'];
150         let chars = chars as &[char];
151         assert_eq!(intrinsics::size_of_val(chars) as u8, 4 * 5);
152
153         let a: &dyn SomeTrait = &"abc\0";
154         a.object_safe();
155
156         assert_eq!(intrinsics::size_of_val(a) as u8, 16);
157         assert_eq!(intrinsics::size_of_val(&0u32) as u8, 4);
158
159         assert_eq!(intrinsics::min_align_of::<u16>() as u8, 2);
160         assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8);
161
162         assert!(!intrinsics::needs_drop::<u8>());
163         assert!(intrinsics::needs_drop::<NoisyDrop>());
164
165         Unique {
166             pointer: 0 as *const &str,
167             _marker: PhantomData,
168         } as Unique<dyn SomeTrait>;
169
170         struct MyDst<T: ?Sized>(T);
171
172         intrinsics::size_of_val(&MyDst([0u8; 4]) as &MyDst<[u8]>);
173
174         struct Foo {
175             x: u8,
176             y: !,
177         }
178
179         unsafe fn zeroed<T>() -> T {
180             intrinsics::init::<T>()
181         }
182
183         unsafe fn uninitialized<T>() -> T {
184             MaybeUninit { uninit: () }.value
185         }
186
187         zeroed::<(u8, u8)>();
188         #[allow(unreachable_code)]
189         {
190             if false {
191                 zeroed::<!>();
192                 zeroed::<Foo>();
193                 uninitialized::<Foo>();
194             }
195         }
196     }
197
198     let _ = box NoisyDrop {
199         text: "Boxed outer got dropped!\0",
200         inner: NoisyDropInner,
201     } as Box<SomeTrait>;
202
203     const FUNC_REF: Option<fn()> = Some(main);
204     match FUNC_REF {
205         Some(_) => {},
206         None => assert!(false),
207     }
208
209     match Ordering::Less {
210         Ordering::Less => {},
211         _ => assert!(false),
212     }
213
214     [NoisyDropInner, NoisyDropInner];
215
216     let x = &[0u32, 42u32] as &[u32];
217     match x {
218         [] => assert_eq!(0u32, 1),
219         [_, ref y @ ..] => assert_eq!(&x[1] as *const u32 as usize, &y[0] as *const u32 as usize),
220     }
221
222     assert_eq!(((|()| 42u8) as fn(()) -> u8)(()), 42);
223
224     extern {
225         #[linkage = "weak"]
226         static ABC: *const u8;
227     }
228
229     {
230         extern {
231             #[linkage = "weak"]
232             static ABC: *const u8;
233         }
234     }
235
236     unsafe { assert_eq!(ABC as usize, 0); }
237
238     &mut (|| Some(0 as *const ())) as &mut FnMut() -> Option<*const ()>;
239 }