Skip to content

Commit ff92ab0

Browse files
committed
More accurate mutability suggestion
1 parent 2699d81 commit ff92ab0

19 files changed

+244
-140
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -408,21 +408,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
408408
fn_decl.implicit_self,
409409
hir::ImplicitSelfKind::RefImm | hir::ImplicitSelfKind::RefMut
410410
) {
411-
err.span_suggestion(
412-
upvar_ident.span,
411+
err.span_suggestion_verbose(
412+
upvar_ident.span.shrink_to_lo(),
413413
"consider changing this to be mutable",
414-
format!("mut {}", upvar_ident.name),
414+
"mut ",
415415
Applicability::MachineApplicable,
416416
);
417417
break;
418418
}
419419
}
420420
}
421421
} else {
422-
err.span_suggestion(
423-
upvar_ident.span,
422+
err.span_suggestion_verbose(
423+
upvar_ident.span.shrink_to_lo(),
424424
"consider changing this to be mutable",
425-
format!("mut {}", upvar_ident.name),
425+
"mut ",
426426
Applicability::MachineApplicable,
427427
);
428428
}
@@ -755,13 +755,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
755755
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
756756
..
757757
}) = node
758-
&& let Ok(name) =
759-
self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span)
760758
{
761-
err.span_suggestion(
762-
pat_span,
759+
err.multipart_suggestion(
763760
"consider changing this to be mutable",
764-
format!("&(mut {name})"),
761+
vec![
762+
(pat_span.until(local_decl.source_info.span), "&(mut ".to_string()),
763+
(
764+
local_decl.source_info.span.shrink_to_hi().with_hi(pat_span.hi()),
765+
")".to_string(),
766+
),
767+
],
765768
Applicability::MachineApplicable,
766769
);
767770
return;

tests/ui/borrowck/borrow-raw-address-of-mutability.stderr

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ LL | let mut x = 0;
1212
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
1313
--> $DIR/borrow-raw-address-of-mutability.rs:11:17
1414
|
15-
LL | let x = 0;
16-
| - help: consider changing this to be mutable: `mut x`
17-
LL | let mut f = || {
1815
LL | let y = &raw mut x;
1916
| ^^^^^^^^^^ cannot borrow as mutable
17+
|
18+
help: consider changing this to be mutable
19+
|
20+
LL | let mut x = 0;
21+
| +++
2022

2123
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
2224
--> $DIR/borrow-raw-address-of-mutability.rs:21:5

tests/ui/borrowck/borrowck-closures-unique.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ LL | c1;
4343
error[E0594]: cannot assign to `x`, as it is not declared as mutable
4444
--> $DIR/borrowck-closures-unique.rs:43:38
4545
|
46-
LL | fn e(x: &'static mut isize) {
47-
| - help: consider changing this to be mutable: `mut x`
4846
LL | let c1 = |y: &'static mut isize| x = y;
4947
| ^^^^^ cannot assign
48+
|
49+
help: consider changing this to be mutable
50+
|
51+
LL | fn e(mut x: &'static mut isize) {
52+
| +++
5053

5154
error: aborting due to 4 previous errors
5255

tests/ui/borrowck/issue-111554.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ LL | || bar(&mut self);
1919
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
2020
--> $DIR/issue-111554.rs:21:16
2121
|
22-
LL | pub fn quux(self) {
23-
| ---- help: consider changing this to be mutable: `mut self`
2422
LL | || bar(&mut self);
2523
| ^^^^^^^^^ cannot borrow as mutable
24+
|
25+
help: consider changing this to be mutable
26+
|
27+
LL | pub fn quux(mut self) {
28+
| +++
2629

2730
error: aborting due to 4 previous errors
2831

tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr

+30-18
Original file line numberDiff line numberDiff line change
@@ -44,56 +44,68 @@ LL | borrowck_closures_unique::e(addr_of_mut!(X));
4444
error[E0594]: cannot assign to `x`, as it is not declared as mutable
4545
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:9:46
4646
|
47-
LL | pub fn e(x: &'static mut isize) {
48-
| - help: consider changing this to be mutable: `mut x`
49-
LL | static mut Y: isize = 3;
5047
LL | let mut c1 = |y: &'static mut isize| x = y;
5148
| ^^^^^ cannot assign
49+
|
50+
help: consider changing this to be mutable
51+
|
52+
LL | pub fn e(mut x: &'static mut isize) {
53+
| +++
5254

5355
error[E0594]: cannot assign to `x`, as it is not declared as mutable
5456
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:22:50
5557
|
56-
LL | pub fn ee(x: &'static mut isize) {
57-
| - help: consider changing this to be mutable: `mut x`
58-
...
5958
LL | let mut c2 = |y: &'static mut isize| x = y;
6059
| ^^^^^ cannot assign
60+
|
61+
help: consider changing this to be mutable
62+
|
63+
LL | pub fn ee(mut x: &'static mut isize) {
64+
| +++
6165

6266
error[E0594]: cannot assign to `x`, as it is not declared as mutable
6367
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:37:13
6468
|
65-
LL | pub fn capture_assign_whole(x: (i32,)) {
66-
| - help: consider changing this to be mutable: `mut x`
67-
LL | || {
6869
LL | x = (1,);
6970
| ^^^^^^^^ cannot assign
71+
|
72+
help: consider changing this to be mutable
73+
|
74+
LL | pub fn capture_assign_whole(mut x: (i32,)) {
75+
| +++
7076

7177
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
7278
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:43:13
7379
|
74-
LL | pub fn capture_assign_part(x: (i32,)) {
75-
| - help: consider changing this to be mutable: `mut x`
76-
LL | || {
7780
LL | x.0 = 1;
7881
| ^^^^^^^ cannot assign
82+
|
83+
help: consider changing this to be mutable
84+
|
85+
LL | pub fn capture_assign_part(mut x: (i32,)) {
86+
| +++
7987

8088
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
8189
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:49:13
8290
|
83-
LL | pub fn capture_reborrow_whole(x: (i32,)) {
84-
| - help: consider changing this to be mutable: `mut x`
85-
LL | || {
8691
LL | &mut x;
8792
| ^^^^^^ cannot borrow as mutable
93+
|
94+
help: consider changing this to be mutable
95+
|
96+
LL | pub fn capture_reborrow_whole(mut x: (i32,)) {
97+
| +++
8898

8999
error[E0596]: cannot borrow `x.0` as mutable, as `x` is not declared as mutable
90100
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:55:13
91101
|
92-
LL | pub fn capture_reborrow_part(x: (i32,)) {
93-
| - help: consider changing this to be mutable: `mut x`
94-
LL | || {
95102
LL | &mut x.0;
96103
| ^^^^^^^^ cannot borrow as mutable
104+
|
105+
help: consider changing this to be mutable
106+
|
107+
LL | pub fn capture_reborrow_part(mut x: (i32,)) {
108+
| +++
97109

98110
error: aborting due to 6 previous errors; 3 warnings emitted
99111

tests/ui/borrowck/mutability-errors.stderr

+40-24
Original file line numberDiff line numberDiff line change
@@ -262,74 +262,90 @@ LL | fn imm_local(mut x: (i32,)) {
262262
error[E0594]: cannot assign to `x`, as it is not declared as mutable
263263
--> $DIR/mutability-errors.rs:60:9
264264
|
265-
LL | fn imm_capture(x: (i32,)) {
266-
| - help: consider changing this to be mutable: `mut x`
267-
LL | || {
268265
LL | x = (1,);
269266
| ^^^^^^^^ cannot assign
267+
|
268+
help: consider changing this to be mutable
269+
|
270+
LL | fn imm_capture(mut x: (i32,)) {
271+
| +++
270272

271273
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
272274
--> $DIR/mutability-errors.rs:61:9
273275
|
274-
LL | fn imm_capture(x: (i32,)) {
275-
| - help: consider changing this to be mutable: `mut x`
276-
...
277276
LL | x.0 = 1;
278277
| ^^^^^^^ cannot assign
278+
|
279+
help: consider changing this to be mutable
280+
|
281+
LL | fn imm_capture(mut x: (i32,)) {
282+
| +++
279283

280284
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
281285
--> $DIR/mutability-errors.rs:62:9
282286
|
283-
LL | fn imm_capture(x: (i32,)) {
284-
| - help: consider changing this to be mutable: `mut x`
285-
...
286287
LL | &mut x;
287288
| ^^^^^^ cannot borrow as mutable
289+
|
290+
help: consider changing this to be mutable
291+
|
292+
LL | fn imm_capture(mut x: (i32,)) {
293+
| +++
288294

289295
error[E0596]: cannot borrow `x.0` as mutable, as `x` is not declared as mutable
290296
--> $DIR/mutability-errors.rs:63:9
291297
|
292-
LL | fn imm_capture(x: (i32,)) {
293-
| - help: consider changing this to be mutable: `mut x`
294-
...
295298
LL | &mut x.0;
296299
| ^^^^^^^^ cannot borrow as mutable
300+
|
301+
help: consider changing this to be mutable
302+
|
303+
LL | fn imm_capture(mut x: (i32,)) {
304+
| +++
297305

298306
error[E0594]: cannot assign to `x`, as it is not declared as mutable
299307
--> $DIR/mutability-errors.rs:66:9
300308
|
301-
LL | fn imm_capture(x: (i32,)) {
302-
| - help: consider changing this to be mutable: `mut x`
303-
...
304309
LL | x = (1,);
305310
| ^^^^^^^^ cannot assign
311+
|
312+
help: consider changing this to be mutable
313+
|
314+
LL | fn imm_capture(mut x: (i32,)) {
315+
| +++
306316

307317
error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable
308318
--> $DIR/mutability-errors.rs:67:9
309319
|
310-
LL | fn imm_capture(x: (i32,)) {
311-
| - help: consider changing this to be mutable: `mut x`
312-
...
313320
LL | x.0 = 1;
314321
| ^^^^^^^ cannot assign
322+
|
323+
help: consider changing this to be mutable
324+
|
325+
LL | fn imm_capture(mut x: (i32,)) {
326+
| +++
315327

316328
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
317329
--> $DIR/mutability-errors.rs:68:9
318330
|
319-
LL | fn imm_capture(x: (i32,)) {
320-
| - help: consider changing this to be mutable: `mut x`
321-
...
322331
LL | &mut x;
323332
| ^^^^^^ cannot borrow as mutable
333+
|
334+
help: consider changing this to be mutable
335+
|
336+
LL | fn imm_capture(mut x: (i32,)) {
337+
| +++
324338

325339
error[E0596]: cannot borrow `x.0` as mutable, as `x` is not declared as mutable
326340
--> $DIR/mutability-errors.rs:69:9
327341
|
328-
LL | fn imm_capture(x: (i32,)) {
329-
| - help: consider changing this to be mutable: `mut x`
330-
...
331342
LL | &mut x.0;
332343
| ^^^^^^^^ cannot borrow as mutable
344+
|
345+
help: consider changing this to be mutable
346+
|
347+
LL | fn imm_capture(mut x: (i32,)) {
348+
| +++
333349

334350
error[E0594]: cannot assign to immutable static item `X`
335351
--> $DIR/mutability-errors.rs:76:5

tests/ui/cannot-mutate-captured-non-mut-var.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
error[E0594]: cannot assign to `x`, as it is not declared as mutable
22
--> $DIR/cannot-mutate-captured-non-mut-var.rs:9:25
33
|
4-
LL | let x = 1;
5-
| - help: consider changing this to be mutable: `mut x`
64
LL | to_fn_once(move|| { x = 2; });
75
| ^^^^^ cannot assign
6+
|
7+
help: consider changing this to be mutable
8+
|
9+
LL | let mut x = 1;
10+
| +++
811

912
error[E0596]: cannot borrow `s` as mutable, as it is not declared as mutable
1013
--> $DIR/cannot-mutate-captured-non-mut-var.rs:13:25
1114
|
12-
LL | let s = std::io::stdin();
13-
| - help: consider changing this to be mutable: `mut s`
1415
LL | to_fn_once(move|| { s.read_to_end(&mut Vec::new()); });
1516
| ^ cannot borrow as mutable
17+
|
18+
help: consider changing this to be mutable
19+
|
20+
LL | let mut s = std::io::stdin();
21+
| +++
1622

1723
error: aborting due to 2 previous errors
1824

tests/ui/closures/2229_closure_analysis/array_subslice.stderr

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
error[E0596]: cannot borrow `x[..]` as mutable, as `x` is not declared as mutable
22
--> $DIR/array_subslice.rs:7:21
33
|
4-
LL | pub fn subslice_array(x: [u8; 3]) {
5-
| - help: consider changing this to be mutable: `mut x`
6-
...
74
LL | let [ref y, ref mut z @ ..] = x;
85
| ^^^^^^^^^ cannot borrow as mutable
6+
|
7+
help: consider changing this to be mutable
8+
|
9+
LL | pub fn subslice_array(mut x: [u8; 3]) {
10+
| +++
911

1012
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
1113
--> $DIR/array_subslice.rs:10:5

tests/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm.stderr

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
error[E0594]: cannot assign to `z.0.0.0`, as it is not declared as mutable
22
--> $DIR/cant-mutate-imm.rs:12:9
33
|
4-
LL | let z = (y, 10);
5-
| - help: consider changing this to be mutable: `mut z`
6-
...
74
LL | z.0.0.0 = 20;
85
| ^^^^^^^^^^^^ cannot assign
6+
|
7+
help: consider changing this to be mutable
8+
|
9+
LL | let mut z = (y, 10);
10+
| +++
911

1012
error[E0594]: cannot assign to `*bx.0`, as it is not declared as mutable
1113
--> $DIR/cant-mutate-imm.rs:24:9
1214
|
13-
LL | let bx = Box::new(x);
14-
| -- help: consider changing this to be mutable: `mut bx`
15-
...
1615
LL | bx.0 = 20;
1716
| ^^^^^^^^^ cannot assign
17+
|
18+
help: consider changing this to be mutable
19+
|
20+
LL | let mut bx = Box::new(x);
21+
| +++
1822

1923
error: aborting due to 2 previous errors
2024

tests/ui/closures/closure-immutable-outer-variable.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error[E0594]: cannot assign to `y`, as it is not declared as mutable
22
--> $DIR/closure-immutable-outer-variable.rs:11:26
33
|
4-
LL | let y = true;
5-
| - help: consider changing this to be mutable: `mut y`
64
LL | foo(Box::new(move || y = !y) as Box<_>);
75
| ^^^^^^ cannot assign
6+
|
7+
help: consider changing this to be mutable
8+
|
9+
LL | let mut y = true;
10+
| +++
811

912
error: aborting due to 1 previous error
1013

0 commit comments

Comments
 (0)