diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-09-06 09:36:42 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-06 09:36:42 +0200 |
| commit | 6968e53a5c4e6d883ae4683d76bb5a8ca444aedb (patch) | |
| tree | 1fb8b3260deeb9e108b9401d4d6cc23388f609e2 | |
| parent | 37a022ee43c95e815c458844b223ad73c11715d8 (diff) | |
| parent | 24d0a01b75c034d52bdca10cca08e69538e871ca (diff) | |
| download | rust-6968e53a5c4e6d883ae4683d76bb5a8ca444aedb.tar.gz rust-6968e53a5c4e6d883ae4683d76bb5a8ca444aedb.zip | |
Rollup merge of #64161 - estebank:point-variant, r=Centril
Point at variant on pattern field count mismatch
| -rw-r--r-- | src/librustc_typeck/check/pat.rs | 36 | ||||
| -rw-r--r-- | src/test/ui/error-codes/E0023.stderr | 9 | ||||
| -rw-r--r-- | src/test/ui/match/match-pattern-field-mismatch.stderr | 3 | ||||
| -rw-r--r-- | src/test/ui/pattern/pat-tuple-overfield.stderr | 6 | ||||
| -rw-r--r-- | src/test/ui/pattern/pattern-error-continue.stderr | 3 |
5 files changed, 48 insertions, 9 deletions
diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs index 4cf0df308fb..24d0659391b 100644 --- a/src/librustc_typeck/check/pat.rs +++ b/src/librustc_typeck/check/pat.rs @@ -675,21 +675,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.tcx.check_stability(variant.fields[i].did, Some(pat.hir_id), subpat.span); } } else { - let subpats_ending = if subpats.len() == 1 { "" } else { "s" }; - let fields_ending = if variant.fields.len() == 1 { "" } else { "s" }; - struct_span_err!(tcx.sess, pat.span, E0023, - "this pattern has {} field{}, but the corresponding {} has {} field{}", - subpats.len(), subpats_ending, res.descr(), - variant.fields.len(), fields_ending) - .span_label(pat.span, format!("expected {} field{}, found {}", - variant.fields.len(), fields_ending, subpats.len())) - .emit(); + // Pattern has wrong number of fields. + self.e0023(pat.span, res, &subpats, &variant.fields); on_error(); return tcx.types.err; } pat_ty } + fn e0023(&self, pat_span: Span, res: Res, subpats: &'tcx [P<Pat>], fields: &[ty::FieldDef]) { + let subpats_ending = if subpats.len() == 1 { "" } else { "s" }; + let fields_ending = if fields.len() == 1 { "" } else { "s" }; + let res_span = self.tcx.def_span(res.def_id()); + struct_span_err!( + self.tcx.sess, + pat_span, + E0023, + "this pattern has {} field{}, but the corresponding {} has {} field{}", + subpats.len(), + subpats_ending, + res.descr(), + fields.len(), + fields_ending, + ) + .span_label(pat_span, format!( + "expected {} field{}, found {}", + fields.len(), + fields_ending, + subpats.len(), + )) + .span_label(res_span, format!("{} defined here", res.descr())) + .emit(); + } + fn check_pat_tuple( &self, span: Span, diff --git a/src/test/ui/error-codes/E0023.stderr b/src/test/ui/error-codes/E0023.stderr index 1bc90a995fe..d04e494c258 100644 --- a/src/test/ui/error-codes/E0023.stderr +++ b/src/test/ui/error-codes/E0023.stderr @@ -1,18 +1,27 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields --> $DIR/E0023.rs:10:9 | +LL | Apple(String, String), + | --------------------- tuple variant defined here +... LL | Fruit::Apple(a) => {}, | ^^^^^^^^^^^^^^^ expected 2 fields, found 1 error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields --> $DIR/E0023.rs:11:9 | +LL | Apple(String, String), + | --------------------- tuple variant defined here +... LL | Fruit::Apple(a, b, c) => {}, | ^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3 error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field --> $DIR/E0023.rs:12:9 | +LL | Pear(u32), + | --------- tuple variant defined here +... LL | Fruit::Pear(1, 2) => {}, | ^^^^^^^^^^^^^^^^^ expected 1 field, found 2 diff --git a/src/test/ui/match/match-pattern-field-mismatch.stderr b/src/test/ui/match/match-pattern-field-mismatch.stderr index 663cd2cd24d..c2298d6fbbf 100644 --- a/src/test/ui/match/match-pattern-field-mismatch.stderr +++ b/src/test/ui/match/match-pattern-field-mismatch.stderr @@ -1,6 +1,9 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields --> $DIR/match-pattern-field-mismatch.rs:10:11 | +LL | Rgb(usize, usize, usize), + | ------------------------ tuple variant defined here +... LL | Color::Rgb(_, _) => { } | ^^^^^^^^^^^^^^^^ expected 3 fields, found 2 diff --git a/src/test/ui/pattern/pat-tuple-overfield.stderr b/src/test/ui/pattern/pat-tuple-overfield.stderr index 0430897510b..e64b6efb08d 100644 --- a/src/test/ui/pattern/pat-tuple-overfield.stderr +++ b/src/test/ui/pattern/pat-tuple-overfield.stderr @@ -19,12 +19,18 @@ LL | (1, 2, .., 3, 4) => {} error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields --> $DIR/pat-tuple-overfield.rs:10:9 | +LL | struct S(u8, u8, u8); + | --------------------- tuple struct defined here +... LL | S(1, 2, 3, 4) => {} | ^^^^^^^^^^^^^ expected 3 fields, found 4 error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields --> $DIR/pat-tuple-overfield.rs:12:9 | +LL | struct S(u8, u8, u8); + | --------------------- tuple struct defined here +... LL | S(1, 2, .., 3, 4) => {} | ^^^^^^^^^^^^^^^^^ expected 3 fields, found 4 diff --git a/src/test/ui/pattern/pattern-error-continue.stderr b/src/test/ui/pattern/pattern-error-continue.stderr index a581f07496e..4fbc630644b 100644 --- a/src/test/ui/pattern/pattern-error-continue.stderr +++ b/src/test/ui/pattern/pattern-error-continue.stderr @@ -15,6 +15,9 @@ LL | A::D(_) => (), error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields --> $DIR/pattern-error-continue.rs:17:9 | +LL | B(isize, isize), + | --------------- tuple variant defined here +... LL | A::B(_, _, _) => (), | ^^^^^^^^^^^^^ expected 2 fields, found 3 |
