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 error should suggest adding mut rather than a type signature #133939

Open
josephcsible opened this issue Dec 5, 2024 · 0 comments
Open
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

@josephcsible
Copy link

josephcsible commented Dec 5, 2024

Code

struct Bar();

impl Bar {
    fn mutate(&mut self) {}
}

struct Foo(Bar);

fn f() -> Option<Foo> {
    None
}

fn main() {
    while let Some(x) = f() {
        let y = match x {
            Foo(ref bar) => {
                bar
            }
        };
        y.mutate()
    }
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0596]: cannot borrow `*y` as mutable, as it is behind a `&` reference
  --> src/main.rs:20:9
   |
20 |         y.mutate()
   |         ^ `y` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |
help: consider annotating `Bar` with `#[derive(Clone)]`
   |
1  + #[derive(Clone)]
2  | struct Bar();
   |
help: consider specifying this binding's type
   |
15 |         let y: &mut Bar = match x {
   |              ++++++++++

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

help: consider changing this to be mutable
   |
16 |             Foo(ref mut bar) => {
   |                     +++

Rationale and extra context

If you make rustc's first suggested change, you still get the same error, and if you make the second, you just end up with this error after:

error[E0308]: mismatched types
  --> src/main.rs:17:17
   |
17 |                 bar
   |                 ^^^ types differ in mutability
   |
   = note: expected mutable reference `&mut Bar`
                      found reference `&Bar`

Which wasn't really any help at all. With my alternative suggested change, you'll still get one more error, but that one will suggest changing Some(x) to Some(mut x), and that will make it work.

Other cases

Rust Version

Tested on the Rust Playground with both stable 1.83.0 and 1.85.0-nightly 2024-12-04 acabb52 (sorry for the lack of the exact command you wanted, but see rust-lang/rust-playground#1043)

Anything else?

No response

@josephcsible josephcsible 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 Dec 5, 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

No branches or pull requests

1 participant