Skip to content

Uplifting captures to by-move is too conservative #30745

Closed
@nagisa

Description

@nagisa

In code like this there’s some algorithm in place which makes the capture of variable x by-value rather than the default by-ref:

fn main(){
    let x = Some(vec![0]);
    (||{ 
        drop(x); // takes `x` by-value, therefore closure captures `x` by-value as well.
    })()
}

However, x will not be captured by-value when the x is matched on in a moving manner:

fn main() {
    let x = Some(vec![0]);
    (|| {
        match x { // cannot move out of captured outer variable in an `Fn` closure [E0507]
            Some(x) => {}, // attempting to move value to here
            None => {}
        }
    })()
}

It would be nice if the algorithm handled more cases (including one presented here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions