]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/stacked-borrows/2phase.rs
Rollup merge of #103084 - inquisitivecrystal:control-flow, r=scottmcm
[rust.git] / src / tools / miri / tests / pass / stacked-borrows / 2phase.rs
1 trait S: Sized {
2     fn tpb(&mut self, _s: Self) {}
3 }
4
5 impl S for i32 {}
6
7 fn two_phase1() {
8     let mut x = 3;
9     x.tpb(x);
10 }
11
12 fn two_phase2() {
13     let mut v = vec![];
14     v.push(v.len());
15 }
16
17 fn two_phase3(b: bool) {
18     let mut x = &mut vec![];
19     let mut y = vec![];
20     x.push((
21         {
22             if b {
23                 x = &mut y;
24             }
25             22
26         },
27         x.len(),
28     ));
29 }
30
31 #[allow(unreachable_code)]
32 fn two_phase_raw() {
33     let x: &mut Vec<i32> = &mut vec![];
34     x.push({
35         // Unfortunately this does not trigger the problem of creating a
36         // raw ponter from a pointer that had a two-phase borrow derived from
37         // it because of the implicit &mut reborrow.
38         let raw = x as *mut _;
39         unsafe {
40             *raw = vec![1];
41         }
42         return;
43     });
44 }
45
46 fn two_phase_overlapping1() {
47     let mut x = vec![];
48     let p = &x;
49     x.push(p.len());
50 }
51
52 fn two_phase_overlapping2() {
53     use std::ops::AddAssign;
54     let mut x = 1;
55     let l = &x;
56     x.add_assign(x + *l);
57 }
58
59 fn with_interior_mutability() {
60     use std::cell::Cell;
61
62     trait Thing: Sized {
63         fn do_the_thing(&mut self, _s: i32) {}
64     }
65
66     impl<T> Thing for Cell<T> {}
67
68     let mut x = Cell::new(1);
69     let l = &x;
70
71     x.do_the_thing({
72         x.set(3);
73         l.set(4);
74         x.get() + l.get()
75     });
76 }
77
78 fn main() {
79     two_phase1();
80     two_phase2();
81     two_phase3(false);
82     two_phase3(true);
83     two_phase_raw();
84     with_interior_mutability();
85     two_phase_overlapping1();
86     two_phase_overlapping2();
87 }