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

Variable capture in closures is too restrictive #8338

Closed
dovreshef opened this issue Aug 6, 2013 · 4 comments
Closed

Variable capture in closures is too restrictive #8338

dovreshef opened this issue Aug 6, 2013 · 4 comments

Comments

@dovreshef
Copy link

This program

fn main() {
    let mut x: int;
    let closure = || { x = 42; };
    closure();
}

Output error: capture of possibly uninitialized variable: 'x', even though as an lvalue there is no danger of uninitialized usage. Could the compiler be made smarter about this? Maybe add a rule that if the variable first appearance in the closure is as an lvalue it's ok.

@alexcrichton
Copy link
Member

cc #2202 and @nikomatsakis

@huonw
Copy link
Member

huonw commented Aug 11, 2014

Triage; no change; I imagine this could be a bit harder with unboxed closures (since closures then have a more transparent representation, with less magic).

@nikomatsakis
Copy link
Contributor

I think this will never be fixed. In general, we model closures in the compiler and in the language as a kind of advanced sugar for things you could type yourself, and the fact is that the captured variable is being bundled up into the environment when it is not initialized. Nominating for closure.

@nikomatsakis
Copy link
Contributor

Decided to just close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants