Skip to content

Last uses aren't moved into closures by default #1894

Closed
@brson

Description

@brson

This fails:

fn main() {                                                                                                                                                    
    let x = ~1;                                                                                                                                                
    let x_in_parent = ptr::addr_of(*x) as uint;                                                                                                                
    let f = fn@() -> uint {                                                                                                                                    
        ptr::addr_of(*x) as uint                                                                                                                               
    };                                                                                                                                                         
    let x_in_child = f();                                                                                                                                      
    assert x_in_parent == x_in_child;                                                                                                                          
} 

Rewriting it with a capture clause works:

fn main() {                                                                                                                                                    
    let x = ~1;                                                                                                                                                
    let x_in_parent = ptr::addr_of(*x) as uint;                                                                                                                
    let f = fn@[move x]() -> uint {                                                                                                                                    
        ptr::addr_of(*x) as uint                                                                                                                               
    };                                                                                                                                                         
    let x_in_child = f();                                                                                                                                      
    assert x_in_parent == x_in_child;                                                                                                                          
}      

This is a bit surprising and makes spawn not do the expected thing when written with the convenient block closure syntax. For this reason core::task uses fn~ closures with explicit capture clauses.

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