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

More precisely point out what is immutable, in E0596 "cannot borrow data in a & reference as mutable" #113842

Open
kpreid opened this issue Jul 18, 2023 · 2 comments
Labels
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.

Comments

@kpreid
Copy link
Contributor

kpreid commented Jul 18, 2023

Code

mod some_library {
    pub fn foo(_: &mut [i32]) {}
    pub fn bar<'a>() -> &'a [i32] {
        &[]
    }
    pub fn bar_mut<'a>() -> &'a mut [i32] {
        &mut []
    }
}

fn main() {
    some_library::foo(&mut some_library::bar());
}

Current output

error[E0596]: cannot borrow data in a `&` reference as mutable
  --> src/main.rs:12:23
   |
12 |     some_library::foo(&mut some_library::bar());
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

Desired output

error[E0596]: cannot borrow data in a `&` reference as mutable
  --> src/main.rs:12:23
   |
12 |     some_library::foo(&mut some_library::bar());
   |                            ^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

help: `some_library::bar()` is of type `&[i32]`, which is an immutable reference
help: `some_library::bar_mut()` might be the operation you need

Rationale and extra context

The current output is confusing because it does not indicate which part of the input is the source of the immutability, and especially because the span contains the &mut (which in this case is irrelevant, but is often attempted and may be needed to trigger DerefMut).

Many newcomers to Rust are not yet familiar with the idea that in order to get mutable access to some data, every part of the path by which they get that data must be mutable.

I have proposed that the compiler suggest calling bar_mut() per the naming convention, but this is questionable since the function might have different semantics, and it is secondary to the main issue here of pointing to the immutability clearly.

Other cases

Without the &mut,

    some_library::foo(some_library::bar());

the error is much clearer, but completely different since no reborrowing is involved:

error[E0308]: mismatched types
  --> src/main.rs:12:23
   |
12 |     some_library::foo(some_library::bar());
   |     ----------------- ^^^^^^^^^^^^^^^^^^^ types differ in mutability
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected mutable reference `&mut [i32]`
                      found reference `&[i32]`
@kpreid kpreid 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. labels Jul 18, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 18, 2023
@mcclure
Copy link

mcclure commented Jul 18, 2023

I ran into this problem with bytemuck (I was using copypasted code and didn't realize, though perhaps I should have, that a _mut version of the function call I had copypasted existed) and this error message would have helped a lot.

In my opinion, the _mut suggestion is appropriate if/when the _mut variant exists. Even though the semantics of the corresponding _mut might not be exactly as expected, Rust already offers suggested spelling-error corrections (for example instead of bar in the sample try calling bar_mug), and the user understands these are not necessarily correct.

@chenyukang chenyukang self-assigned this Jul 21, 2023
@chenyukang
Copy link
Member

Here is my local result of a trivial fix:

error[E0596]: cannot borrow data in a `&` reference as mutable
  --> ./p/ref.rs:12:23
   |
12 |     some_library::foo(&mut some_library::bar());
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
   |
   = help: `some_library::bar()` is of type `&[i32]`, which is an immutable reference

I'm not sure whether to change the span of cannot borrow as mutable, seems a lot of borrow checking diagnostic are using the whole expr span.

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 30, 2023
@chenyukang chenyukang removed their assignment Mar 2, 2024
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants