Skip to content

"&mut v[v[0]..]" doesn't compile #110351

Open
@safinaskar

Description

@safinaskar

Consider this code:

fn main() {
    let mut v = vec![1, 2];
    let x = &mut v[v[0]..];
}

It doesn't compile ( https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b69d691f31f8c8ef9e7ca949c4be542b, rustc 1.68.2).

Of course, this example can be simply fixed by extracting v[0]. But I still argue that this is correct code, and it should compile. Even if this is not possible to fix in short time, I still think this should be fixed in some next-generation borrow checker.

I actually got similar code in my actual production code base. This is simplified example from my production code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e56a648d80a8af7c3eabdcfe504df774

Adding -Zpolonius changes nothing

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.NLL-poloniusIssues related for using Polonius in the borrow checkerT-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