Skip to content

Closure inference fails to infer that move is required when pattern matching #30046

Closed
@nikomatsakis

Description

@nikomatsakis

See this example, which fails to compile:

fn foo<F>(f: F)
    where F: FnOnce()
{
}

fn main() {
    let x = Some(vec![1, 2, 3]);
    foo(|| {
        match x {
            Some(y) => { }
            None => { }
        }
    });
}

Error:

<anon>:9:15: 9:16 error: cannot move out of captured outer variable in an `FnOnce` closure
<anon>:9         match x {
                       ^
<anon>:10:18: 10:19 note: attempting to move value to here
<anon>:10             Some(y) => { }
                           ^
<anon>:10:18: 10:19 help: to prevent the move, use `ref y` or `ref mut y` to capture value by reference
error: aborting due to previous error
playpen: application terminated with error code 101

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-type-systemArea: Type systemC-bugCategory: This is a bug.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions