1 // run-pass
2 // Test that all coercions can actually be done using casts (modulo the lints).
3 
4 #![allow(trivial_casts, trivial_numeric_casts)]
5 
6 trait Foo {
foo(&self)7     fn foo(&self) {}
8 }
9 
10 pub struct Bar;
11 
12 impl Foo for Bar {}
13 
main()14 pub fn main() {
15     // Numeric
16     let _ = 42_i32 as i32;
17     let _ = 42_u8 as u8;
18 
19     // & to * pointers
20     let x: &u32 = &42;
21     let _ = x as *const u32;
22 
23     let x: &mut u32 = &mut 42;
24     let _ = x as *mut u32;
25 
26     // unsize array
27     let x: &[u32; 3] = &[42, 43, 44];
28     let _ = x as &[u32];
29     let _ = x as *const [u32];
30 
31     let x: &mut [u32; 3] = &mut [42, 43, 44];
32     let _ = x as &mut [u32];
33     let _ = x as *mut [u32];
34 
35     let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
36     let _ = x as Box<[u32]>;
37 
38     // unsize trait
39     let x: &Bar = &Bar;
40     let _ = x as &dyn Foo;
41     let _ = x as *const dyn Foo;
42 
43     let x: &mut Bar = &mut Bar;
44     let _ = x as &mut dyn Foo;
45     let _ = x as *mut dyn Foo;
46 
47     let x: Box<Bar> = Box::new(Bar);
48     let _ = x as Box<dyn Foo>;
49 
50     // functions
51     fn baz(_x: i32) {}
52     let _ = &baz as &dyn Fn(i32);
53     let x = |_x: i32| {};
54     let _ = &x as &dyn Fn(i32);
55 }
56 
57 // subtyping
test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar)58 pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
59     let _ = a as &'a Bar;
60     let _ = b as &'a Bar;
61     let _ = b as &'b Bar;
62 }
63