Skip to content

Destructuring fails to infer FnOnce #26139

New issue

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

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

Already on GitHub? # to your account

Closed
mdinger opened this issue Jun 9, 2015 · 4 comments
Closed

Destructuring fails to infer FnOnce #26139

mdinger opened this issue Jun 9, 2015 · 4 comments
Labels
A-type-system Area: Type system

Comments

@mdinger
Copy link
Contributor

mdinger commented Jun 9, 2015

This should move n but it doesn't. It doesn't seem to infer it properly.

Code:

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

Error:

<anon>:12:23: 12:24 error: cannot move out of captured outer variable in an `Fn` closure
<anon>:12         let Name(i) = n;
                                ^
@abonander
Copy link
Contributor

Currently the compiler needs a hint when it should move a value into a closure:

let consume = move || {
    //...
};

Eventually, I think we do want to infer moving upvars but it might be difficult. I think there's already an issue for tracking this problem that explains it in more detail.

@mdinger
Copy link
Contributor Author

mdinger commented Jun 9, 2015

Even with move ||, the exact same error remains.

@talchas
Copy link

talchas commented Jun 9, 2015

So move also is insufficient here - you wind up with the same error. However if you write

    let n = Name(String::new());
    let m = Name(String::new());
    let consume = move || {
        let m = m;
        let Name(i) = n;
    };

it will work, and yet fail ("error: cannot move out of captured outer variable in an FnOnce closure") without the move. It seems that the destructuring-let isn't included in figuring out it needs to be an FnOnce closure or something, /and/ the closure is missing move.

@brson
Copy link
Contributor

brson commented Aug 25, 2016

Closing in favor of #30046

@brson brson closed this as completed Aug 25, 2016
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

5 participants