Skip to content

Invalid suggestion for mutability correction in error message #33819

Description

@Vtec234

The compiler makes an invalid suggestion for how to correct the immutability of a reference in a match block. To reproduce, try to compile the following code:

use std::fs::File;
use std::io::Write;

fn main() {
    let mut op = Some(File::open("").unwrap());
    match op {
        Some(ref mut f) => { write!(f, "!").unwrap();  },
        None => {},
    }
}

On stable and beta, the compiler will print:

<anon>:7:33: 7:34 error: cannot borrow immutable local variable `f` as mutable
<anon>:7         Some(ref f) => { write!(f, "!").unwrap();  },
                                     ^
<anon>:7:26: 7:40 note: in this expansion of write! (defined in <std macros>)
<anon>:7:14: 7:19 help: to make the local variable mutable, use `mut` as shown:
<anon>:          Some(mut ref f) => { write!(f, "!").unwrap();  },

On nightly, it will print:

error: cannot borrow immutable local variable `f` as mutable
 --> <anon>:7:33
7 |>         Some(ref f) => { write!(f, "!").unwrap();  },
  |>              -----              ^ cannot borrow mutably
  |>              |
  |>              use `mut ref f` here to make mutable

Both of these suggestions are erroneous, as mut ref f is invalid syntax, and should be ref mut f.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions