rust/tests/ui/trivial_casts-rpass.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.3 KiB
Rust
Raw Normal View History

// run-pass
2015-03-23 04:28:57 +00:00
// Test that all coercions can actually be done using casts (modulo the lints).
2015-03-23 22:23:34 +00:00
#![allow(trivial_casts, trivial_numeric_casts)]
2015-03-23 04:28:57 +00:00
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;
2019-05-28 18:47:21 +00:00
let _ = x as &dyn Foo;
let _ = x as *const dyn Foo;
2015-03-23 04:28:57 +00:00
let x: &mut Bar = &mut Bar;
2019-05-28 18:47:21 +00:00
let _ = x as &mut dyn Foo;
let _ = x as *mut dyn Foo;
2015-03-23 04:28:57 +00:00
let x: Box<Bar> = Box::new(Bar);
2019-05-28 18:47:21 +00:00
let _ = x as Box<dyn Foo>;
2015-03-23 04:28:57 +00:00
// functions
fn baz(_x: i32) {}
2019-05-28 18:47:21 +00:00
let _ = &baz as &dyn Fn(i32);
2015-03-23 04:28:57 +00:00
let x = |_x: i32| {};
2019-05-28 18:47:21 +00:00
let _ = &x as &dyn Fn(i32);
2015-03-23 04:28:57 +00:00
}
// 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;
}