Don't suggest changing {ImmRef,MutRef} implicit self to be mutable
This commit is contained in:
parent
b9c5fdc888
commit
57e67e4ab2
3 changed files with 79 additions and 6 deletions
|
@ -416,12 +416,28 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
||||||
_,
|
_,
|
||||||
) = pat.kind
|
) = pat.kind
|
||||||
{
|
{
|
||||||
err.span_suggestion(
|
if upvar_ident.name == kw::SelfLower {
|
||||||
upvar_ident.span,
|
for (_, node) in self.infcx.tcx.hir().parent_iter(upvar_hir_id) {
|
||||||
"consider changing this to be mutable",
|
if let Some(fn_decl) = node.fn_decl() {
|
||||||
format!("mut {}", upvar_ident.name),
|
if !matches!(fn_decl.implicit_self, hir::ImplicitSelfKind::ImmRef | hir::ImplicitSelfKind::MutRef) {
|
||||||
Applicability::MachineApplicable,
|
err.span_suggestion(
|
||||||
);
|
upvar_ident.span,
|
||||||
|
"consider changing this to be mutable",
|
||||||
|
format!("mut {}", upvar_ident.name),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err.span_suggestion(
|
||||||
|
upvar_ident.span,
|
||||||
|
"consider changing this to be mutable",
|
||||||
|
format!("mut {}", upvar_ident.name),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let tcx = self.infcx.tcx;
|
let tcx = self.infcx.tcx;
|
||||||
|
|
28
tests/ui/borrowck/issue-111554.rs
Normal file
28
tests/ui/borrowck/issue-111554.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
struct Foo {}
|
||||||
|
|
||||||
|
impl Foo {
|
||||||
|
pub fn foo(&mut self) {
|
||||||
|
|| bar(&mut self);
|
||||||
|
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn baz(&self) {
|
||||||
|
|| bar(&mut self);
|
||||||
|
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable
|
||||||
|
//~| ERROR cannot borrow data in a `&` reference as mutable
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn qux(mut self) {
|
||||||
|
|| bar(&mut self);
|
||||||
|
// OK
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quux(self) {
|
||||||
|
|| bar(&mut self);
|
||||||
|
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(_: &mut Foo) {}
|
||||||
|
|
||||||
|
fn main() {}
|
29
tests/ui/borrowck/issue-111554.stderr
Normal file
29
tests/ui/borrowck/issue-111554.stderr
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
|
||||||
|
--> $DIR/issue-111554.rs:5:16
|
||||||
|
|
|
||||||
|
LL | || bar(&mut self);
|
||||||
|
| ^^^^^^^^^ cannot borrow as mutable
|
||||||
|
|
||||||
|
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
|
||||||
|
--> $DIR/issue-111554.rs:10:16
|
||||||
|
|
|
||||||
|
LL | || bar(&mut self);
|
||||||
|
| ^^^^^^^^^ cannot borrow as mutable
|
||||||
|
|
||||||
|
error[E0596]: cannot borrow data in a `&` reference as mutable
|
||||||
|
--> $DIR/issue-111554.rs:10:16
|
||||||
|
|
|
||||||
|
LL | || bar(&mut self);
|
||||||
|
| ^^^^^^^^^ cannot borrow as mutable
|
||||||
|
|
||||||
|
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
|
||||||
|
--> $DIR/issue-111554.rs:21:16
|
||||||
|
|
|
||||||
|
LL | pub fn quux(self) {
|
||||||
|
| ---- help: consider changing this to be mutable: `mut self`
|
||||||
|
LL | || bar(&mut self);
|
||||||
|
| ^^^^^^^^^ cannot borrow as mutable
|
||||||
|
|
||||||
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0596`.
|
Loading…
Add table
Add a link
Reference in a new issue