Skip to content

Commit 31250ce

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#34319 - zackmdavis:explain_slice_pattern_errors, r=GuillaumeGomez
extended information for slice pattern errors (E0527 through E0529) r? @GuillaumeGomez
2 parents e804a3c + e960021 commit 31250ce

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

src/librustc_typeck/diagnostics.rs

+98-3
Original file line numberDiff line numberDiff line change
@@ -3864,6 +3864,104 @@ impl SpaceLlama for i32 {
38643864
```
38653865
"##,
38663866

3867+
E0527: r##"
3868+
The number of elements in an array or slice pattern differed from the number of
3869+
elements in the array being matched.
3870+
3871+
Example of erroneous code:
3872+
3873+
```compile_fail,E0527
3874+
#![feature(slice_patterns)]
3875+
3876+
let r = &[1, 2, 3, 4];
3877+
match r {
3878+
&[a, b] => { // error: pattern requires 2 elements but array
3879+
// has 4
3880+
println!("a={}, b={}", a, b);
3881+
}
3882+
}
3883+
```
3884+
3885+
Ensure that the pattern is consistent with the size of the matched
3886+
array. Additional elements can be matched with `..`:
3887+
3888+
```
3889+
#![feature(slice_patterns)]
3890+
3891+
let r = &[1, 2, 3, 4];
3892+
match r {
3893+
&[a, b, ..] => { // ok!
3894+
println!("a={}, b={}", a, b);
3895+
}
3896+
}
3897+
```
3898+
"##,
3899+
3900+
E0528: r##"
3901+
An array or slice pattern required more elements than were present in the
3902+
matched array.
3903+
3904+
Example of erroneous code:
3905+
3906+
```compile_fail,E0528
3907+
#![feature(slice_patterns)]
3908+
3909+
let r = &[1, 2];
3910+
match r {
3911+
&[a, b, c, rest..] => { // error: pattern requires at least 3
3912+
// elements but array has 2
3913+
println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3914+
}
3915+
}
3916+
```
3917+
3918+
Ensure that the matched array has at least as many elements as the pattern
3919+
requires. You can match an arbitrary number of remaining elements with `..`:
3920+
3921+
```
3922+
#![feature(slice_patterns)]
3923+
3924+
let r = &[1, 2, 3, 4, 5];
3925+
match r {
3926+
&[a, b, c, rest..] => { // ok!
3927+
// prints `a=1, b=2, c=3 rest=[4, 5]`
3928+
println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3929+
}
3930+
}
3931+
```
3932+
"##,
3933+
3934+
E0529: r##"
3935+
An array or slice pattern was matched against some other type.
3936+
3937+
Example of erroneous code:
3938+
3939+
```compile_fail,E0529
3940+
#![feature(slice_patterns)]
3941+
3942+
let r: f32 = 1.0;
3943+
match r {
3944+
[a, b] => { // error: expected an array or slice, found `f32`
3945+
println!("a={}, b={}", a, b);
3946+
}
3947+
}
3948+
```
3949+
3950+
Ensure that the pattern and the expression being matched on are of consistent
3951+
types:
3952+
3953+
```
3954+
#![feature(slice_patterns)]
3955+
3956+
let r = [1.0, 2.0];
3957+
match r {
3958+
[a, b] => { // ok!
3959+
println!("a={}, b={}", a, b);
3960+
}
3961+
}
3962+
```
3963+
"##,
3964+
38673965
E0559: r##"
38683966
An unknown field was specified into an enum's structure variant.
38693967
@@ -3985,8 +4083,5 @@ register_diagnostics! {
39854083
E0436, // functional record update requires a struct
39864084
E0513, // no type for local variable ..
39874085
E0521, // redundant default implementations of trait
3988-
E0527, // expected {} elements, found {}
3989-
E0528, // expected at least {} elements, found {}
3990-
E0529, // slice pattern expects array or slice, not `{}`
39914086
E0533, // `{}` does not name a unit variant, unit struct or a constant
39924087
}

0 commit comments

Comments
 (0)