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 ```
This commit is contained in:
commit
f9d00b4a78
3 changed files with 37 additions and 7 deletions
|
@ -905,6 +905,8 @@ fn suggest_ampmut<'tcx>(
|
||||||
Some(c) if c.is_whitespace() => true,
|
Some(c) if c.is_whitespace() => true,
|
||||||
// e.g. `&mut(x)`
|
// e.g. `&mut(x)`
|
||||||
Some('(') => true,
|
Some('(') => true,
|
||||||
|
// e.g. `&mut{x}`
|
||||||
|
Some('{') => true,
|
||||||
// e.g. `&mutablevar`
|
// e.g. `&mutablevar`
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
@ -912,9 +914,7 @@ fn suggest_ampmut<'tcx>(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let (true, Some(ws_pos)) =
|
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
|
||||||
(src.starts_with("&'"), src.find(|c: char| -> bool { c.is_whitespace() }))
|
|
||||||
{
|
|
||||||
let lt_name = &src[1..ws_pos];
|
let lt_name = &src[1..ws_pos];
|
||||||
let ty = src[ws_pos..].trim_start();
|
let ty = src[ws_pos..].trim_start();
|
||||||
if !is_mutbl(ty) {
|
if !is_mutbl(ty) {
|
||||||
|
@ -940,9 +940,7 @@ fn suggest_ampmut<'tcx>(
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span) {
|
if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span) {
|
||||||
if let (true, Some(ws_pos)) =
|
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
|
||||||
(src.starts_with("&'"), src.find(|c: char| -> bool { c.is_whitespace() }))
|
|
||||||
{
|
|
||||||
let lt_name = &src[1..ws_pos];
|
let lt_name = &src[1..ws_pos];
|
||||||
let ty = &src[ws_pos..];
|
let ty = &src[ws_pos..];
|
||||||
return (highlight_span, format!("&{} mut{}", lt_name, ty));
|
return (highlight_span, format!("&{} mut{}", lt_name, ty));
|
||||||
|
|
|
@ -12,4 +12,18 @@ fn main() {
|
||||||
*r = 0;
|
*r = 0;
|
||||||
//~^ ERROR cannot assign to `*r`, which is behind a `&` reference
|
//~^ ERROR cannot assign to `*r`, which is behind a `&` reference
|
||||||
//~| NOTE `r` is a `&` reference, so the data it refers to cannot be written
|
//~| NOTE `r` is a `&` reference, so the data it refers to cannot be written
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
let x: &usize = &mut{0};
|
||||||
|
//~^ HELP consider changing this to be a mutable reference
|
||||||
|
*x = 1;
|
||||||
|
//~^ ERROR cannot assign to `*x`, which is behind a `&` reference
|
||||||
|
//~| NOTE `x` is a `&` reference, so the data it refers to cannot be written
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
let y: &usize = &mut(0);
|
||||||
|
//~^ HELP consider changing this to be a mutable reference
|
||||||
|
*y = 1;
|
||||||
|
//~^ ERROR cannot assign to `*y`, which is behind a `&` reference
|
||||||
|
//~| NOTE `y` is a `&` reference, so the data it refers to cannot be written
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,25 @@ LL |
|
||||||
LL | *r = 0;
|
LL | *r = 0;
|
||||||
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written
|
| ^^^^^^ `r` is a `&` reference, so the data it refers to cannot be written
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error[E0594]: cannot assign to `*x`, which is behind a `&` reference
|
||||||
|
--> $DIR/issue-85765.rs:19:5
|
||||||
|
|
|
||||||
|
LL | let x: &usize = &mut{0};
|
||||||
|
| - help: consider changing this to be a mutable reference: `&mut usize`
|
||||||
|
LL |
|
||||||
|
LL | *x = 1;
|
||||||
|
| ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
|
||||||
|
|
||||||
|
error[E0594]: cannot assign to `*y`, which is behind a `&` reference
|
||||||
|
--> $DIR/issue-85765.rs:26:5
|
||||||
|
|
|
||||||
|
LL | let y: &usize = &mut(0);
|
||||||
|
| - help: consider changing this to be a mutable reference: `&mut usize`
|
||||||
|
LL |
|
||||||
|
LL | *y = 1;
|
||||||
|
| ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written
|
||||||
|
|
||||||
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
Some errors have detailed explanations: E0594, E0596.
|
Some errors have detailed explanations: E0594, E0596.
|
||||||
For more information about an error, try `rustc --explain E0594`.
|
For more information about an error, try `rustc --explain E0594`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue