Skip to content

Unable to inference a result type of an indexing operation #2534

@ztlpn

Description

@ztlpn

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 (4444192)

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 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions