]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-lend-flow-if.rs
Auto merge of #106458 - albertlarsan68:move-tests, r=jyn514
[rust.git] / tests / ui / borrowck / borrowck-lend-flow-if.rs
1 // Note: the borrowck analysis is currently flow-insensitive.
2 // Therefore, some of these errors are marked as spurious and could be
3 // corrected by a simple change to the analysis.  The others are
4 // either genuine or would require more advanced changes.  The latter
5 // cases are noted.
6
7
8
9 fn borrow(_v: &isize) {}
10 fn borrow_mut(_v: &mut isize) {}
11 fn cond() -> bool { panic!() }
12 fn for_func<F>(_f: F) where F: FnOnce() -> bool { panic!() }
13 fn produce<T>() -> T { panic!(); }
14
15 fn inc(v: &mut Box<isize>) {
16     *v = Box::new(**v + 1);
17 }
18
19 fn pre_freeze_cond() {
20     // In this instance, the freeze is conditional and starts before
21     // the mut borrow.
22
23     let u = Box::new(0);
24     let mut v: Box<_> = Box::new(3);
25     let mut _w = &u;
26     if cond() {
27         _w = &v;
28     }
29     borrow_mut(&mut *v); //~ ERROR cannot borrow
30     _w.use_ref();
31 }
32
33 fn pre_freeze_else() {
34     // In this instance, the freeze and mut borrow are on separate sides
35     // of the if.
36
37     let u = Box::new(0);
38     let mut v: Box<_> = Box::new(3);
39     let mut _w = &u;
40     if cond() {
41         _w = &v;
42     } else {
43         borrow_mut(&mut *v);
44     }
45     _w.use_ref();
46 }
47
48 fn main() {}
49
50 trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { }  }
51 impl<T> Fake for T { }