Skip to content

Commit f9d00b4

Browse files
Rollup merge of #87458 - ibraheemdev:help-msg-block-borrow, r=oli-obk
Fix help message for modification to &T created by &{t} Previous: ```rust error[E0594]: cannot assign to `*x` which is behind a `&` reference --> src/main.rs:3:5 | 2 | let x: &usize = &mut{0}; | ------- help: consider changing this to be a mutable reference: `&mut mut{0}` 3 | *x = 1; | ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written ```
2 parents e3c6cd2 + b4a873f commit f9d00b4

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -905,16 +905,16 @@ fn suggest_ampmut<'tcx>(
905905
Some(c) if c.is_whitespace() => true,
906906
// e.g. `&mut(x)`
907907
Some('(') => true,
908+
// e.g. `&mut{x}`
909+
Some('{') => true,
908910
// e.g. `&mutablevar`
909911
_ => false,
910912
}
911913
} else {
912914
false
913915
}
914916
};
915-
if let (true, Some(ws_pos)) =
916-
(src.starts_with("&'"), src.find(|c: char| -> bool { c.is_whitespace() }))
917-
{
917+
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
918918
let lt_name = &src[1..ws_pos];
919919
let ty = src[ws_pos..].trim_start();
920920
if !is_mutbl(ty) {
@@ -940,9 +940,7 @@ fn suggest_ampmut<'tcx>(
940940
};
941941

942942
if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span) {
943-
if let (true, Some(ws_pos)) =
944-
(src.starts_with("&'"), src.find(|c: char| -> bool { c.is_whitespace() }))
945-
{
943+
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
946944
let lt_name = &src[1..ws_pos];
947945
let ty = &src[ws_pos..];
948946
return (highlight_span, format!("&{} mut{}", lt_name, ty));

src/test/ui/borrowck/issue-85765.rs

+14
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@ fn main() {
1212
*r = 0;
1313
//~^ ERROR cannot assign to `*r`, which is behind a `&` reference
1414
//~| NOTE `r` is a `&` reference, so the data it refers to cannot be written
15+
16+
#[rustfmt::skip]
17+
let x: &usize = &mut{0};
18+
//~^ HELP consider changing this to be a mutable reference
19+
*x = 1;
20+
//~^ ERROR cannot assign to `*x`, which is behind a `&` reference
21+
//~| NOTE `x` is a `&` reference, so the data it refers to cannot be written
22+
23+
#[rustfmt::skip]
24+
let y: &usize = &mut(0);
25+
//~^ HELP consider changing this to be a mutable reference
26+
*y = 1;
27+
//~^ ERROR cannot assign to `*y`, which is behind a `&` reference
28+
//~| NOTE `y` is a `&` reference, so the data it refers to cannot be written
1529
}

src/test/ui/borrowck/issue-85765.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,25 @@ LL |
1616
LL | *r = 0;
1717
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written
1818

19-
error: aborting due to 2 previous errors
19+
error[E0594]: cannot assign to `*x`, which is behind a `&` reference
20+
--> $DIR/issue-85765.rs:19:5
21+
|
22+
LL | let x: &usize = &mut{0};
23+
| - help: consider changing this to be a mutable reference: `&mut usize`
24+
LL |
25+
LL | *x = 1;
26+
| ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
27+
28+
error[E0594]: cannot assign to `*y`, which is behind a `&` reference
29+
--> $DIR/issue-85765.rs:26:5
30+
|
31+
LL | let y: &usize = &mut(0);
32+
| - help: consider changing this to be a mutable reference: `&mut usize`
33+
LL |
34+
LL | *y = 1;
35+
| ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written
36+
37+
error: aborting due to 4 previous errors
2038

2139
Some errors have detailed explanations: E0594, E0596.
2240
For more information about an error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)