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

When type inference depends on the content of a closure, earlier statements do not make use of this information #51273

Closed
dylanede opened this issue Jun 1, 2018 · 3 comments
Labels
A-inference Area: Type inference

Comments

@dylanede
Copy link
Contributor

dylanede commented Jun 1, 2018

Minimal example:

fn main() {
    // Behaviour does not appear linked to the use of unimplemented!()
    let x = unimplemented!();

    // Move the definition of g above this line to get this to compile
    x.0; // E0282: cannot infer type for `_`
    let g = || {
        let x: (i32, i32) = x;
    };
}

playground

This appears in stable, beta and nightly.

This appears to be a case where type inference should not depend on the order of these statements in the function.

This problem can be a deal-breaker when unnameable types are involved and reordering the statements isn't possible.

@dylanede dylanede changed the title When type inference depends on the content of a closure, closures appearing in earlier statements do not make use of this information When type inference depends on the content of a closure, Earlier statements do not make use of this information Jun 1, 2018
@dylanede dylanede changed the title When type inference depends on the content of a closure, Earlier statements do not make use of this information When type inference depends on the content of a closure, earlier statements do not make use of this information Jun 1, 2018
@leoyvens
Copy link
Contributor

leoyvens commented Jun 1, 2018

You're right, type checking should not depend on the order of the statements, however it's a known limitation that sometimes it does, notably in indexing x[0], field access x.0 and method cals x.foo(). You don't even need the closure for that:

let x = unimplemented!();
// Move `let x` above this line to get this to compile
x.0; // E0282: cannot infer type for `_`
     // note: type must be known at this point
let x: (i32, i32) = x;

If you look closely at the nightly error you'll see the note: type must be known at this point which marks the fact that type checking failed early. We hope to improve this in the future, but for now I'm closing this as a known issue. Thank you for the report!

@leoyvens leoyvens closed this as completed Jun 1, 2018
@dylanede
Copy link
Contributor Author

dylanede commented Jun 1, 2018

@leodasvacas, OK, is there an issue tracking this somewhere then? If not, maybe one should be created.

@leoyvens
Copy link
Contributor

leoyvens commented Jun 1, 2018

@dylanede We don't have a comprehensive issue tracking this, mostly because we don't know if we'll ever be able to actually fix this, but it would be good to have one. #50692 is fundamentally about the same thing, also #36418 and #8280. rust-lang/chalk#105 is about the first thing we need to do to improve this.

@leoyvens leoyvens added the A-inference Area: Type inference label Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference
Projects
None yet
Development

No branches or pull requests

2 participants