-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #106641 - chenyukang:yukang/fix-105761-segguest-this,…
… r=estebank Provide help on closures capturing self causing borrow checker errors Fixes #105761 r? ````@estebank````
- Loading branch information
Showing
5 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/ui/suggestions/issue-105761-suggest-self-for-closure.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//run-rustfix | ||
#![allow(unused)] | ||
|
||
struct S; | ||
impl S { | ||
fn foo(&mut self) { | ||
let x = |this: &Self, v: i32| { | ||
this.bar(); | ||
this.hel(); | ||
}; | ||
self.qux(); //~ ERROR cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
x(self, 1); | ||
x(self, 3); | ||
} | ||
fn bar(&self) {} | ||
fn hel(&self) {} | ||
fn qux(&mut self) {} | ||
|
||
fn hello(&mut self) { | ||
let y = |this: &Self| { | ||
this.bar(); | ||
}; | ||
self.qux(); //~ ERROR cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
y(self); | ||
} | ||
} | ||
|
||
fn main() {} |
28 changes: 28 additions & 0 deletions
28
tests/ui/suggestions/issue-105761-suggest-self-for-closure.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//run-rustfix | ||
#![allow(unused)] | ||
|
||
struct S; | ||
impl S { | ||
fn foo(&mut self) { | ||
let x = |v: i32| { | ||
self.bar(); | ||
self.hel(); | ||
}; | ||
self.qux(); //~ ERROR cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
x(1); | ||
x(3); | ||
} | ||
fn bar(&self) {} | ||
fn hel(&self) {} | ||
fn qux(&mut self) {} | ||
|
||
fn hello(&mut self) { | ||
let y = || { | ||
self.bar(); | ||
}; | ||
self.qux(); //~ ERROR cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
y(); | ||
} | ||
} | ||
|
||
fn main() {} |
49 changes: 49 additions & 0 deletions
49
tests/ui/suggestions/issue-105761-suggest-self-for-closure.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
--> $DIR/issue-105761-suggest-self-for-closure.rs:11:9 | ||
| | ||
LL | let x = |v: i32| { | ||
| -------- immutable borrow occurs here | ||
LL | self.bar(); | ||
| ---- first borrow occurs due to use of `self` in closure | ||
... | ||
LL | self.qux(); | ||
| ^^^^^^^^^^ mutable borrow occurs here | ||
LL | x(1); | ||
| - immutable borrow later used here | ||
| | ||
help: try explicitly pass `&Self` into the Closure as an argument | ||
| | ||
LL ~ let x = |this: &Self, v: i32| { | ||
LL ~ this.bar(); | ||
LL ~ this.hel(); | ||
LL | }; | ||
LL | self.qux(); | ||
LL ~ x(self, 1); | ||
LL ~ x(self, 3); | ||
| | ||
|
||
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
--> $DIR/issue-105761-suggest-self-for-closure.rs:23:9 | ||
| | ||
LL | let y = || { | ||
| -- immutable borrow occurs here | ||
LL | self.bar(); | ||
| ---- first borrow occurs due to use of `self` in closure | ||
LL | }; | ||
LL | self.qux(); | ||
| ^^^^^^^^^^ mutable borrow occurs here | ||
LL | y(); | ||
| - immutable borrow later used here | ||
| | ||
help: try explicitly pass `&Self` into the Closure as an argument | ||
| | ||
LL ~ let y = |this: &Self| { | ||
LL ~ this.bar(); | ||
LL | }; | ||
LL | self.qux(); | ||
LL ~ y(self); | ||
| | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0502`. |