Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better slices array type inference #74626

Open
leonardo-m opened this issue Jul 22, 2020 · 2 comments
Open

Better slices array type inference #74626

leonardo-m opened this issue Jul 22, 2020 · 2 comments
Labels
A-inference Area: Type inference C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

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?

@Alexendoo
Copy link
Member

Much like giving the hint of _u32 to the first element, you can do

    let foos2 = [&[1_u32, 2, 3][..], &[1, 2], &[]];

@leonardo-m
Copy link
Author

Thank you, your code compiles, and I'll use it. But I think my original ER issue is still valid.

@Alexendoo Alexendoo added A-inference Area: Type inference C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 22, 2020
@dtolnay dtolnay changed the title [ER] Better slices array type inference Better slices array type inference Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants