Skip to content

Slice syntax and &mut operator #55520

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
cramertj opened this issue Oct 30, 2018 · 3 comments
Closed

Slice syntax and &mut operator #55520

cramertj opened this issue Oct 30, 2018 · 3 comments
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cramertj
Copy link
Member

cramertj commented Oct 30, 2018

fn mut_iter(_: &mut dyn Iterator<Item=u8>) {}

fn main() {
    let v = vec![1, 2, 3];
    mut_iter(&mut v.iter().cloned()); // this compiles fine
    mut_iter(&mut v[..].iter().cloned()); // this does not
    mut_iter(&mut (&v[..]).iter().cloned()); // this also compiles fine
}

gives this error:

error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
 --> src/main.rs:6:19
  |
4 |     let v = vec![1, 2, 3];
  |         - help: consider changing this to be mutable: `mut v`
5 |     mut_iter(&mut v.iter().cloned());
6 |     mut_iter(&mut v[..].iter().cloned());
  |                   ^ cannot borrow as mutable

error: aborting due to previous error

In both cases, the only thing being mutably referenced is the iterator, not the vec or the slice itself. It's clear that the &mut is binding loosely to the whole remainder of the expression (as it should) because mut_iter is accepting a mutable reference as an argument, so there's no chance this is being parsed as (&mut v[..])....

What's going on here? Repros on both stable and nightly (with and without NLL, which means it's not just funkiness in AST borrowck, right?).

@cramertj cramertj added A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 30, 2018
@cramertj cramertj changed the title Strange slice syntax and borrowchecking interop Slice syntax and &mut operator Oct 30, 2018
@cramertj cramertj removed the A-borrow-checker Area: The borrow checker label Oct 30, 2018
@cramertj
Copy link
Member Author

Looking further, I'm guessing this has something to do with convert_place_derefs_to_mutable which will convert Index into IndexMut. Exactly why that's making it's way all the way down to the initial slice provided to the call is still a mystery, but it's not a stretch to imagine it recursing on the expression in some less-than-precise way.

@ollie27
Copy link
Member

ollie27 commented Oct 30, 2018

This looks like a duplicate of #28935.

@cramertj
Copy link
Member Author

@ollie27 Indeed! good catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

2 participants