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

Type inference fail in a closure #63702

Open
Yura52 opened this issue Aug 19, 2019 · 4 comments
Open

Type inference fail in a closure #63702

Yura52 opened this issue Aug 19, 2019 · 4 comments
Labels
A-closures Area: closures (`|args| { .. }`) A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Yura52
Copy link

Yura52 commented Aug 19, 2019

Let's have a look at two similar closures:

fn foo(v: &Vec<i32>) -> i32 {
    let x: usize = 0;
    let condition = |idx| v[idx] > 0;
    if condition(x) {0} else {1}
}

fn bar(v: &Vec<(i32, i32)>) -> i32 {
    let x: usize = 0;
    let condition = |idx| v[idx].0 > 0;
    if condition(x) {0} else {1}
}

The first one is successfully compiled, but the second one requires additional type annotations: playground 4

It seems that there is enough information for fully automatic type inference in both cases. Is it a compilation bug then?

@jonas-schievink jonas-schievink added A-closures Area: closures (`|args| { .. }`) A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 19, 2019
@hellow554
Copy link
Contributor

Same goes for array and slices

@qnighy
Copy link
Contributor

qnighy commented Aug 20, 2019

Dot operators require immediate resolution of outermost non-reference type of the receiver. In this case the compiler has to know that the expression v[idx] is a tuple. However, when the compiler sees the expression for the first time, the type of idx is unknown (it might be Range<usize> for example). So I guess this isn't a bug in a strict sense but there might be a room for improvement in the inference rules.

@Yura52
Copy link
Author

Yura52 commented Aug 20, 2019

Dot operators require immediate resolution of outermost non-reference type of the receiver. In this case the compiler has to know that the expression v[idx] is a tuple. However, when the compiler sees the expression for the first time, the type of idx is unknown (it might be Range<usize> for example). So I guess this isn't a bug in a strict sense but there might be a room for improvement in the inference rules.

Thank you for the explanation. Well, now the question is if the limitation could be less strict :)

@Spoonbender
Copy link

triage: no change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants