Skip to content
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
12 changes: 10 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
binding_mode: BindingMode(ByRef::No, Mutability::Not),
opt_ty_info: Some(sp),
pat_span,
..
})) => {
if suggest {
err.span_note(sp, "the binding is already a mutable borrow");
err.span_suggestion_verbose(
pat_span.shrink_to_lo(),
"consider making the binding mutable if you need to reborrow \
multiple times",
"mut ".to_string(),
Applicability::MaybeIncorrect,
);
}
}
_ => {
Expand All @@ -356,9 +364,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
// give a best effort structured suggestion.
err.span_suggestion_verbose(
source_info.span.with_hi(source_info.span.lo() + BytePos(5)),
"try removing `&mut` here",
"if there is only one mutable reborrow, remove the `&mut`",
"",
Applicability::MachineApplicable,
Applicability::MaybeIncorrect,
);
} else {
// This can occur with things like `(&mut self).foo()`.
Expand Down
24 changes: 13 additions & 11 deletions tests/ui/borrowck/mut-borrow-of-mut-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
#![crate_type = "rlib"]

pub fn f(b: &mut i32) {
//~^ ERROR cannot borrow
//~| NOTE not mutable
//~| NOTE the binding is already a mutable borrow
//~^ ERROR: cannot borrow
//~| NOTE: not mutable
//~| NOTE: the binding is already a mutable borrow
//~| HELP: consider making the binding mutable if you need to reborrow multiple times
h(&mut b);
//~^ NOTE cannot borrow as mutable
//~| HELP try removing `&mut` here
//~^ NOTE: cannot borrow as mutable
//~| HELP: if there is only one mutable reborrow, remove the `&mut`
g(&mut &mut b);
//~^ NOTE cannot borrow as mutable
//~| HELP try removing `&mut` here
//~^ NOTE: cannot borrow as mutable
//~| HELP: if there is only one mutable reborrow, remove the `&mut`
}

pub fn g(b: &mut i32) { //~ NOTE the binding is already a mutable borrow
pub fn g(b: &mut i32) { //~ NOTE: the binding is already a mutable borrow
//~^ HELP: consider making the binding mutable if you need to reborrow multiple times
h(&mut &mut b);
//~^ ERROR cannot borrow
//~| NOTE cannot borrow as mutable
//~| HELP try removing `&mut` here
//~^ ERROR: cannot borrow
//~| NOTE: cannot borrow as mutable
//~| HELP: if there is only one mutable reborrow, remove the `&mut`
}

pub fn h(_: &mut i32) {}
Expand Down
20 changes: 14 additions & 6 deletions tests/ui/borrowck/mut-borrow-of-mut-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,44 @@ note: the binding is already a mutable borrow
|
LL | pub fn f(b: &mut i32) {
| ^^^^^^^^
help: try removing `&mut` here
help: consider making the binding mutable if you need to reborrow multiple times
|
LL | pub fn f(mut b: &mut i32) {
| +++
help: if there is only one mutable reborrow, remove the `&mut`
|
LL - h(&mut b);
LL + h(b);
|
help: try removing `&mut` here
help: if there is only one mutable reborrow, remove the `&mut`
|
LL - g(&mut &mut b);
LL + g(&mut b);
|

error[E0596]: cannot borrow `b` as mutable, as it is not declared as mutable
--> $DIR/mut-borrow-of-mut-ref.rs:17:12
--> $DIR/mut-borrow-of-mut-ref.rs:19:12
|
LL | h(&mut &mut b);
| ^^^^^^ cannot borrow as mutable
|
note: the binding is already a mutable borrow
--> $DIR/mut-borrow-of-mut-ref.rs:16:13
--> $DIR/mut-borrow-of-mut-ref.rs:17:13
|
LL | pub fn g(b: &mut i32) {
| ^^^^^^^^
help: try removing `&mut` here
help: consider making the binding mutable if you need to reborrow multiple times
|
LL | pub fn g(mut b: &mut i32) {
| +++
help: if there is only one mutable reborrow, remove the `&mut`
|
LL - h(&mut &mut b);
LL + h(&mut b);
|

error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
--> $DIR/mut-borrow-of-mut-ref.rs:34:5
--> $DIR/mut-borrow-of-mut-ref.rs:36:5
|
LL | f.bar();
| ^ cannot borrow as mutable
Expand Down
11 changes: 6 additions & 5 deletions tests/ui/did_you_mean/issue-31424.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ struct Struct;

impl Struct {
fn foo(&mut self) {
(&mut self).bar(); //~ ERROR cannot borrow
//~^ HELP try removing `&mut` here
(&mut self).bar(); //~ ERROR: cannot borrow
//~^ HELP: try removing `&mut` here
}

// In this case we could keep the suggestion, but to distinguish the
// two cases is pretty hard. It's an obscure case anyway.
fn bar(self: &mut Self) {
//~^ WARN function cannot return without recursing
//~^^ HELP a `loop` may express intention better if this is on purpose
//~^ WARN: function cannot return without recursing
//~| HELP: a `loop` may express intention better if this is on purpose
//~| HELP: consider making the binding mutable if you need to reborrow multiple times
(&mut self).bar(); //~ ERROR cannot borrow
//~^ HELP try removing `&mut` here
//~^ HELP: try removing `&mut` here
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/ui/did_you_mean/issue-31424.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ LL | (&mut self).bar();
= note: `#[warn(unconditional_recursion)]` on by default

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-31424.rs:16:9
--> $DIR/issue-31424.rs:17:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable
Expand All @@ -39,10 +39,14 @@ note: the binding is already a mutable borrow
LL | fn bar(self: &mut Self) {
| ^^^^^^^^^
help: try removing `&mut` here
--> $DIR/issue-31424.rs:16:9
--> $DIR/issue-31424.rs:17:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^
help: consider making the binding mutable if you need to reborrow multiple times
|
LL | fn bar(mut self: &mut Self) {
| +++

error: aborting due to 2 previous errors; 1 warning emitted

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/did_you_mean/issue-34126.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ struct Z { }
impl Z {
fn run(&self, z: &mut Z) { }
fn start(&mut self) {
self.run(&mut self); //~ ERROR cannot borrow
//~| ERROR cannot borrow
//~| HELP try removing `&mut` here
self.run(&mut self); //~ ERROR: cannot borrow
//~| ERROR: cannot borrow
//~| HELP: if there is only one mutable reborrow, remove the `&mut`
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/did_you_mean/issue-34126.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: the binding is already a mutable borrow
|
LL | fn start(&mut self) {
| ^^^^^^^^^
help: try removing `&mut` here
help: if there is only one mutable reborrow, remove the `&mut`
|
LL - self.run(&mut self);
LL + self.run(self);
Expand Down
19 changes: 10 additions & 9 deletions tests/ui/nll/issue-51191.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ struct Struct;

impl Struct {
fn bar(self: &mut Self) {
//~^ WARN function cannot return without recursing
//~^^ HELP a `loop` may express intention better if this is on purpose
//~^ WARN: function cannot return without recursing
//~| HELP: a `loop` may express intention better if this is on purpose
//~| HELP: consider making the binding mutable if you need to reborrow multiple times
(&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~^^ HELP try removing `&mut` here
//~^ ERROR: cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~| HELP: try removing `&mut` here
}

fn imm(self) { //~ HELP consider changing this to be mutable
(&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~^ ERROR: cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
}

fn mtbl(mut self) {
Expand All @@ -20,14 +21,14 @@ impl Struct {

fn immref(&self) {
(&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~^^ ERROR cannot borrow data in a `&` reference as mutable [E0596]
//~^ ERROR: cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~| ERROR: cannot borrow data in a `&` reference as mutable [E0596]
}

fn mtblref(&mut self) {
(&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~^^ HELP try removing `&mut` here
//~^ ERROR: cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~| HELP: try removing `&mut` here
}
}

Expand Down
20 changes: 12 additions & 8 deletions tests/ui/nll/issue-51191.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | (&mut self).bar();
= note: `#[warn(unconditional_recursion)]` on by default

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:7:9
--> $DIR/issue-51191.rs:8:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable
Expand All @@ -22,13 +22,17 @@ note: the binding is already a mutable borrow
LL | fn bar(self: &mut Self) {
| ^^^^^^^^^
help: try removing `&mut` here
--> $DIR/issue-51191.rs:7:9
--> $DIR/issue-51191.rs:8:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^
help: consider making the binding mutable if you need to reborrow multiple times
|
LL | fn bar(mut self: &mut Self) {
| +++

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:13:9
--> $DIR/issue-51191.rs:14:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable
Expand All @@ -39,30 +43,30 @@ LL | fn imm(mut self) {
| +++

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:22:9
--> $DIR/issue-51191.rs:23:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/issue-51191.rs:22:9
--> $DIR/issue-51191.rs:23:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:28:9
--> $DIR/issue-51191.rs:29:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable
|
note: the binding is already a mutable borrow
--> $DIR/issue-51191.rs:27:16
--> $DIR/issue-51191.rs:28:16
|
LL | fn mtblref(&mut self) {
| ^^^^^^^^^
help: try removing `&mut` here
--> $DIR/issue-51191.rs:28:9
--> $DIR/issue-51191.rs:29:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^
Expand Down
Loading