-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[borrowck] Fix help on mutating &self in async fns
Previously, when rustc was provided an async function that tried to mutate through a shared reference to an implicit self (as shown in the ui test), rustc would suggest modifying the parameter signature to `&mut` + the fully qualified name of the ty (in the case of the repro `S`). If a user modified their code to match the suggestion, the compiler would not accept it. This commit modifies the suggestion so that when rustc is provided the ui test that is also attached in this commit, it suggests (correctly) `&mut self`. We try to be careful about distinguishing between implicit and explicit self annotations, since the latter seem to be handled correctly already. Fixes #93093
- Loading branch information
1 parent
ecf7299
commit b885700
Showing
3 changed files
with
52 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// edition:2018 | ||
struct S { | ||
foo: usize, | ||
} | ||
impl S { | ||
async fn bar(&self) { //~ HELP consider changing this to be a mutable reference | ||
//~| SUGGESTION &mut self | ||
self.foo += 1; //~ ERROR cannot assign to `self.foo`, which is behind a `&` reference [E0594] | ||
} | ||
} | ||
|
||
fn main() { | ||
S { foo: 1 }.bar(); | ||
} |
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,12 @@ | ||
error[E0594]: cannot assign to `self.foo`, which is behind a `&` reference | ||
--> $DIR/issue-93093.rs:8:9 | ||
| | ||
LL | async fn bar(&self) { | ||
| ----- help: consider changing this to be a mutable reference: `&mut self` | ||
LL | | ||
LL | self.foo += 1; | ||
| ^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be written | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0594`. |