Skip to content

IndexMut chosen when Index would suffice #29869

Closed
@alexcrichton

Description

@alexcrichton

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions