summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/const-eval/ub-write-through-immutable.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/consts/const-eval/ub-write-through-immutable.rs')
-rw-r--r--tests/ui/consts/const-eval/ub-write-through-immutable.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/ui/consts/const-eval/ub-write-through-immutable.rs b/tests/ui/consts/const-eval/ub-write-through-immutable.rs
new file mode 100644
index 000000000..945367f18
--- /dev/null
+++ b/tests/ui/consts/const-eval/ub-write-through-immutable.rs
@@ -0,0 +1,30 @@
+//! Ensure we catch UB due to writing through a shared reference.
+#![feature(const_mut_refs, const_refs_to_cell)]
+#![deny(writes_through_immutable_pointer)]
+#![allow(invalid_reference_casting)]
+
+use std::mem;
+use std::cell::UnsafeCell;
+
+const WRITE_AFTER_CAST: () = unsafe {
+ let mut x = 0;
+ let ptr = &x as *const i32 as *mut i32;
+ *ptr = 0; //~ERROR: writes_through_immutable_pointer
+ //~^ previously accepted
+};
+
+const WRITE_AFTER_TRANSMUTE: () = unsafe {
+ let mut x = 0;
+ let ptr: *mut i32 = mem::transmute(&x);
+ *ptr = 0; //~ERROR: writes_through_immutable_pointer
+ //~^ previously accepted
+};
+
+// it's okay when there is interior mutability;
+const WRITE_INTERIOR_MUT: () = unsafe {
+ let x = UnsafeCell::new(0);
+ let ptr = &x as *const _ as *mut i32;
+ *ptr = 0;
+};
+
+fn main() {}