]> git.proxmox.com Git - rustc.git/blame - src/test/ui/unsafe/unsafe-borrow.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / src / test / ui / unsafe / unsafe-borrow.rs
CommitLineData
136023e0
XL
1// revisions: mirunsafeck thirunsafeck
2// [thirunsafeck]compile-flags: -Z thir-unsafeck
3
4#![feature(rustc_attrs)]
5#![allow(unused,dead_code)]
6
7fn tuple_struct() {
8 #[rustc_layout_scalar_valid_range_start(1)]
9 struct NonZero<T>(T);
10
11 let mut foo = unsafe { NonZero((1,)) };
12 let a = &mut foo.0.0;
13 //~^ ERROR: mutation of layout constrained field is unsafe
14}
15
16fn slice() {
17 #[rustc_layout_scalar_valid_range_start(1)]
18 struct NonZero<'a, T>(&'a mut [T]);
19
20 let mut nums = [1, 2, 3, 4];
21 let mut foo = unsafe { NonZero(&mut nums[..]) };
22 let a = &mut foo.0[2];
23 // ^ not unsafe because there is an implicit dereference here
24}
25
26fn array() {
27 #[rustc_layout_scalar_valid_range_start(1)]
28 struct NonZero<T>([T; 4]);
29
30 let nums = [1, 2, 3, 4];
31 let mut foo = unsafe { NonZero(nums) };
32 let a = &mut foo.0[2];
33 //~^ ERROR: mutation of layout constrained field is unsafe
34}
35
36fn block() {
37 #[rustc_layout_scalar_valid_range_start(1)]
38 struct NonZero<T>(T);
39
40 let foo = unsafe { NonZero((1,)) };
41 &mut { foo.0 }.0;
42 // ^ not unsafe because the result of the block expression is a new place
43}
44
45fn mtch() {
46 #[rustc_layout_scalar_valid_range_start(1)]
47 struct NonZero<T>(T);
48
49 let mut foo = unsafe { NonZero((1,)) };
50 match &mut foo {
51 NonZero((a,)) => *a = 0,
52 //~^ ERROR: mutation of layout constrained field is unsafe
53 }
54}
55
56fn main() {}