Skip to content

Commit 89ecae5

Browse files
committed
Better span for "make binding mutable" suggestion
1 parent 140392b commit 89ecae5

25 files changed

+134
-101
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,13 +3757,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37573757
assigned_span: Span,
37583758
err_place: Place<'tcx>,
37593759
) {
3760-
let (from_arg, local_decl, local_name) = match err_place.as_local() {
3761-
Some(local) => (
3762-
self.body.local_kind(local) == LocalKind::Arg,
3763-
Some(&self.body.local_decls[local]),
3764-
self.local_names[local],
3765-
),
3766-
None => (false, None, None),
3760+
let (from_arg, local_decl) = match err_place.as_local() {
3761+
Some(local) => {
3762+
(self.body.local_kind(local) == LocalKind::Arg, Some(&self.body.local_decls[local]))
3763+
}
3764+
None => (false, None),
37673765
};
37683766

37693767
// If root local is initialized immediately (everything apart from let
@@ -3795,13 +3793,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37953793
err.span_label(assigned_span, format!("first assignment to {place_description}"));
37963794
}
37973795
if let Some(decl) = local_decl
3798-
&& let Some(name) = local_name
37993796
&& decl.can_be_made_mutable()
38003797
{
3801-
err.span_suggestion(
3802-
decl.source_info.span,
3798+
err.span_suggestion_verbose(
3799+
decl.source_info.span.shrink_to_lo(),
38033800
"consider making this binding mutable",
3804-
format!("mut {name}"),
3801+
"mut ".to_string(),
38053802
Applicability::MachineApplicable,
38063803
);
38073804
if !from_arg
@@ -3813,10 +3810,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
38133810
}))
38143811
)
38153812
{
3816-
err.span_suggestion(
3817-
decl.source_info.span,
3813+
err.span_suggestion_verbose(
3814+
decl.source_info.span.shrink_to_lo(),
38183815
"to modify the original value, take a borrow instead",
3819-
format!("ref mut {name}"),
3816+
"ref mut ".to_string(),
38203817
Applicability::MaybeIncorrect,
38213818
);
38223819
}

tests/ui/assign-imm-local-twice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn test() {
22
let v: isize;
33
//~^ HELP consider making this binding mutable
4-
//~| SUGGESTION mut v
4+
//~| SUGGESTION mut
55
v = 1; //~ NOTE first assignment
66
println!("v={}", v);
77
v = 2; //~ ERROR cannot assign twice to immutable variable

tests/ui/assign-imm-local-twice.stderr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
error[E0384]: cannot assign twice to immutable variable `v`
22
--> $DIR/assign-imm-local-twice.rs:7:5
33
|
4-
LL | let v: isize;
5-
| - help: consider making this binding mutable: `mut v`
6-
...
74
LL | v = 1;
85
| ----- first assignment to `v`
96
LL | println!("v={}", v);
107
LL | v = 2;
118
| ^^^^^ cannot assign twice to immutable variable
9+
|
10+
help: consider making this binding mutable
11+
|
12+
LL | let mut v: isize;
13+
| +++
1214

1315
error: aborting due to 1 previous error
1416

tests/ui/async-await/issue-61452.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ error[E0384]: cannot assign twice to immutable variable `x`
1313
--> $DIR/issue-61452.rs:9:5
1414
|
1515
LL | pub async fn g(x: usize) {
16-
| -
17-
| |
18-
| first assignment to `x`
19-
| help: consider making this binding mutable: `mut x`
16+
| - first assignment to `x`
2017
LL | x += 1;
2118
| ^^^^^^ cannot assign twice to immutable variable
19+
|
20+
help: consider making this binding mutable
21+
|
22+
LL | pub async fn g(mut x: usize) {
23+
| +++
2224

2325
error: aborting due to 2 previous errors
2426

tests/ui/borrowck/borrowck-match-binding-is-assignment.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ LL | x += 1;
99
help: consider making this binding mutable
1010
|
1111
LL | mut x => {
12-
| ~~~~~
12+
| +++
1313
help: to modify the original value, take a borrow instead
1414
|
1515
LL | ref mut x => {
16-
| ~~~~~~~~~
16+
| +++++++
1717

1818
error[E0384]: cannot assign twice to immutable variable `x`
1919
--> $DIR/borrowck-match-binding-is-assignment.rs:20:13
@@ -26,11 +26,11 @@ LL | x += 1;
2626
help: consider making this binding mutable
2727
|
2828
LL | E::Foo(mut x) => {
29-
| ~~~~~
29+
| +++
3030
help: to modify the original value, take a borrow instead
3131
|
3232
LL | E::Foo(ref mut x) => {
33-
| ~~~~~~~~~
33+
| +++++++
3434

3535
error[E0384]: cannot assign twice to immutable variable `x`
3636
--> $DIR/borrowck-match-binding-is-assignment.rs:26:13
@@ -43,11 +43,11 @@ LL | x += 1;
4343
help: consider making this binding mutable
4444
|
4545
LL | S { bar: mut x } => {
46-
| ~~~~~
46+
| +++
4747
help: to modify the original value, take a borrow instead
4848
|
4949
LL | S { bar: ref mut x } => {
50-
| ~~~~~~~~~
50+
| +++++++
5151

5252
error[E0384]: cannot assign twice to immutable variable `x`
5353
--> $DIR/borrowck-match-binding-is-assignment.rs:32:13
@@ -60,11 +60,11 @@ LL | x += 1;
6060
help: consider making this binding mutable
6161
|
6262
LL | (mut x,) => {
63-
| ~~~~~
63+
| +++
6464
help: to modify the original value, take a borrow instead
6565
|
6666
LL | (ref mut x,) => {
67-
| ~~~~~~~~~
67+
| +++++++
6868

6969
error[E0384]: cannot assign twice to immutable variable `x`
7070
--> $DIR/borrowck-match-binding-is-assignment.rs:38:13
@@ -77,11 +77,11 @@ LL | x += 1;
7777
help: consider making this binding mutable
7878
|
7979
LL | [mut x,_,_] => {
80-
| ~~~~~
80+
| +++
8181
help: to modify the original value, take a borrow instead
8282
|
8383
LL | [ref mut x,_,_] => {
84-
| ~~~~~~~~~
84+
| +++++++
8585

8686
error: aborting due to 5 previous errors
8787

tests/ui/borrowck/immutable-arg.stderr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error[E0384]: cannot assign to immutable argument `_x`
22
--> $DIR/immutable-arg.rs:2:5
33
|
4-
LL | fn foo(_x: u32) {
5-
| -- help: consider making this binding mutable: `mut _x`
64
LL | _x = 4;
75
| ^^^^^^ cannot assign to immutable argument
6+
|
7+
help: consider making this binding mutable
8+
|
9+
LL | fn foo(mut _x: u32) {
10+
| +++
811

912
error: aborting due to 1 previous error
1013

tests/ui/borrowck/issue-45199.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn test_drop_replace() {
22
let b: Box<isize>;
33
//~^ HELP consider making this binding mutable
4-
//~| SUGGESTION mut b
4+
//~| SUGGESTION mut
55
b = Box::new(1); //~ NOTE first assignment
66
b = Box::new(2); //~ ERROR cannot assign twice to immutable variable `b`
77
//~| NOTE cannot assign twice to immutable
@@ -10,13 +10,13 @@ fn test_drop_replace() {
1010
fn test_call() {
1111
let b = Box::new(1); //~ NOTE first assignment
1212
//~| HELP consider making this binding mutable
13-
//~| SUGGESTION mut b
13+
//~| SUGGESTION mut
1414
b = Box::new(2); //~ ERROR cannot assign twice to immutable variable `b`
1515
//~| NOTE cannot assign twice to immutable
1616
}
1717

1818
fn test_args(b: Box<i32>) { //~ HELP consider making this binding mutable
19-
//~| SUGGESTION mut b
19+
//~| SUGGESTION mut
2020
b = Box::new(2); //~ ERROR cannot assign to immutable argument `b`
2121
//~| NOTE cannot assign to immutable argument
2222
}

tests/ui/borrowck/issue-45199.stderr

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
error[E0384]: cannot assign twice to immutable variable `b`
22
--> $DIR/issue-45199.rs:6:5
33
|
4-
LL | let b: Box<isize>;
5-
| - help: consider making this binding mutable: `mut b`
6-
...
74
LL | b = Box::new(1);
85
| - first assignment to `b`
96
LL | b = Box::new(2);
107
| ^ cannot assign twice to immutable variable
8+
|
9+
help: consider making this binding mutable
10+
|
11+
LL | let mut b: Box<isize>;
12+
| +++
1113

1214
error[E0384]: cannot assign twice to immutable variable `b`
1315
--> $DIR/issue-45199.rs:14:5
1416
|
1517
LL | let b = Box::new(1);
16-
| -
17-
| |
18-
| first assignment to `b`
19-
| help: consider making this binding mutable: `mut b`
18+
| - first assignment to `b`
2019
...
2120
LL | b = Box::new(2);
2221
| ^ cannot assign twice to immutable variable
22+
|
23+
help: consider making this binding mutable
24+
|
25+
LL | let mut b = Box::new(1);
26+
| +++
2327

2428
error[E0384]: cannot assign to immutable argument `b`
2529
--> $DIR/issue-45199.rs:20:5
2630
|
27-
LL | fn test_args(b: Box<i32>) {
28-
| - help: consider making this binding mutable: `mut b`
29-
LL |
3031
LL | b = Box::new(2);
3132
| ^ cannot assign to immutable argument
33+
|
34+
help: consider making this binding mutable
35+
|
36+
LL | fn test_args(mut b: Box<i32>) {
37+
| +++
3238

3339
error: aborting due to 3 previous errors
3440

tests/ui/borrowck/suggest-ref-mut-issue-118596.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ LL | x = 2;
99
help: consider making this binding mutable
1010
|
1111
LL | if let Some(mut x) = y {
12-
| ~~~~~
12+
| +++
1313
help: to modify the original value, take a borrow instead
1414
|
1515
LL | if let Some(ref mut x) = y {
16-
| ~~~~~~~~~
16+
| +++++++
1717

1818
error[E0384]: cannot assign twice to immutable variable `x`
1919
--> $DIR/suggest-ref-mut-issue-118596.rs:9:5
@@ -26,11 +26,11 @@ LL | x = 0;
2626
help: consider making this binding mutable
2727
|
2828
LL | let [mut x, ref xs_hold @ ..] = arr;
29-
| ~~~~~
29+
| +++
3030
help: to modify the original value, take a borrow instead
3131
|
3232
LL | let [ref mut x, ref xs_hold @ ..] = arr;
33-
| ~~~~~~~~~
33+
| +++++++
3434

3535
error: aborting due to 2 previous errors
3636

tests/ui/borrowck/tainted-promoteds.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ error[E0384]: cannot assign twice to immutable variable `a`
22
--> $DIR/tainted-promoteds.rs:7:5
33
|
44
LL | let a = 0;
5-
| -
6-
| |
7-
| first assignment to `a`
8-
| help: consider making this binding mutable: `mut a`
5+
| - first assignment to `a`
96
LL | a = &0 * &1 * &2 * &3;
107
| ^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
8+
|
9+
help: consider making this binding mutable
10+
|
11+
LL | let mut a = 0;
12+
| +++
1113

1214
error: aborting due to 1 previous error
1315

0 commit comments

Comments
 (0)