summaryrefslogtreecommitdiffstats
path: root/src/test/ui/trivial_casts-rpass.rs
blob: 8e49468bf0c455db10f0db0c32c39875118acd0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// run-pass
// Test that all coercions can actually be done using casts (modulo the lints).

#![allow(trivial_casts, trivial_numeric_casts)]

trait Foo {
    fn foo(&self) {}
}

pub struct Bar;

impl Foo for Bar {}

pub fn main() {
    // Numeric
    let _ = 42_i32 as i32;
    let _ = 42_u8 as u8;

    // & to * pointers
    let x: &u32 = &42;
    let _ = x as *const u32;

    let x: &mut u32 = &mut 42;
    let _ = x as *mut u32;

    // unsize array
    let x: &[u32; 3] = &[42, 43, 44];
    let _ = x as &[u32];
    let _ = x as *const [u32];

    let x: &mut [u32; 3] = &mut [42, 43, 44];
    let _ = x as &mut [u32];
    let _ = x as *mut [u32];

    let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
    let _ = x as Box<[u32]>;

    // unsize trait
    let x: &Bar = &Bar;
    let _ = x as &dyn Foo;
    let _ = x as *const dyn Foo;

    let x: &mut Bar = &mut Bar;
    let _ = x as &mut dyn Foo;
    let _ = x as *mut dyn Foo;

    let x: Box<Bar> = Box::new(Bar);
    let _ = x as Box<dyn Foo>;

    // functions
    fn baz(_x: i32) {}
    let _ = &baz as &dyn Fn(i32);
    let x = |_x: i32| {};
    let _ = &x as &dyn Fn(i32);
}

// subtyping
pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
    let _ = a as &'a Bar;
    let _ = b as &'a Bar;
    let _ = b as &'b Bar;
}