From 24d0a01b75c034d52bdca10cca08e69538e871ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 4 Sep 2019 23:59:04 -0700 Subject: [PATCH] review comment --- src/librustc_typeck/check/pat.rs | 38 +++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs index 9a3f5e9e1b5d8..24d0659391b04 100644 --- a/src/librustc_typeck/check/pat.rs +++ b/src/librustc_typeck/check/pat.rs @@ -675,23 +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" }; - let span = tcx.def_span(res.def_id()); - 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())) - .span_label(span, format!("{} defined here", res.descr())) - .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], 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,