Skip to content

Arithmetic makes borrow checker overly restrictive #29743

Closed
@dotdash

Description

@dotdash

UPDATE: This was fixed by #47167 but still needs a regression test to be added.


fn main() {
    let mut i = [1,2,3];
    i[i[0]] = 0; // Works
    i[i[0] - 1] = 0; // Is rejected
}

I had expect the - 1 not to make any difference, but the borrow checker complains with the following error:

<anon>:4:7: 4:11 error: cannot use `i[..]` because it was mutably borrowed
<anon>:4     i[i[0] - 1] = 0;
               ^~~~
<anon>:4:5: 4:6 note: borrow of `i` occurs here
<anon>:4     i[i[0] - 1] = 0;

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.P-mediumMedium priorityT-langRelevant to the language team, which will review and decide on the PR/issue.fixed-by-NLLBugs fixed, but only when NLL is enabled.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions