Skip to content

[borrowck] Fix help on mutating &self in async fns #93221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,32 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
// don't create labels for compiler-generated spans
Some(_) => None,
None => {
let (span, suggestion) = suggest_ampmut(
self.infcx.tcx,
local_decl,
opt_assignment_rhs_span,
*opt_ty_info,
);
let (span, suggestion) = if name != kw::SelfLower {
suggest_ampmut(
self.infcx.tcx,
local_decl,
opt_assignment_rhs_span,
*opt_ty_info,
)
} else {
match local_decl.local_info.as_deref() {
Some(LocalInfo::User(ClearCrossCrate::Set(
mir::BindingForm::Var(mir::VarBindingForm {
opt_ty_info: None,
..
}),
))) => {
suggest_ampmut_self(self.infcx.tcx, local_decl)
}
// explicit self (eg `self: &'a Self`)
_ => suggest_ampmut(
self.infcx.tcx,
local_decl,
opt_assignment_rhs_span,
*opt_ty_info,
),
}
};
Some((true, span, suggestion))
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/borrowck/issue-93093.rs
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();
}
12 changes: 12 additions & 0 deletions src/test/ui/borrowck/issue-93093.stderr
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`.