From 65e3ff4df44effdfa60971cd7b66e4ce8a4c5bb9 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Thu, 16 Jun 2016 23:15:06 -0700 Subject: [PATCH 1/3] add extended information for E0529, slice pattern expects array or slice --- src/librustc_typeck/diagnostics.rs | 32 +++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 500f624ea3f72..3867ed5fa6868 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -3980,6 +3980,37 @@ impl SpaceLlama for i32 { ``` "##, +E0529: r##" +An array or slice pattern was matched against some other type. + +Example of erroneous code: + +```compile_fail,E0529 +#![feature(slice_patterns)] + +let r: f32 = 1.0; +match r { + [a, b] => { // error: expected an array or slice, found `f32` + println!("a={}, b={}", a, b); + } +} +``` + +Ensure that the pattern and the expression being matched on are of consistent +types: + +``` +#![feature(slice_patterns)] + +let r = [1.0, 2.0]; +match r { + [a, b] => { // ok! + println!("a={}, b={}", a, b); + } +} +``` +"##, + E0559: r##" An unknown field was specified into an enum's structure variant. @@ -4102,6 +4133,5 @@ register_diagnostics! { E0521, // redundant default implementations of trait E0527, // expected {} elements, found {} E0528, // expected at least {} elements, found {} - E0529, // slice pattern expects array or slice, not `{}` E0533, // `{}` does not name a unit variant, unit struct or a constant } From 7093d1de8455556640578ad3d493bc9bff79b8e5 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Thu, 16 Jun 2016 23:45:44 -0700 Subject: [PATCH 2/3] add extended info for E0527, slice pattern element count expectations --- src/librustc_typeck/diagnostics.rs | 34 +++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 3867ed5fa6868..cf8fd3da1f5ba 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -3980,6 +3980,39 @@ impl SpaceLlama for i32 { ``` "##, +E0527: r##" +The number of elements in an array or slice pattern differed from the number of +elements in the array being matched. + +Example of erroneous code: + +```compile_fail,E0527 +#![feature(slice_patterns)] + +let r = &[1, 2, 3, 4]; +match r { + &[a, b] => { // error: pattern requires 2 elements but array + // has 4 + println!("a={}, b={}", a, b); + } +} +``` + +Ensure that the pattern is consistent with the size of the matched +array. Additional elements can be matched with `..`: + +``` +#![feature(slice_patterns)] + +let r = &[1, 2, 3, 4]; +match r { + &[a, b, ..] => { // ok! + println!("a={}, b={}", a, b); + } +} +``` +"##, + E0529: r##" An array or slice pattern was matched against some other type. @@ -4131,7 +4164,6 @@ register_diagnostics! { E0436, // functional record update requires a struct E0513, // no type for local variable .. E0521, // redundant default implementations of trait - E0527, // expected {} elements, found {} E0528, // expected at least {} elements, found {} E0533, // `{}` does not name a unit variant, unit struct or a constant } From e960021d30faff871f98c9c463b43c4359af280d Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Fri, 17 Jun 2016 00:03:36 -0700 Subject: [PATCH 3/3] extended info for E0528, expected at least this-and-such many elements --- src/librustc_typeck/diagnostics.rs | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index cf8fd3da1f5ba..4b0b616c5f648 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4013,6 +4013,40 @@ match r { ``` "##, +E0528: r##" +An array or slice pattern required more elements than were present in the +matched array. + +Example of erroneous code: + +```compile_fail,E0528 +#![feature(slice_patterns)] + +let r = &[1, 2]; +match r { + &[a, b, c, rest..] => { // error: pattern requires at least 3 + // elements but array has 2 + println!("a={}, b={}, c={} rest={:?}", a, b, c, rest); + } +} +``` + +Ensure that the matched array has at least as many elements as the pattern +requires. You can match an arbitrary number of remaining elements with `..`: + +``` +#![feature(slice_patterns)] + +let r = &[1, 2, 3, 4, 5]; +match r { + &[a, b, c, rest..] => { // ok! + // prints `a=1, b=2, c=3 rest=[4, 5]` + println!("a={}, b={}, c={} rest={:?}", a, b, c, rest); + } +} +``` +"##, + E0529: r##" An array or slice pattern was matched against some other type. @@ -4164,6 +4198,5 @@ register_diagnostics! { E0436, // functional record update requires a struct E0513, // no type for local variable .. E0521, // redundant default implementations of trait - E0528, // expected at least {} elements, found {} E0533, // `{}` does not name a unit variant, unit struct or a constant }