Skip to content

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

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

Closed
k21 opened this issue Dec 13, 2018 · 2 comments
Closed

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

k21 opened this issue Dec 13, 2018 · 2 comments
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions NLL-diagnostics Working towards the "diagnostic parity" goal

Comments

@k21
Copy link

k21 commented Dec 13, 2018

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)

@estebank
Copy link
Contributor

CC #53773 & #54256.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-borrow-checker Area: The borrow checker NLL-diagnostics Working towards the "diagnostic parity" goal labels Dec 13, 2018
@estebank
Copy link
Contributor

Current 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 later used here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions NLL-diagnostics Working towards the "diagnostic parity" goal
Projects
None yet
Development

No branches or pull requests

2 participants