]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-mut-borrow-of-mut-base-ptr.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-mut-borrow-of-mut-base-ptr.rs
1 // Test that attempt to mutably borrow `&mut` pointer while pointee is
2 // borrowed yields an error.
3 //
4 // Example from compiler/rustc_borrowck/borrowck/README.md
5
6
7
8 fn foo<'a>(mut t0: &'a mut isize,
9            mut t1: &'a mut isize) {
10     let p: &isize = &*t0;     // Freezes `*t0`
11     let mut t2 = &mut t0;   //~ ERROR cannot borrow `t0`
12     **t2 += 1;              // Mutates `*t0`
13     p.use_ref();
14 }
15
16 fn bar<'a>(mut t0: &'a mut isize,
17            mut t1: &'a mut isize) {
18     let p: &mut isize = &mut *t0; // Claims `*t0`
19     let mut t2 = &mut t0;       //~ ERROR cannot borrow `t0`
20     **t2 += 1;                  // Mutates `*t0` but not through `*p`
21     p.use_mut();
22 }
23
24 fn main() {
25 }
26
27 trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { }  }
28 impl<T> Fake for T { }