]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-lend-flow-loop.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-lend-flow-loop.rs
1 fn borrow(_v: &isize) {}
2 fn borrow_mut(_v: &mut isize) {}
3 fn cond() -> bool { panic!() }
4 fn produce<T>() -> T { panic!(); }
5
6
7 fn inc(v: &mut Box<isize>) {
8     *v = Box::new(**v + 1);
9 }
10
11
12 fn loop_overarching_alias_mut() {
13     // In this instance, the borrow ends on the line before the loop
14
15     let mut v: Box<_> = Box::new(3);
16     let mut x = &mut v;
17     **x += 1;
18     loop {
19         borrow(&*v); // OK
20     }
21 }
22
23 fn block_overarching_alias_mut() {
24     // In this instance, the borrow encompasses the entire closure call.
25
26     let mut v: Box<_> = Box::new(3);
27     let mut x = &mut v;
28     for _ in 0..3 {
29         borrow(&*v); //~ ERROR cannot borrow
30     }
31     *x = Box::new(5);
32 }
33 fn loop_aliased_mut() {
34     // In this instance, the borrow ends right after each assignment to _x
35
36     let mut v: Box<_> = Box::new(3);
37     let mut w: Box<_> = Box::new(4);
38     let mut _x = &w;
39     loop {
40         borrow_mut(&mut *v); // OK
41         _x = &v;
42     }
43 }
44
45 fn while_aliased_mut() {
46     // In this instance, the borrow ends right after each assignment to _x
47
48     let mut v: Box<_> = Box::new(3);
49     let mut w: Box<_> = Box::new(4);
50     let mut _x = &w;
51     while cond() {
52         borrow_mut(&mut *v); // OK
53         _x = &v;
54     }
55 }
56
57
58 fn loop_aliased_mut_break() {
59     // In this instance, the borrow ends right after each assignment to _x
60
61     let mut v: Box<_> = Box::new(3);
62     let mut w: Box<_> = Box::new(4);
63     let mut _x = &w;
64     loop {
65         borrow_mut(&mut *v);
66         _x = &v;
67         break;
68     }
69     borrow_mut(&mut *v); // OK
70 }
71
72 fn while_aliased_mut_break() {
73     // In this instance, the borrow ends right after each assignment to _x
74
75     let mut v: Box<_> = Box::new(3);
76     let mut w: Box<_> = Box::new(4);
77     let mut _x = &w;
78     while cond() {
79         borrow_mut(&mut *v);
80         _x = &v;
81         break;
82     }
83     borrow_mut(&mut *v); // OK
84 }
85
86 fn while_aliased_mut_cond(cond: bool, cond2: bool) {
87     let mut v: Box<_> = Box::new(3);
88     let mut w: Box<_> = Box::new(4);
89     let mut x = &mut w;
90     while cond {
91         **x += 1;
92         borrow(&*v); //~ ERROR cannot borrow
93         if cond2 {
94             x = &mut v; // OK
95         }
96     }
97 }
98 fn loop_break_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F) where
99     F: FnMut(&'r mut usize) -> bool,
100 {
101     // Here we check that when you break out of an inner loop, the
102     // borrows that go out of scope as you exit the inner loop are
103     // removed from the bitset.
104
105     while cond() {
106         while cond() {
107             // this borrow is limited to the scope of `r`...
108             let r: &'r mut usize = produce();
109             if !f(&mut *r) {
110                 break; // ...so it is not live as exit the `while` loop here
111             }
112         }
113     }
114 }
115
116 fn loop_loop_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F)
117     where F: FnMut(&'r mut usize) -> bool
118 {
119     // Similar to `loop_break_pops_scopes` but for the `loop` keyword
120
121     while cond() {
122         while cond() {
123             // this borrow is limited to the scope of `r`...
124             let r: &'r mut usize = produce();
125             if !f(&mut *r) {
126                 continue; // ...so it is not live as exit (and re-enter) the `while` loop here
127             }
128         }
129     }
130 }
131
132 fn main() {}