Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nikomatsakis opened this Issue Nov 25, 2015 · 6 comments

Comments

Projects
None yet
6 participants
@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 25, 2015

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
@nikomatsakis

This comment has been minimized.

Copy link
Contributor Author

nikomatsakis commented Nov 25, 2015

triage: P-medium

@durka

This comment has been minimized.

Copy link
Contributor

durka commented Nov 25, 2015

A-const-fn?

@durka

This comment has been minimized.

Copy link
Contributor

durka commented Nov 25, 2015

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

@nikomatsakis

This comment has been minimized.

Copy link
Contributor Author

nikomatsakis commented Dec 15, 2015

@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.

@nagisa

This comment has been minimized.

Copy link
Contributor

nagisa commented Jan 29, 2016

Might be duplicate of #26139?

@nikomatsakis

This comment has been minimized.

Copy link
Contributor Author

nikomatsakis commented Aug 25, 2016

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);
}

@Mark-Simulacrum Mark-Simulacrum added C-bug and removed I-wrong labels Jul 24, 2017

bors added a commit that referenced this issue Nov 12, 2017

Auto merge of #45864 - nikomatsakis:issue-30046-infer-fn-once-in-clos…
…ures, r=eddyb

adjust closure kind based on the guarantor's upvar note

Fixes #30046.

r? @eddyb

@bors bors closed this in #45864 Nov 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.