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