summaryrefslogtreecommitdiffstats
path: root/src/test/ui/unsafe/unsafe-borrow.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/unsafe/unsafe-borrow.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/unsafe/unsafe-borrow.rs')
-rw-r--r--src/test/ui/unsafe/unsafe-borrow.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/test/ui/unsafe/unsafe-borrow.rs b/src/test/ui/unsafe/unsafe-borrow.rs
new file mode 100644
index 000000000..8dddc70be
--- /dev/null
+++ b/src/test/ui/unsafe/unsafe-borrow.rs
@@ -0,0 +1,56 @@
+// revisions: mirunsafeck thirunsafeck
+// [thirunsafeck]compile-flags: -Z thir-unsafeck
+
+#![feature(rustc_attrs)]
+#![allow(unused,dead_code)]
+
+fn tuple_struct() {
+ #[rustc_layout_scalar_valid_range_start(1)]
+ struct NonZero<T>(T);
+
+ let mut foo = unsafe { NonZero((1,)) };
+ let a = &mut foo.0.0;
+ //~^ ERROR: mutation of layout constrained field is unsafe
+}
+
+fn slice() {
+ #[rustc_layout_scalar_valid_range_start(1)]
+ struct NonZero<'a, T>(&'a mut [T]);
+
+ let mut nums = [1, 2, 3, 4];
+ let mut foo = unsafe { NonZero(&mut nums[..]) };
+ let a = &mut foo.0[2];
+ // ^ not unsafe because there is an implicit dereference here
+}
+
+fn array() {
+ #[rustc_layout_scalar_valid_range_start(1)]
+ struct NonZero<T>([T; 4]);
+
+ let nums = [1, 2, 3, 4];
+ let mut foo = unsafe { NonZero(nums) };
+ let a = &mut foo.0[2];
+ //~^ ERROR: mutation of layout constrained field is unsafe
+}
+
+fn block() {
+ #[rustc_layout_scalar_valid_range_start(1)]
+ struct NonZero<T>(T);
+
+ let foo = unsafe { NonZero((1,)) };
+ &mut { foo.0 }.0;
+ // ^ not unsafe because the result of the block expression is a new place
+}
+
+fn mtch() {
+ #[rustc_layout_scalar_valid_range_start(1)]
+ struct NonZero<T>(T);
+
+ let mut foo = unsafe { NonZero((1,)) };
+ match &mut foo {
+ NonZero((a,)) => *a = 0,
+ //~^ ERROR: mutation of layout constrained field is unsafe
+ }
+}
+
+fn main() {}