Skip to content

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

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

Activity

  1. nikomatsakis commented on Nov 25, 2015

    @nikomatsakis
    ContributorAuthor

    triage: P-medium

  2. durka commented on Nov 25, 2015

    @durka
    Contributor

    A-const-fn?

  3. durka commented on Nov 25, 2015

    @durka
    Contributor

    Do you just mean the help message is wrong, or it should have automatically created a move closure?

  4. nikomatsakis commented on Dec 15, 2015

    @nikomatsakis
    ContributorAuthor

    @durka neither. Normally, even for non-move closures, we infer when particular variables are used in the body and we move just those variables. For some reason, that is not triggering in this particular case.

  5. nagisa commented on Jan 29, 2016

    @nagisa
    Member

    Might be duplicate of #26139?

  6. added
    E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.
    on Aug 25, 2016
  7. nikomatsakis commented on Aug 25, 2016

    @nikomatsakis
    ContributorAuthor

    Related example from #26139:

    struct Name(String);
    
    fn apply<F>(f: F) where
        F: FnOnce() {
    
        f()
    }
    
    fn main() {
        let n = Name(String::new());
        let consume = || {
            let Name(i) = n;
        };
    
        apply(consume);
    }
  8. added
    C-bugCategory: This is a bug.
    and removed on Jul 24, 2017
  9. added a commit that references this issue on Nov 12, 2017
    c1aacdc
  10. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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