]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/transmute.rs
Added crosspointer transmute error and tests
[rust.git] / tests / compile-fail / transmute.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 extern crate core;
5
6 use std::mem::transmute as my_transmute;
7 use std::vec::Vec as MyVec;
8
9 fn my_vec() -> MyVec<i32> {
10     vec![]
11 }
12
13 #[allow(needless_lifetimes)]
14 #[deny(useless_transmute)]
15 unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
16     let _: &'a T = core::intrinsics::transmute(t);
17     //~^ ERROR transmute from a type (`&'a T`) to itself
18
19     let _: &'a U = core::intrinsics::transmute(t);
20 }
21
22 #[deny(useless_transmute)]
23 fn useless() {
24     unsafe {
25         let _: Vec<i32> = core::intrinsics::transmute(my_vec());
26         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
27
28         let _: Vec<i32> = core::mem::transmute(my_vec());
29         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
30
31         let _: Vec<i32> = std::intrinsics::transmute(my_vec());
32         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
33
34         let _: Vec<i32> = std::mem::transmute(my_vec());
35         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
36
37         let _: Vec<i32> = my_transmute(my_vec());
38         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
39
40         let _: Vec<u32> = core::intrinsics::transmute(my_vec());
41         let _: Vec<u32> = core::mem::transmute(my_vec());
42         let _: Vec<u32> = std::intrinsics::transmute(my_vec());
43         let _: Vec<u32> = std::mem::transmute(my_vec());
44         let _: Vec<u32> = my_transmute(my_vec());
45     }
46 }
47
48 #[deny(crosspointer_transmute)]
49 fn crosspointer() {
50     let mut vec: Vec<i32> = vec![];
51     let vec_const_ptr: *const Vec<i32> = &vec as *const Vec<i32>;
52     let vec_mut_ptr: *mut Vec<i32> = &mut vec as *mut Vec<i32>;
53
54     unsafe {
55         let _: Vec<i32> = core::intrinsics::transmute(vec_const_ptr);
56         //~^ ERROR transmute from a type (`*const collections::vec::Vec<i32>`) to the type that it points to (`collections::vec::Vec<i32>`)
57
58         let _: Vec<i32> = core::intrinsics::transmute(vec_mut_ptr);
59         //~^ ERROR transmute from a type (`*mut collections::vec::Vec<i32>`) to the type that it points to (`collections::vec::Vec<i32>`)
60
61         let _: *const Vec<i32> = core::intrinsics::transmute(my_vec());
62         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to a pointer to that type (`*const collections::vec::Vec<i32>`)
63
64         let _: *mut Vec<i32> = core::intrinsics::transmute(my_vec());
65         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to a pointer to that type (`*mut collections::vec::Vec<i32>`)
66     }
67 }
68
69 fn main() {
70     useless();
71     crosspointer();
72 }