summaryrefslogtreecommitdiffstats
path: root/tests/ui/lint/reference_casting.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
commitdc0db358abe19481e475e10c32149b53370f1a1c (patch)
treeab8ce99c4b255ce46f99ef402c27916055b899ee /tests/ui/lint/reference_casting.rs
parentReleasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff)
downloadrustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz
rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/lint/reference_casting.rs')
-rw-r--r--tests/ui/lint/reference_casting.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/ui/lint/reference_casting.rs b/tests/ui/lint/reference_casting.rs
new file mode 100644
index 000000000..996382049
--- /dev/null
+++ b/tests/ui/lint/reference_casting.rs
@@ -0,0 +1,51 @@
+// check-fail
+
+#![feature(ptr_from_ref)]
+#![deny(invalid_reference_casting)]
+
+extern "C" {
+ // N.B., mutability can be easily incorrect in FFI calls -- as
+ // in C, the default is mutable pointers.
+ fn ffi(c: *mut u8);
+ fn int_ffi(c: *mut i32);
+}
+
+fn main() {
+ let s = String::from("Hello");
+ let a = &s;
+ unsafe {
+ let num = &3i32;
+ let mut_num = &mut 3i32;
+
+ (*(a as *const _ as *mut String)).push_str(" world");
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ *(a as *const _ as *mut _) = String::from("Replaced");
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ *(a as *const _ as *mut String) += " world";
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ let _num = &mut *(num as *const i32 as *mut i32);
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ let _num = &mut *(num as *const i32).cast_mut();
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ let _num = *{ num as *const i32 }.cast_mut();
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ *std::ptr::from_ref(num).cast_mut() += 1;
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ *std::ptr::from_ref({ num }).cast_mut() += 1;
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ *{ std::ptr::from_ref(num) }.cast_mut() += 1;
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+ *(std::ptr::from_ref({ num }) as *mut i32) += 1;
+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
+
+ // Shouldn't be warned against
+ println!("{}", *(num as *const _ as *const i16));
+ println!("{}", *(mut_num as *mut _ as *mut i16));
+ ffi(a.as_ptr() as *mut _);
+ int_ffi(num as *const _ as *mut _);
+ int_ffi(&3 as *const _ as *mut _);
+ let mut value = 3;
+ let value: *const i32 = &mut value;
+ *(value as *const i16 as *mut i16) = 42;
+ }
+}