]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trivial_casts.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / trivial_casts.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that all coercions can actually be done using casts (modulo the lints).
12
13 #![allow(trivial_casts, trivial_numeric_casts)]
14
15 trait Foo {
16     fn foo(&self) {}
17 }
18
19 pub struct Bar;
20
21 impl Foo for Bar {}
22
23 pub fn main() {
24     // Numeric
25     let _ = 42_i32 as i32;
26     let _ = 42_u8 as u8;
27
28     // & to * pointers
29     let x: &u32 = &42;
30     let _ = x as *const u32;
31
32     let x: &mut u32 = &mut 42;
33     let _ = x as *mut u32;
34
35     // unsize array
36     let x: &[u32; 3] = &[42, 43, 44];
37     let _ = x as &[u32];
38     let _ = x as *const [u32];
39
40     let x: &mut [u32; 3] = &mut [42, 43, 44];
41     let _ = x as &mut [u32];
42     let _ = x as *mut [u32];
43
44     let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
45     let _ = x as Box<[u32]>;
46
47     // unsize trait
48     let x: &Bar = &Bar;
49     let _ = x as &Foo;
50     let _ = x as *const Foo;
51
52     let x: &mut Bar = &mut Bar;
53     let _ = x as &mut Foo;
54     let _ = x as *mut Foo;
55
56     let x: Box<Bar> = Box::new(Bar);
57     let _ = x as Box<Foo>;
58
59     // functions
60     fn baz(_x: i32) {}
61     let _ = &baz as &Fn(i32);
62     let x = |_x: i32| {};
63     let _ = &x as &Fn(i32);
64 }
65
66 // subtyping
67 pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
68     let _ = a as &'a Bar;
69     let _ = b as &'a Bar;
70     let _ = b as &'b Bar;
71 }