Skip to content

Incorrect "in later iteration of loop" error diagnostic #56785

Closed
@k21

Description

@k21

The 2018 edition adds an incorrect "in later iteration of loop" qualifier to the borrow checker error message if an issues occurs within a loop.

Repro code:

fn main() {
    for _ in 0..1 {
        let mut x = 1;
        let xm = &mut x;
        &x;
        *xm = 2;
    }
}

Playground Link

Actual Output:

error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
 --> src/main.rs:5:9
  |
4 |         let xm = &mut x;
  |                  ------ mutable borrow occurs here
5 |         &x;
  |         ^^ immutable borrow occurs here
6 |         *xm = 2;
  |         ------- mutable borrow used here, in later iteration of loop

error: aborting due to previous error

For more information about this error, try `rustc --explain E0502`.
error: Could not compile `playground`.

Expected Output:

The error message should not say in later iteration of loop since the use occurs in the same iteration of the loop as both borrows. The value and the reference do not survive between loop iterations.

Rust version: stable (1.31.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsNLL-diagnosticsWorking towards the "diagnostic parity" goal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions