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

cannot assign to data in an index of HashMap<_, _> diagnostic suggests solution that does not type check #134917

Open
epilys opened this issue Dec 30, 2024 · 0 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

@epilys
Copy link
Contributor

epilys commented Dec 30, 2024

Code

use std::collections::HashMap;

fn main() {
    let mut map: HashMap<bool, bool> = HashMap::default();
    map[&true] = false;
}

Current output

error[E0594]: cannot assign to data in an index of `HashMap<bool, bool>`
 --> src/main.rs:5:5
  |
5 |     map[&true] = false;
  |     ^^^^^^^^^^^^^^^^^^ cannot assign
  |
  = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<bool, bool>`
help: to modify a `HashMap<bool, bool>`, use `.get_mut()`, `.insert()` or the entry API
  |
5 |     map.insert(&true, false);
  |        ~~~~~~~~     ~      +
5 |     map.get_mut(&true).map(|val| { *val = false; });
  |        ~~~~~~~~~     ~~~~~~~~~~~~~~~~~~        ++++
5 |     let val = map.entry(&true).or_insert(false);
  |     +++++++++    ~~~~~~~     ~~~~~~~~~~~~     +

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

Desired output

error[E0594]: cannot assign to data in an index of `HashMap<bool, bool>`
 --> src/main.rs:5:5
  |
5 |     map[&true] = false;
  |     ^^^^^^^^^^^^^^^^^^ cannot assign
  |
  = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<bool, bool>`
help: to modify a `HashMap<bool, bool>`, use `.get_mut()`, `.insert()` or the entry API
  |
5 |     map.insert(true, false);
  |        ~~~~~~~~    ~      +
5 |     map.get_mut(&true).map(|val| { *val = false; });
  |        ~~~~~~~~~     ~~~~~~~~~~~~~~~~~~        ++++
5 |     let val = map.entry(true).or_insert(false);
  |     +++++++++    ~~~~~~~    ~~~~~~~~~~~~     +

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

Rationale and extra context

HashMap::insert and HashMap::entry take owned key values.

Other cases

Rust Version

$ rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1

Anything else?

Similar fixes might be necessary for other container types in std::collections

@epilys epilys 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 30, 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