Skip to content

Commit 4eac6fe

Browse files
committed
Add test for a.f(|this| a.g())
1 parent d7f9e81 commit 4eac6fe

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![allow(unused)]
2+
struct S;
3+
4+
impl S {
5+
fn call(&mut self, f: impl FnOnce((), &mut Self)) {
6+
// change state or something ...
7+
f((), self);
8+
// change state or something ...
9+
}
10+
11+
fn get(&self) {}
12+
fn set(&mut self) {}
13+
}
14+
15+
fn main() {
16+
let mut v = S;
17+
18+
v.call(|(), this: &mut S| v.get());
19+
//~^ error: cannot borrow `v` as mutable because it is also borrowed as immutable
20+
v.call(|(), this: &mut S| v.set());
21+
//~^ error: cannot borrow `v` as mutable more than once at a time
22+
//~| error: cannot borrow `v` as mutable more than once at a time
23+
24+
v.call(|(), this: &mut S| {
25+
//~^ error: cannot borrow `v` as mutable more than once at a time
26+
//~| error: cannot borrow `v` as mutable more than once at a time
27+
28+
_ = v;
29+
v.set();
30+
v.get();
31+
S::get(&v);
32+
33+
use std::ops::Add;
34+
let v = 0u32;
35+
_ = v + v;
36+
_ = v.add(3);
37+
});
38+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
2+
--> $DIR/issue-109271-pass-self-into-closure.rs:18:5
3+
|
4+
LL | v.call(|(), this: &mut S| v.get());
5+
| ^^----^------------------^-^^^^^^^
6+
| | | | |
7+
| | | | first borrow occurs due to use of `v` in closure
8+
| | | immutable borrow occurs here
9+
| | immutable borrow later used by call
10+
| mutable borrow occurs here
11+
12+
error[E0499]: cannot borrow `v` as mutable more than once at a time
13+
--> $DIR/issue-109271-pass-self-into-closure.rs:20:5
14+
|
15+
LL | v.call(|(), this: &mut S| v.set());
16+
| ^^----^------------------^-^^^^^^^
17+
| | | | |
18+
| | | | first borrow occurs due to use of `v` in closure
19+
| | | first mutable borrow occurs here
20+
| | first borrow later used by call
21+
| second mutable borrow occurs here
22+
23+
error[E0499]: cannot borrow `v` as mutable more than once at a time
24+
--> $DIR/issue-109271-pass-self-into-closure.rs:20:12
25+
|
26+
LL | v.call(|(), this: &mut S| v.set());
27+
| -------^^^^^^^^^^^^^^^^^^---------
28+
| | | | |
29+
| | | | second borrow occurs due to use of `v` in closure
30+
| | | second mutable borrow occurs here
31+
| | first borrow later used by call
32+
| first mutable borrow occurs here
33+
34+
error[E0499]: cannot borrow `v` as mutable more than once at a time
35+
--> $DIR/issue-109271-pass-self-into-closure.rs:24:5
36+
|
37+
LL | v.call(|(), this: &mut S| {
38+
| ^ ---- ------------------ first mutable borrow occurs here
39+
| | |
40+
| _____| first borrow later used by call
41+
| |
42+
LL | |
43+
LL | |
44+
LL | |
45+
LL | | _ = v;
46+
LL | | v.set();
47+
| | - first borrow occurs due to use of `v` in closure
48+
... |
49+
LL | | _ = v.add(3);
50+
LL | | });
51+
| |______^ second mutable borrow occurs here
52+
53+
error[E0499]: cannot borrow `v` as mutable more than once at a time
54+
--> $DIR/issue-109271-pass-self-into-closure.rs:24:12
55+
|
56+
LL | v.call(|(), this: &mut S| {
57+
| - ---- ^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
58+
| | |
59+
| _____| first borrow later used by call
60+
| |
61+
LL | |
62+
LL | |
63+
LL | |
64+
LL | | _ = v;
65+
LL | | v.set();
66+
| | - second borrow occurs due to use of `v` in closure
67+
... |
68+
LL | | _ = v.add(3);
69+
LL | | });
70+
| |______- first mutable borrow occurs here
71+
72+
error: aborting due to 5 previous errors
73+
74+
Some errors have detailed explanations: E0499, E0502.
75+
For more information about an error, try `rustc --explain E0499`.

0 commit comments

Comments
 (0)