Skip to content

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

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

Open
safinaskar opened this issue Apr 15, 2023 · 4 comments
Open

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

safinaskar opened this issue Apr 15, 2023 · 4 comments
Labels
A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@safinaskar
Copy link
Contributor

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

@safinaskar safinaskar added the C-bug Category: This is a bug. label Apr 15, 2023
@safinaskar
Copy link
Contributor Author

@rustbot label +A-lifetimes +T-compiler +A-borrow-checker +NLL-polonius +A-NLL

@rustbot rustbot added A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions A-NLL Area: Non-lexical lifetimes (NLL) NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 15, 2023
@safinaskar
Copy link
Contributor Author

Even &mut v[(v.len() - 3)..] doesn't compile!

@csmoe
Copy link
Member

csmoe commented Apr 15, 2023

Duplicate of #30127

@safinaskar
Copy link
Contributor Author

Indeed. v.index_mut(v[0]..) works

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-lifetimes Area: Lifetimes / regions A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants