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

Most upvoted comments

Autoref and unsizing are done now.

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.