]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/unsafe_ptr.rs
Move /src/test to /tests
[rust.git] / tests / ui / closures / 2229_closure_analysis / unsafe_ptr.rs
1 // edition:2021
2
3 // Test that we restrict precision of a capture when we access a raw ptr,
4 // i.e. the capture doesn't deref the raw ptr.
5
6
7 #![feature(rustc_attrs)]
8
9 #[derive(Debug)]
10 struct S {
11     s: String,
12     t: String,
13 }
14
15 struct T(*const S);
16
17 fn unsafe_imm() {
18     let s = "".into();
19     let t = "".into();
20     let my_speed: Box<S> = Box::new(S { s, t });
21
22     let p : *const S = Box::into_raw(my_speed);
23     let t = T(p);
24
25     let c = #[rustc_capture_analysis]
26     //~^ ERROR: attributes on expressions are experimental
27     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
28      || unsafe {
29     //~^ ERROR: First Pass analysis includes:
30     //~| ERROR: Min Capture analysis includes:
31         println!("{:?}", (*t.0).s);
32         //~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
33         //~| NOTE: Min Capture t[(0, 0)] -> ImmBorrow
34     };
35
36     c();
37 }
38
39 fn unsafe_mut() {
40     let s = "".into();
41     let t = "".into();
42     let mut my_speed: Box<S> = Box::new(S { s, t });
43     let p : *mut S = &mut *my_speed;
44
45     let c = #[rustc_capture_analysis]
46     //~^ ERROR: attributes on expressions are experimental
47     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
48     || {
49     //~^ ERROR: First Pass analysis includes:
50     //~| ERROR: Min Capture analysis includes:
51         let x = unsafe { &mut (*p).s };
52         //~^ NOTE: Capturing p[Deref,(0, 0)] -> ImmBorrow
53         //~| NOTE: Min Capture p[] -> ImmBorrow
54         *x = "s".into();
55     };
56     c();
57 }
58
59 fn main() {
60     unsafe_mut();
61     unsafe_imm();
62 }