Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E0596 should suggest changing type if it suggest taking taking mutable reference instead of just reference #106857

Open
matthiaskrgr opened this issue Jan 14, 2023 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Jan 14, 2023

Given the following code:

fn main() {
    let x: &[isize] = &[1, 2, 3, 4, 5];
    // Can't mutably slice an immutable slice
    let _slice: &mut [isize] = &mut [0, 1];
    let _ = &mut x[2..4]; //~ERROR cannot borrow `*x` as mutable, as it is behind a `&` reference
}

The current output is:

error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
 --> src/lib.rs:5:18
  |
5 |     let _ = &mut x[2..4]; //~ERROR cannot borrow `*x` as mutable, as it is behind a `&` reference
  |                  ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
  |
help: consider changing this to be a mutable reference
  |
2 |     let x: &[isize] = &mut [1, 2, 3, 4, 5];
  |                       ~~~~~~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0596`.

If we apply the suggestion this will cause the next problem, because &mut[1,2...] is not &[isize] but &mut [isize] 🙃

fn main() {
    let x: &[isize] = &mut [1, 2, 3, 4, 5];
    // Can't mutably slice an immutable slice
    let _slice: &mut [isize] = &mut [0, 1];
    let _ = &mut x[2..4]; //~ERROR cannot borrow `*x` as mutable, as it is behind a `&` reference
}

so we should suggest let x: &mut [isize] = &mut [1, 2, 3, 4, 5]; right away, if possible.

@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels Jan 14, 2023
@estebank
Copy link
Contributor

estebank commented Jan 14, 2023

I think we can use the same strategy we use here https://github.com/rust-lang/rust/pull/106360/files#diff-aef5d12ac7ec2060165cf1db78128f941c789bb2fc90c7ed351049b0be4d435eR1361-R1378, to turn spans into HIR nodes we can operate on, in

fn suggest_ampmut<'tcx>(
tcx: TyCtxt<'tcx>,
local_decl: &mir::LocalDecl<'tcx>,
opt_assignment_rhs_span: Option<Span>,
opt_ty_info: Option<Span>,
) -> (bool, Span, String) {
if let Some(assignment_rhs_span) = opt_assignment_rhs_span
&& let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span)
{
let is_mutbl = |ty: &str| -> bool {
if let Some(rest) = ty.strip_prefix("mut") {
match rest.chars().next() {
// e.g. `&mut x`
Some(c) if c.is_whitespace() => true,
// e.g. `&mut(x)`
Some('(') => true,
// e.g. `&mut{x}`
Some('{') => true,
// e.g. `&mutablevar`
_ => false,
}
} else {
false
}
};
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
let lt_name = &src[1..ws_pos];
let ty = src[ws_pos..].trim_start();
if !is_mutbl(ty) {
return (true, assignment_rhs_span, format!("&{lt_name} mut {ty}"));
}
} else if let Some(stripped) = src.strip_prefix('&') {
let stripped = stripped.trim_start();
if !is_mutbl(stripped) {
return (true, assignment_rhs_span, format!("&mut {stripped}"));
}
}
}

The current output for the second case after applying the suggestion does guide you in the right direction, but ideally we won't need it in the future:

error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
 --> src/main.rs:5:18
  |
2 |     let x: &[isize] = &mut [1, 2, 3, 4, 5];
  |         - consider changing this binding's type to be: `&mut [isize]`
...
5 |     let _ = &mut x[2..4]; //~ERROR cannot borrow `*x` as mutable, as it is behind a `&` reference
  |                  ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable

@Ezrashaw
Copy link
Contributor

Hmm, I'll have a go at this.

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue May 9, 2023
…stebank

tweak "make mut" spans when assigning to locals

Work towards fixing rust-lang#106857

This PR just cleans up a lot of spans which is helpful before properly fixing the issues. Best reviewed commit-by-commit.

r? `@estebank`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants