Commit fcf3006
committed
Auto merge of rust-lang#113199 - b-naber:slice-pattern-type-inference, r=lcnr
Infer type in irrefutable slice patterns with fixed length as array
Fixes rust-lang#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`File tree
19 files changed
+489
-105
lines changed- compiler/rustc_hir_typeck/src
- fn_ctxt
- tests/ui
- array-slice-vec
- destructuring-assignment
- pattern
19 files changed
+489
-105
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1463 | 1463 | | |
1464 | 1464 | | |
1465 | 1465 | | |
1466 | | - | |
| 1466 | + | |
1467 | 1467 | | |
1468 | 1468 | | |
1469 | 1469 | | |
1470 | | - | |
| 1470 | + | |
1471 | 1471 | | |
1472 | 1472 | | |
1473 | 1473 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
12 | 32 | | |
13 | 33 | | |
14 | 34 | | |
| |||
18 | 38 | | |
19 | 39 | | |
20 | 40 | | |
21 | | - | |
| 41 | + | |
22 | 42 | | |
23 | 43 | | |
24 | 44 | | |
25 | 45 | | |
26 | 46 | | |
27 | | - | |
| 47 | + | |
28 | 48 | | |
29 | 49 | | |
30 | 50 | | |
31 | 51 | | |
32 | 52 | | |
33 | 53 | | |
34 | | - | |
| 54 | + | |
35 | 55 | | |
36 | 56 | | |
37 | 57 | | |
| |||
0 commit comments