Skip to content

Better slices array type inference #74626

@leonardo-m

Description

@leonardo-m

This code:

#[allow(unused_variables)]
fn main() {
    let foos1: [&[u32]; 3] = [&[1_u32, 2, 3], &[1, 2], &[]];
    let foos2 = [&[1_u32, 2, 3], &[1, 2], &[]];
}

Currently (rustc 1.47.0-nightly 8ad7bc3 2020-07-21) this gives:

error[E0308]: mismatched types
 --> ...\test.rs:4:34
  |
4 |     let foos2 = [&[1_u32, 2, 3], &[1, 2], &[]];
  |                                  ^^^^^^^ expected an array with a fixed size of 3 elements, found one with 2 elements
  |
  = note:   expected type `&[u32; 3]`
          found reference `&[u32; 2]`

Is it possible to improve the type system so foo2 too compiles?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions