Skip to content

Commit 004af40

Browse files
authored
Rollup merge of #34319 - zackmdavis:explain_slice_pattern_errors, r=GuillaumeGomez
extended information for slice pattern errors (E0527 through E0529) r? @GuillaumeGomez
2 parents ea07d52 + e960021 commit 004af40

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3897,6 +3897,104 @@ impl SpaceLlama for i32 {
38973897
```
38983898
"##,
38993899

3900+
E0527: r##"
3901+
The number of elements in an array or slice pattern differed from the number of
3902+
elements in the array being matched.
3903+
3904+
Example of erroneous code:
3905+
3906+
```compile_fail,E0527
3907+
#![feature(slice_patterns)]
3908+
3909+
let r = &[1, 2, 3, 4];
3910+
match r {
3911+
&[a, b] => { // error: pattern requires 2 elements but array
3912+
// has 4
3913+
println!("a={}, b={}", a, b);
3914+
}
3915+
}
3916+
```
3917+
3918+
Ensure that the pattern is consistent with the size of the matched
3919+
array. Additional elements can be matched with `..`:
3920+
3921+
```
3922+
#![feature(slice_patterns)]
3923+
3924+
let r = &[1, 2, 3, 4];
3925+
match r {
3926+
&[a, b, ..] => { // ok!
3927+
println!("a={}, b={}", a, b);
3928+
}
3929+
}
3930+
```
3931+
"##,
3932+
3933+
E0528: r##"
3934+
An array or slice pattern required more elements than were present in the
3935+
matched array.
3936+
3937+
Example of erroneous code:
3938+
3939+
```compile_fail,E0528
3940+
#![feature(slice_patterns)]
3941+
3942+
let r = &[1, 2];
3943+
match r {
3944+
&[a, b, c, rest..] => { // error: pattern requires at least 3
3945+
// elements but array has 2
3946+
println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3947+
}
3948+
}
3949+
```
3950+
3951+
Ensure that the matched array has at least as many elements as the pattern
3952+
requires. You can match an arbitrary number of remaining elements with `..`:
3953+
3954+
```
3955+
#![feature(slice_patterns)]
3956+
3957+
let r = &[1, 2, 3, 4, 5];
3958+
match r {
3959+
&[a, b, c, rest..] => { // ok!
3960+
// prints `a=1, b=2, c=3 rest=[4, 5]`
3961+
println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3962+
}
3963+
}
3964+
```
3965+
"##,
3966+
3967+
E0529: r##"
3968+
An array or slice pattern was matched against some other type.
3969+
3970+
Example of erroneous code:
3971+
3972+
```compile_fail,E0529
3973+
#![feature(slice_patterns)]
3974+
3975+
let r: f32 = 1.0;
3976+
match r {
3977+
[a, b] => { // error: expected an array or slice, found `f32`
3978+
println!("a={}, b={}", a, b);
3979+
}
3980+
}
3981+
```
3982+
3983+
Ensure that the pattern and the expression being matched on are of consistent
3984+
types:
3985+
3986+
```
3987+
#![feature(slice_patterns)]
3988+
3989+
let r = [1.0, 2.0];
3990+
match r {
3991+
[a, b] => { // ok!
3992+
println!("a={}, b={}", a, b);
3993+
}
3994+
}
3995+
```
3996+
"##,
3997+
39003998
E0559: r##"
39013999
An unknown field was specified into an enum's structure variant.
39024000
@@ -4018,8 +4116,5 @@ register_diagnostics! {
40184116
E0436, // functional record update requires a struct
40194117
E0513, // no type for local variable ..
40204118
E0521, // redundant default implementations of trait
4021-
E0527, // expected {} elements, found {}
4022-
E0528, // expected at least {} elements, found {}
4023-
E0529, // slice pattern expects array or slice, not `{}`
40244119
E0533, // `{}` does not name a unit variant, unit struct or a constant
40254120
}

0 commit comments

Comments
 (0)