rust-analyzer: Unable to inference a result type of an indexing operation
Hi everyone, I was under impression that type inference involving arrays should work, but it is behaving weirdly:
fn foo(bar: [usize; 2]) -> usize {
let baz = bar[1];
baz
}
fn foo2(bar: [usize; 2]) -> usize {
let baz = bar[1];
return baz;
}
fn foo3(bar: [usize; 2]) {
let baz = bar[1];
println!("{}", baz);
}
fn main() {
let foo4 = |bar: [usize; 2]| -> usize {
let baz = bar[1];
baz
};
let foo5 = |bar: [usize; 2]| -> usize {
let baz = bar[1];
return baz;
};
println!(
"{} {} {} {}",
foo([1, 2]),
foo2([1, 2]),
foo4([1, 2]),
foo5([1, 2])
);
foo3([1, 2]);
}
The type inferred for baz is usize for foo1 and foo2 (good), {unknown} for foo3 and foo4 (I would expect that type inference is not too hard there) and () for foo5 (plain wrong!).
Tested with the latest master (https://github.com/rust-analyzer/rust-analyzer/commit/4444192b05c107a40a5a05ea3c9091ad8f8cbbcc)
Edit: there are two separate issues: type in foo3 and foo4 is not inferred because it is an indexing operation and is inferred incorrectly in foo5 because of the return statement in the closure (#2547 )
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (16 by maintainers)
Commits related to this issue
- Merge #2592 2592: Add std::ops::Index support for infering r=edwin0cheng a=edwin0cheng see also #2534 Seem like this can't fix #2534 for this case: ```rust fn foo3(bar: [usize; 2]) { let... — committed to rust-lang/rust-analyzer by bors[bot] 5 years ago
- Implement Chalk variable kinds This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more ceremony. (It also exposes some places where we'... — committed to flodiebold/rust-analyzer by flodiebold 4 years ago
- Implement Chalk variable kinds This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more ceremony. (It also exposes some places where we'... — committed to flodiebold/rust-analyzer by flodiebold 4 years ago
- Merge #5149 5149: Implement Chalk variable kinds r=flodiebold a=flodiebold This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more cer... — committed to rust-lang/rust-analyzer by bors[bot] 4 years ago
- Implement Chalk variable kinds This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more ceremony. (It also exposes some places where we'... — committed to flodiebold/rust-analyzer by flodiebold 4 years ago
- Merge #5149 5149: Implement Chalk variable kinds r=flodiebold a=flodiebold This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more cer... — committed to rust-lang/rust-analyzer by bors[bot] 4 years ago
- Merge #5319 5319: Chalk upgrade r=flodiebold a=flodiebold - upgrade Chalk - make use of Chalk's `Unsize` impls, remove ours - use Chalk's built-in array type - search efficiently for impls fo... — committed to rust-lang/rust-analyzer by bors[bot] 4 years ago
Autoref and unsizing are done now.
See the linked Chalk issue https://github.com/rust-lang/chalk/issues/327.
We’re not making use of the Chalk support yet.
I see, thank you for linking the related Chalk issue.
It seems not fixed yet.