]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/errors.rs
use compiletest_rs
[rust.git] / tests / compile-fail / errors.rs
1 #![feature(custom_attribute)]
2 #![allow(dead_code, unused_attributes)]
3
4 #[miri_run]
5 fn overwriting_part_of_relocation_makes_the_rest_undefined() -> i32 {
6     let mut p = &42;
7     unsafe {
8         let ptr: *mut _ = &mut p;
9         *(ptr as *mut u32) = 123;
10     }
11     *p //~ ERROR: attempted to read undefined bytes
12 }
13
14 #[miri_run]
15 fn pointers_to_different_allocations_are_unorderable() -> bool {
16     let x: *const u8 = &1;
17     let y: *const u8 = &2;
18     x < y //~ ERROR: attempted to do math or a comparison on pointers into different allocations
19 }
20
21 #[miri_run]
22 fn invalid_bool() -> u8 {
23     let b = unsafe { std::mem::transmute::<u8, bool>(2) };
24     if b { 1 } else { 2 } //~ ERROR: invalid boolean value read
25 }
26
27 #[miri_run]
28 fn undefined_byte_read() -> u8 {
29     let v: Vec<u8> = Vec::with_capacity(10);
30     let undef = unsafe { *v.get_unchecked(5) };
31     undef + 1 //~ ERROR: attempted to read undefined bytes
32 }
33
34 #[miri_run]
35 fn out_of_bounds_read() -> u8 {
36     let v: Vec<u8> = vec![1, 2];
37     unsafe { *v.get_unchecked(5) } //~ ERROR: pointer offset outside bounds of allocation
38 }
39
40 #[miri_run]
41 fn dangling_pointer_deref() -> i32 {
42     let p = {
43         let b = Box::new(42);
44         &*b as *const i32
45     };
46     unsafe { *p } //~ ERROR: dangling pointer was dereferenced
47 }
48
49 fn main() {}