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

Compiler with NLL feature gives mangled suggestion text for E0596 #51879

Closed
turbulencetoo opened this issue Jun 28, 2018 · 2 comments · Fixed by #51612
Closed

Compiler with NLL feature gives mangled suggestion text for E0596 #51879

turbulencetoo opened this issue Jun 28, 2018 · 2 comments · Fixed by #51612
Labels
A-NLL Area: Non-lexical lifetimes (NLL) NLL-diagnostics Working towards the "diagnostic parity" goal

Comments

@turbulencetoo
Copy link

Compiling this code (playground):

#![feature(nll)]

use std::collections::{HashMap};

fn main() {
    let map: HashMap<u32, Vec<u32>> = HashMap::new();
    for (_, nodes) in map.iter() {
            nodes.retain(|&n| n == 3);
    }
}

Gives this error:

error[E0596]: cannot borrow immutable item `*nodes` as mutable
 --> src/main.rs:8:13
  |
7 |     for (_, nodes) in map.iter() {
  |             ----- help: consider changing this to be a mutable reference: `&mut odes`
8 |             nodes.retain(|&n| n == 3);
  |             ^^^^^ `nodes` is a `&` reference, so the data it refers to cannot be borrowed as mutable

See that it creates a recommendation of using "&mut odes" which assumes I had an &nodes in my source code.

A similar error is seen here (playground) where it chops off the value being iterated over instead of the value being bound to the iterator's Item.

5 |     for nodes in vector.iter() {
  |                  ------------- help: consider changing this to be a mutable reference: `&mut ector.iter()`

Removing the #![feature(nll)] causes there to be less suggestion text from the compiler, and thus does not have this bug.

@matthewjasper matthewjasper added WG-compiler-nll NLL-diagnostics Working towards the "diagnostic parity" goal A-NLL Area: Non-lexical lifetimes (NLL) labels Jun 29, 2018
@matthewjasper
Copy link
Contributor

cc #51612

@nikomatsakis
Copy link
Contributor

Triage: Not yet fixed. Worth fixing for the preview, particularly since I think there is a pending PR that fixes it?

@nikomatsakis nikomatsakis added this to the Rust 2018 Preview 2 milestone Jun 29, 2018
bors added a commit that referenced this issue Jul 10, 2018
NLL: fix E0594 "change to mutable ref" suggestion

Fix #51515.
Fix #51879.

Questions:
- [x] Is this the right place to fix this? It feels brittle, being so close to the frontend. **It's probably fine.**
- [ ] Have I missed any other cases that trigger this behavior?
- [x] Is it okay to use HELP and SUGGESTION in the UI test? **Yes.**
- [x] Do I need more tests for this? **No.**
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jul 10, 2018
…r=pnkfelix

NLL: fix E0594 "change to mutable ref" suggestion

Fix rust-lang#51515.
Fix rust-lang#51879.

Questions:
- [x] Is this the right place to fix this? It feels brittle, being so close to the frontend. **It's probably fine.**
- [ ] Have I missed any other cases that trigger this behavior?
- [x] Is it okay to use HELP and SUGGESTION in the UI test? **Yes.**
- [x] Do I need more tests for this? **No.**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) NLL-diagnostics Working towards the "diagnostic parity" goal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants