Skip to content

IndexMut chosen when Index would suffice #29869

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
alexcrichton opened this issue Nov 16, 2015 · 3 comments
Closed

IndexMut chosen when Index would suffice #29869

alexcrichton opened this issue Nov 16, 2015 · 3 comments

Comments

@alexcrichton
Copy link
Member

This code incorrectly reports that vec needs to be listed as mutable:

use std::cell::RefCell;

fn stuff(a: &mut u32) {
}

pub fn main() {
    let vals: Vec<RefCell<u32>> = vec![];

    stuff(&mut *vals[0].borrow_mut());
}
<anon>:9:17: 9:21 error: cannot borrow immutable local variable `vals` as mutable
<anon>:9     stuff(&mut *vals[0].borrow_mut());
                         ^~~~
error: aborting due to previous error
playpen: application terminated with error code 101

If, however, you change the code slightly:

use std::cell::RefCell;

fn stuff(a: &mut u32) {
}

pub fn main() {
    let vals: Vec<RefCell<u32>> = vec![];

    // notice the parens + & around vals[0]
    stuff(&mut *(&vals[0]).borrow_mut());
}

Then this will compile successfully.

@carllerche
Copy link
Member

I just hit this (replying to track issue).

@nagisa
Copy link
Member

nagisa commented Nov 16, 2015

Duplicate of #28935

@alexcrichton
Copy link
Member Author

Aha!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants