Rollup merge of #106410 - clubby789:borrow-mut-self-mut-self-diag, r=compiler-errors
Suggest `mut self: &mut Self` for `?Sized` impls Closes #106325 Closes #93078 The suggestion is _probably_ not what the user wants (hence `MaybeIncorrect`) but at least makes the problem in the above issues clearer. It might be better to add a note explaining why this is the case, but I'm not sure how best to word that so this is a start. ``@rustbot`` label +A-diagnostics
This commit is contained in:
commit
fe075319e6
3 changed files with 43 additions and 11 deletions
|
@ -344,20 +344,25 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||
} else {
|
||||
err.span_help(source_info.span, "try removing `&mut` here");
|
||||
}
|
||||
} else if decl.mutability == Mutability::Not
|
||||
&& !matches!(
|
||||
} else if decl.mutability == Mutability::Not {
|
||||
if matches!(
|
||||
decl.local_info,
|
||||
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
|
||||
hir::ImplicitSelfKind::MutRef
|
||||
))))
|
||||
)
|
||||
{
|
||||
),)))
|
||||
) {
|
||||
err.note(
|
||||
"as `Self` may be unsized, this call attempts to take `&mut &mut self`",
|
||||
);
|
||||
err.note("however, `&mut self` expands to `self: &mut Self`, therefore `self` cannot be borrowed mutably");
|
||||
} else {
|
||||
err.span_suggestion_verbose(
|
||||
decl.source_info.span.shrink_to_lo(),
|
||||
"consider making the binding mutable",
|
||||
"mut ",
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
15
src/test/ui/borrowck/issue-93078.rs
Normal file
15
src/test/ui/borrowck/issue-93078.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
trait Modify {
|
||||
fn modify(&mut self) ;
|
||||
}
|
||||
|
||||
impl<T> Modify for T {
|
||||
fn modify(&mut self) {}
|
||||
}
|
||||
|
||||
trait Foo {
|
||||
fn mute(&mut self) {
|
||||
self.modify(); //~ ERROR cannot borrow `self` as mutable
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
12
src/test/ui/borrowck/issue-93078.stderr
Normal file
12
src/test/ui/borrowck/issue-93078.stderr
Normal file
|
@ -0,0 +1,12 @@
|
|||
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
|
||||
--> $DIR/issue-93078.rs:11:9
|
||||
|
|
||||
LL | self.modify();
|
||||
| ^^^^^^^^^^^^^ cannot borrow as mutable
|
||||
|
|
||||
= note: as `Self` may be unsized, this call attempts to take `&mut &mut self`
|
||||
= note: however, `&mut self` expands to `self: &mut Self`, therefore `self` cannot be borrowed mutably
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0596`.
|
Loading…
Add table
Add a link
Reference in a new issue