rust/tests/ui/borrowck/borrowck-loan-rcvr.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
753 B
Rust
Raw Normal View History

struct Point { x: isize, y: isize }
trait Methods {
fn impurem(&self);
2015-01-03 15:45:00 +00:00
fn blockm<F>(&self, f: F) where F: FnOnce();
}
impl Methods for Point {
fn impurem(&self) {
}
2015-01-03 15:45:00 +00:00
fn blockm<F>(&self, f: F) where F: FnOnce() { f() }
}
fn a() {
let mut p = Point {x: 3, y: 4};
// Here: it's ok to call even though receiver is mutable, because we
// can loan it out.
p.impurem();
// But in this case we do not honor the loan:
p.blockm(|| { //~ ERROR cannot borrow `p` as mutable
p.x = 10;
})
}
fn b() {
let mut p = Point {x: 3, y: 4};
// Here I create an outstanding loan and check that we get conflicts:
2013-03-15 19:24:24 +00:00
let l = &mut p;
p.impurem(); //~ ERROR cannot borrow
l.x += 1;
}
fn main() {
}