Skip to content

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

Closed
@turbulencetoo

Description

@turbulencetoo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)NLL-diagnosticsWorking towards the "diagnostic parity" goal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions