Skip to content

Using an iterator in a for loop doesn't borrow the object #16820

Closed
@elinorbgr

Description

@elinorbgr

This simple code compiles on current master (0d3bd77) :

fn main() {
    let mut vector = vec!(1u, 2u);
    for &x in vector.iter() {
        let cap = vector.capacity();
        println!("Capacity was: {}", cap);
        vector.grow(cap, &0u); // be sure to cause reallocation
        *vector.get_mut(1u) = 5u;
        println!("Value: {}", x);
    }
}

and outputs:

Capacity was: 4
Value: 1
Capacity was: 8
Value: 2

while on 0.11 the borrow checker would complain that vector is already immutably borrowed by vector.iter().
It allows me to write 5u in vector[1] and then read it as 2u, because reallocation of the vector caused the iterator's slice to refer to freed memory.

(was found in a StackOverflow question : http://stackoverflow.com/q/25528271/2536143 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions