Skip to content

[ER] A simple case of missed type inference #94519

Not planned
@leonardo-m

Description

@leonardo-m

A type inference problem:

#![feature(generic_arg_infer)]
fn main() {
    let foo = |x| x + x; // OK
    let _a = foo(10_u8);

    let bar = |[a,b,c,d,e,f,g,h,i]: [_; _]| [c,f,i,b,e,h,a,d,g]; // OK
    let _b = bar([0_u8; 9]);

    let spam = |[a,b,c,d,e,f,g,h,i]| [c,f,i,b,e,h,a,d,g]; // error[E0282]: type annotations needed
    let _c = spam([0_u8; 9]);
}

In the second case the added : [_; _] adds very little information, at best it just states it's an array. I think Rustc could perform this little step and accept the "spam" case too.

Activity

scottmcm

scottmcm commented on Mar 5, 2022

@scottmcm
Member

Possible dup of #76342 where it was pointed out that references are also possible: the spam closure could also validly type as &[T; 9] -> [&T; 9]. It's always unclear to me when closures can care about things after themselves, to pick up call here that would disambiguate the rest of the way.

leonardo-m

leonardo-m commented on Mar 7, 2022

@leonardo-m
Author

related:

let test = |[[a,b,c], [d,e,f], [g,h,i]]: [[_; _]; _]| [[g,d,a], [h,e,b], [i,f,c]];
jyn514

jyn514 commented on Apr 26, 2023

@jyn514
Member

I'm going to close this as a duplicate of #76342; I think they have the same cause, that [a,b,c,d,e,f,g,h,i] could be inferred as either an array or a slice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @scottmcm@leonardo-m@jyn514

        Issue actions

          [ER] A simple case of missed type inference · Issue #94519 · rust-lang/rust