Skip to content

Diagnostic for E0596 points at the wrong borrow? #68786

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

Open
lf- opened this issue Feb 2, 2020 · 3 comments
Open

Diagnostic for E0596 points at the wrong borrow? #68786

lf- opened this issue Feb 2, 2020 · 3 comments
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lf-
Copy link
Contributor

lf- commented Feb 2, 2020

I was writing the following function but forgot to put .as_mut_slice() rather than .as_slice():

fn find_closest(input: &str, options: &Vec<&str>) {
    let mut distances: Vec<_> = options
        .iter()
        .map(|_| ("a", 1.)) // actual code removed but types are the same
        .collect();
    distances
        .as_slice()
        .sort_unstable_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
}

This produced a E0596, with a message that I had trouble with as someone new to Rust:

error[E0596]: cannot borrow data in a `&` reference as mutable
  --> src\app.rs:18:5
   |
18 | /     distances
19 | |         .as_slice()
   | |___________________^ cannot borrow as mutable

error: aborting due to previous error

When trying to figure out what the compiler was taking issue with, I thought that it meant that it wanted the elements in the Vec to be themselves mutable, which didn't make any sense to me.

I think the diagnostic is referring to the borrow done by the .sort_unstable_by(F), but it is not pointing at it and the current message can be misread as meaning that the .as_slice() is borrowing something improperly itself, which it is not.

The diagnostic would be more helpful if it was something like this:

error[E0596]: cannot borrow data in a `&` reference as mutable
  --> src\app.rs:18:5
   |
18 | /     distances
19 | |         .as_slice()
20 | |         .sort_unstable_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
   | |_____________^ sort_unstable_by cannot borrow &self as mutable

error: aborting due to previous error
rustc --version
rustc 1.42.0-nightly (212b2c7da 2020-01-30)
@estebank
Copy link
Contributor

estebank commented Feb 3, 2020

FWIW, the solution is for you to use distances.as_mut_slice().

@estebank estebank 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. A-borrow-checker Area: The borrow checker labels Feb 3, 2020
@lf-
Copy link
Contributor Author

lf- commented Feb 3, 2020

Yep, I figured out that was the issue, but the compiler diagnostic didn't help :)

@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 3, 2020
@estebank
Copy link
Contributor

estebank commented Feb 3, 2023

Triage: the span now covers what was desired

error[E0596]: cannot borrow data in a `&` reference as mutable
 --> src/lib.rs:6:5
  |
6 | /     distances
7 | |         .as_slice()
8 | |         .sort_unstable_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
  | |________________________________________________________________^ cannot borrow as mutable

But I'd like it if we could say "hey, distances is mut, as_slice() borrows & and not &mut, you likely want to change it".

CC #45405

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. 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