Skip to content

Borrow checker gives confusing error message when in loop #10876

Closed

Description

When the borrow check fails in a loop, the error message can be quite confusing:

enum Nat {
    S(~Nat),
    Z
}
fn test(x: &mut Nat) {
    let mut p = &mut *x;
    loop {
        match p {
            &Z => break,
            &S(~ref mut n) => p = &mut *n
        }
    }
}

The borrow checker informs me:

Test.rs|43 col 16 error| cannot borrow `*(*p)#0` as mutable more than once at a time
Test.rs|43 col 16 n| second borrow of `*(*p)#0` as mutable occurs here

This message is confusing, because the two occurrences have the same line number and column. In fact, the error message is right: the borrow check only fails because it is being performed in a loop. The error message should mention something like that to the effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions