]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-loan-rcvr.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-loan-rcvr.rs
1 struct Point { x: isize, y: isize }
2
3 trait Methods {
4     fn impurem(&self);
5     fn blockm<F>(&self, f: F) where F: FnOnce();
6 }
7
8 impl Methods for Point {
9     fn impurem(&self) {
10     }
11
12     fn blockm<F>(&self, f: F) where F: FnOnce() { f() }
13 }
14
15 fn a() {
16     let mut p = Point {x: 3, y: 4};
17
18     // Here: it's ok to call even though receiver is mutable, because we
19     // can loan it out.
20     p.impurem();
21
22     // But in this case we do not honor the loan:
23     p.blockm(|| { //~ ERROR cannot borrow `p` as mutable
24         p.x = 10;
25     })
26 }
27
28 fn b() {
29     let mut p = Point {x: 3, y: 4};
30
31     // Here I create an outstanding loan and check that we get conflicts:
32
33     let l = &mut p;
34     p.impurem(); //~ ERROR cannot borrow
35
36     l.x += 1;
37 }
38
39 fn main() {
40 }