@@ -204,7 +204,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
204
204
check_pat_enum ( pcx, pat, path, subpats. as_ref ( ) . map ( |v| & v[ ..] ) , expected, true ) ;
205
205
}
206
206
PatKind :: Path ( ref path) => {
207
- check_pat_enum ( pcx, pat, path, None , expected, false ) ;
207
+ check_pat_enum ( pcx, pat, path, Some ( & [ ] ) , expected, false ) ;
208
208
}
209
209
PatKind :: QPath ( ref qself, ref path) => {
210
210
let self_ty = fcx. to_ty ( & qself. ty ) ;
@@ -599,12 +599,12 @@ fn bad_struct_kind_err(sess: &Session, pat: &hir::Pat, path: &hir::Path, lint: b
599
599
}
600
600
}
601
601
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 )
608
608
{
609
609
// Typecheck the path.
610
610
let fcx = pcx. fcx ;
@@ -687,59 +687,41 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
687
687
demand:: eqtype ( fcx, pat. span , expected, pat_ty) ;
688
688
689
689
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 {
691
691
ty:: TyEnum ( enum_def, expected_substs) => {
692
692
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)
713
694
}
714
695
ty:: TyStruct ( struct_def, expected_substs) => {
715
696
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)
730
698
}
731
699
_ => {
732
700
report_bad_struct_kind ( false ) ;
733
701
return ;
734
702
}
735
703
} ;
736
704
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
+
737
718
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) ;
741
723
}
742
- } else if arg_tys . is_empty ( ) {
724
+ } else if variant . fields . is_empty ( ) {
743
725
span_err ! ( tcx. sess, pat. span, E0024 ,
744
726
"this pattern has {} field{}, but the corresponding {} has no fields" ,
745
727
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>,
752
734
"this pattern has {} field{}, but the corresponding {} has {} field{}" ,
753
735
subpats. len( ) , if subpats. len( ) == 1 { "" } else { "s" } ,
754
736
kind_name,
755
- arg_tys . len( ) , if arg_tys . len( ) == 1 { "" } else { "s" } ) ;
737
+ variant . fields . len( ) , if variant . fields . len( ) == 1 { "" } else { "s" } ) ;
756
738
757
739
for pat in subpats {
758
740
check_pat ( pcx, & pat, tcx. types . err ) ;
0 commit comments