Skip to content

Commit

Permalink
Auto merge of #113199 - b-naber:slice-pattern-type-inference, r=lcnr
Browse files Browse the repository at this point in the history
Infer type in irrefutable slice patterns with fixed length as array

Fixes rust-lang/rust#76342

In irrefutable slice patterns with a fixed length, we can infer the type as an array type. We now choose to prefer some implementations over others, e.g. in:

```
struct Zeroes;

const ARR: [usize; 2] = [0; 2];
const ARR2: [usize; 2] = [2; 2];

impl Into<&'static [usize; 2]> for Zeroes {
    fn into(self) -> &'static [usize; 2] {
        &ARR
    }
}

impl Into<&'static [usize]> for Zeroes {
    fn into(self) -> &'static [usize] {
        &ARR2
    }
}

fn main() {
    let &[a, b] = Zeroes.into();
}
```

We now prefer the impl candidate `impl Into<&'static [usize; 2]> for Zeroes`, it's not entirely clear to me that this is correct, but given that the slice impl would require a type annotation anyway, this doesn't seem unreasonable.

r? `@lcnr`
  • Loading branch information
bors committed Aug 3, 2023
2 parents f115af9 + 305f652 commit 4b6d53e
Showing 0 changed files with 0 additions and 0 deletions.

0 comments on commit 4b6d53e

Please sign in to comment.