]> git.lizzy.rs Git - rust.git/blob - example/mini_core_hello_world.rs
Implement line debuginfo
[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)]
4 #![no_core]
5 #![allow(dead_code)]
6
7 extern crate mini_core;
8
9 use mini_core::*;
10
11 #[link(name = "c")]
12 extern "C" {
13     fn puts(s: *const u8);
14 }
15
16 unsafe extern "C" fn my_puts(s: *const u8) {
17     puts(s);
18 }
19
20 // TODO remove when jit supports linking rlibs
21 #[cfg(jit)]
22 extern "C" fn panic<T>(_: T) -> ! {
23     unsafe {
24         intrinsics::abort();
25     }
26 }
27
28 #[lang = "termination"]
29 trait Termination {
30     fn report(self) -> i32;
31 }
32
33 impl Termination for () {
34     fn report(self) -> i32 {
35         unsafe {
36             NUM = 6 * 7 + 1 + (1u8 == 1u8) as u8; // 44
37             *NUM_REF as i32
38         }
39     }
40 }
41
42 trait SomeTrait {
43     fn object_safe(&self);
44 }
45
46 impl SomeTrait for &'static str {
47     fn object_safe(&self) {
48         unsafe {
49             puts(*self as *const str as *const u8);
50         }
51     }
52 }
53
54 struct NoisyDrop {
55     text: &'static str,
56     inner: NoisyDropInner,
57 }
58
59 struct NoisyDropInner;
60
61 impl Drop for NoisyDrop {
62     fn drop(&mut self) {
63         unsafe {
64             puts(self.text as *const str as *const u8);
65         }
66     }
67 }
68
69 impl Drop for NoisyDropInner {
70     fn drop(&mut self) {
71         unsafe {
72             puts("Inner got dropped!\0" as *const str as *const u8);
73         }
74     }
75 }
76
77 enum Ordering {
78     Less = -1,
79     Equal = 0,
80     Greater = 1,
81 }
82
83 #[lang = "start"]
84 fn start<T: Termination + 'static>(
85     main: fn() -> T,
86     _argc: isize,
87     _argv: *const *const u8,
88 ) -> isize {
89     main().report() as isize
90 }
91
92 static mut NUM: u8 = 6 * 7;
93 static NUM_REF: &'static u8 = unsafe { &NUM };
94
95 macro_rules! assert {
96     ($e:expr) => {
97         if !$e {
98             panic(&(stringify!(! $e), file!(), line!(), 0));
99         }
100     };
101 }
102
103 macro_rules! assert_eq {
104     ($l:expr, $r: expr) => {
105         if $l != $r {
106             panic(&(stringify!($l != $r), file!(), line!(), 0));
107         }
108     }
109 }
110
111 struct Unique<T: ?Sized> {
112     pointer: *const T,
113     _marker: PhantomData<T>,
114 }
115
116 impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
117
118 fn take_f32(f: f32) {}
119 fn take_unique(u: Unique<()>) {}
120
121 fn main() {
122     take_unique(Unique {
123         pointer: 0 as *const (),
124         _marker: PhantomData,
125     });
126     take_f32(0.1);
127
128     //return;
129
130     unsafe {
131         let hello: &[u8] = b"Hello\0" as &[u8; 6];
132         let ptr: *const u8 = hello as *const [u8] as *const u8;
133         puts(ptr);
134
135         // TODO remove when jit supports linking rlibs
136         #[cfg(not(jit))]
137         {
138             let world: Box<&str> = box "World!\0";
139             puts(*world as *const str as *const u8);
140             world as Box<SomeTrait>;
141         }
142
143         assert_eq!(intrinsics::size_of_val(hello) as u8, 6);
144
145         let chars = &['C', 'h', 'a', 'r', 's'];
146         let chars = chars as &[char];
147         assert_eq!(intrinsics::size_of_val(chars) as u8, 4 * 5);
148
149         let a: &dyn SomeTrait = &"abc\0";
150         a.object_safe();
151
152         assert_eq!(intrinsics::size_of_val(a) as u8, 16);
153         assert_eq!(intrinsics::size_of_val(&0u32) as u8, 4);
154
155         assert_eq!(intrinsics::min_align_of::<u16>() as u8, 2);
156         assert_eq!(intrinsics::min_align_of_val(&a) as u8, intrinsics::min_align_of::<&str>() as u8);
157
158         assert!(!intrinsics::needs_drop::<u8>());
159         assert!(intrinsics::needs_drop::<NoisyDrop>());
160
161         Unique {
162             pointer: 0 as *const &str,
163             _marker: PhantomData,
164         } as Unique<dyn SomeTrait>;
165
166         struct MyDst<T: ?Sized>(T);
167
168         intrinsics::size_of_val(&MyDst([0u8; 4]) as &MyDst<[u8]>);
169     }
170
171     let _ = NoisyDrop {
172         text: "Outer got dropped!\0",
173         inner: NoisyDropInner,
174     };
175
176     const FUNC_REF: Option<fn()> = Some(main);
177     match FUNC_REF {
178         Some(_) => {},
179         None => assert!(false),
180     }
181
182     match Ordering::Less {
183         Ordering::Less => {},
184         _ => assert!(false),
185     }
186
187     [NoisyDropInner, NoisyDropInner];
188 }