Skip to content

Calling a closure needs to be treated like a unique-immutable borrow #12224

Closed
@nikomatsakis

Description

@nikomatsakis

We made closures linear so as to help prevent recursion, but the other part of that job was never done -- invoking a closure needs to be considered a unique-immutable borrow (a handy concept brought into being, at least internally, by #6801).

The goal is to prevent a program like this from compiling:

use std::hashmap::HashMap;

type Fn<'a> = 'a ||;

fn call1(c: |Fn|) {
    c(|| {
        c(|| ());
    });
}

pub fn main() {
    println!("Hello, world!");
    let mut map = HashMap::new();
    let mut c = 0;
    call1(|f| {
            error!("Inserting");
            map.insert(22, c);
            c += 1;
            error!("Finding");
            let x = map.find(&22).unwrap();
            let y = *x;
            assert_eq!(*x, y);
            f();
            error!("Found {}", *x);
            assert_eq!(*x, y);
});
}

Part of #2202.

Nominating for 1.0 (backwards compat).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions