2013-11-21 08:04:47 -06:00
|
|
|
// Test that attempt to mutably borrow `&mut` pointer while pointee is
|
|
|
|
// borrowed yields an error.
|
|
|
|
//
|
2020-08-27 22:58:48 -05:00
|
|
|
// Example from compiler/rustc_borrowck/borrowck/README.md
|
2013-11-21 08:04:47 -06:00
|
|
|
|
2018-08-14 18:16:05 -05:00
|
|
|
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn foo<'a>(mut t0: &'a mut isize,
|
|
|
|
mut t1: &'a mut isize) {
|
|
|
|
let p: &isize = &*t0; // Freezes `*t0`
|
2013-11-21 08:04:47 -06:00
|
|
|
let mut t2 = &mut t0; //~ ERROR cannot borrow `t0`
|
|
|
|
**t2 += 1; // Mutates `*t0`
|
2018-08-14 18:16:05 -05:00
|
|
|
p.use_ref();
|
2013-11-21 08:04:47 -06:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn bar<'a>(mut t0: &'a mut isize,
|
|
|
|
mut t1: &'a mut isize) {
|
|
|
|
let p: &mut isize = &mut *t0; // Claims `*t0`
|
2013-11-21 08:04:47 -06:00
|
|
|
let mut t2 = &mut t0; //~ ERROR cannot borrow `t0`
|
|
|
|
**t2 += 1; // Mutates `*t0` but not through `*p`
|
2018-08-14 18:16:05 -05:00
|
|
|
p.use_mut();
|
2013-11-21 08:04:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
2018-08-14 18:16:05 -05:00
|
|
|
|
|
|
|
trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { } }
|
|
|
|
impl<T> Fake for T { }
|