summaryrefslogtreecommitdiffstats
path: root/tests/ui/trivial_casts-rpass.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/trivial_casts-rpass.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/trivial_casts-rpass.rs')
-rw-r--r--tests/ui/trivial_casts-rpass.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/ui/trivial_casts-rpass.rs b/tests/ui/trivial_casts-rpass.rs
new file mode 100644
index 000000000..8e49468bf
--- /dev/null
+++ b/tests/ui/trivial_casts-rpass.rs
@@ -0,0 +1,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;
+}