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