Skip to content

Commit af55b41

Browse files
committed
clean up check_pat_enum
1 parent b9e61c9 commit af55b41

File tree

1 file changed

+28
-46
lines changed

1 file changed

+28
-46
lines changed

src/librustc_typeck/check/_match.rs

+28-46
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,12 @@ fn bad_struct_kind_err(sess: &Session, pat: &hir::Pat, path: &hir::Path, lint: b
599599
}
600600
}
601601

602-
pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
603-
pat: &hir::Pat,
604-
path: &hir::Path,
605-
subpats: Option<&'tcx [P<hir::Pat>]>,
606-
expected: Ty<'tcx>,
607-
is_tuple_struct_pat: bool)
602+
fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
603+
pat: &hir::Pat,
604+
path: &hir::Path,
605+
subpats: Option<&'tcx [P<hir::Pat>]>,
606+
expected: Ty<'tcx>,
607+
is_tuple_struct_pat: bool)
608608
{
609609
// Typecheck the path.
610610
let fcx = pcx.fcx;
@@ -687,59 +687,41 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
687687
demand::eqtype(fcx, pat.span, expected, pat_ty);
688688

689689
let real_path_ty = fcx.node_ty(pat.id);
690-
let (arg_tys, kind_name): (Vec<_>, &'static str) = match real_path_ty.sty {
690+
let (kind_name, variant, expected_substs) = match real_path_ty.sty {
691691
ty::TyEnum(enum_def, expected_substs) => {
692692
let variant = enum_def.variant_of_def(def);
693-
if variant.kind() == ty::VariantKind::Struct {
694-
report_bad_struct_kind(false);
695-
return;
696-
}
697-
if is_tuple_struct_pat && variant.kind() != ty::VariantKind::Tuple {
698-
// Matching unit variants with tuple variant patterns (`UnitVariant(..)`)
699-
// is allowed for backward compatibility.
700-
let is_special_case = variant.kind() == ty::VariantKind::Unit;
701-
report_bad_struct_kind(is_special_case);
702-
if !is_special_case {
703-
return
704-
}
705-
}
706-
(variant.fields
707-
.iter()
708-
.map(|f| fcx.instantiate_type_scheme(pat.span,
709-
expected_substs,
710-
&f.unsubst_ty()))
711-
.collect(),
712-
"variant")
693+
("variant", variant, expected_substs)
713694
}
714695
ty::TyStruct(struct_def, expected_substs) => {
715696
let variant = struct_def.struct_variant();
716-
if is_tuple_struct_pat && variant.kind() != ty::VariantKind::Tuple {
717-
// Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
718-
// is allowed for backward compatibility.
719-
let is_special_case = variant.kind() == ty::VariantKind::Unit;
720-
report_bad_struct_kind(is_special_case);
721-
return;
722-
}
723-
(variant.fields
724-
.iter()
725-
.map(|f| fcx.instantiate_type_scheme(pat.span,
726-
expected_substs,
727-
&f.unsubst_ty()))
728-
.collect(),
729-
"struct")
697+
("struct", variant, expected_substs)
730698
}
731699
_ => {
732700
report_bad_struct_kind(false);
733701
return;
734702
}
735703
};
736704

705+
match (is_tuple_struct_pat, variant.kind()) {
706+
(true, ty::VariantKind::Unit) => {
707+
// Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
708+
// is allowed for backward compatibility.
709+
report_bad_struct_kind(true);
710+
}
711+
(_, ty::VariantKind::Struct) => {
712+
report_bad_struct_kind(false);
713+
return
714+
}
715+
_ => {}
716+
}
717+
737718
if let Some(subpats) = subpats {
738-
if subpats.len() == arg_tys.len() {
739-
for (subpat, arg_ty) in subpats.iter().zip(arg_tys) {
740-
check_pat(pcx, &subpat, arg_ty);
719+
if subpats.len() == variant.fields.len() {
720+
for (subpat, field) in subpats.iter().zip(&variant.fields) {
721+
let field_ty = fcx.field_ty(subpat.span, field, expected_substs);
722+
check_pat(pcx, &subpat, field_ty);
741723
}
742-
} else if arg_tys.is_empty() {
724+
} else if variant.fields.is_empty() {
743725
span_err!(tcx.sess, pat.span, E0024,
744726
"this pattern has {} field{}, but the corresponding {} has no fields",
745727
subpats.len(), if subpats.len() == 1 {""} else {"s"}, kind_name);
@@ -752,7 +734,7 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
752734
"this pattern has {} field{}, but the corresponding {} has {} field{}",
753735
subpats.len(), if subpats.len() == 1 {""} else {"s"},
754736
kind_name,
755-
arg_tys.len(), if arg_tys.len() == 1 {""} else {"s"});
737+
variant.fields.len(), if variant.fields.len() == 1 {""} else {"s"});
756738

757739
for pat in subpats {
758740
check_pat(pcx, &pat, tcx.types.err);

0 commit comments

Comments
 (0)