@@ -203,7 +203,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
203203 check_pat_enum ( pcx, pat, path, subpats. as_ref ( ) . map ( |v| & v[ ..] ) , expected, true ) ;
204204 }
205205 PatKind :: Path ( ref path) => {
206- check_pat_enum ( pcx, pat, path, None , expected, false ) ;
206+ check_pat_enum ( pcx, pat, path, Some ( & [ ] ) , expected, false ) ;
207207 }
208208 PatKind :: QPath ( ref qself, ref path) => {
209209 let self_ty = fcx. to_ty ( & qself. ty ) ;
@@ -597,12 +597,12 @@ fn bad_struct_kind_err(sess: &Session, pat: &hir::Pat, path: &hir::Path, lint: b
597597 }
598598}
599599
600- pub fn check_pat_enum < ' a , ' tcx > ( pcx : & pat_ctxt < ' a , ' tcx > ,
601- pat : & hir:: Pat ,
602- path : & hir:: Path ,
603- subpats : Option < & ' tcx [ P < hir:: Pat > ] > ,
604- expected : Ty < ' tcx > ,
605- is_tuple_struct_pat : bool )
600+ fn check_pat_enum < ' a , ' tcx > ( pcx : & pat_ctxt < ' a , ' tcx > ,
601+ pat : & hir:: Pat ,
602+ path : & hir:: Path ,
603+ subpats : Option < & ' tcx [ P < hir:: Pat > ] > ,
604+ expected : Ty < ' tcx > ,
605+ is_tuple_struct_pat : bool )
606606{
607607 // Typecheck the path.
608608 let fcx = pcx. fcx ;
@@ -685,59 +685,41 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
685685 demand:: eqtype ( fcx, pat. span , expected, pat_ty) ;
686686
687687 let real_path_ty = fcx. node_ty ( pat. id ) ;
688- let ( arg_tys , kind_name ) : ( Vec < _ > , & ' static str ) = match real_path_ty. sty {
688+ let ( kind_name , variant , expected_substs ) = match real_path_ty. sty {
689689 ty:: TyEnum ( enum_def, expected_substs) => {
690690 let variant = enum_def. variant_of_def ( def) ;
691- if variant. kind ( ) == ty:: VariantKind :: Struct {
692- report_bad_struct_kind ( false ) ;
693- return ;
694- }
695- if is_tuple_struct_pat && variant. kind ( ) != ty:: VariantKind :: Tuple {
696- // Matching unit variants with tuple variant patterns (`UnitVariant(..)`)
697- // is allowed for backward compatibility.
698- let is_special_case = variant. kind ( ) == ty:: VariantKind :: Unit ;
699- report_bad_struct_kind ( is_special_case) ;
700- if !is_special_case {
701- return
702- }
703- }
704- ( variant. fields
705- . iter ( )
706- . map ( |f| fcx. instantiate_type_scheme ( pat. span ,
707- expected_substs,
708- & f. unsubst_ty ( ) ) )
709- . collect ( ) ,
710- "variant" )
691+ ( "variant" , variant, expected_substs)
711692 }
712693 ty:: TyStruct ( struct_def, expected_substs) => {
713694 let variant = struct_def. struct_variant ( ) ;
714- if is_tuple_struct_pat && variant. kind ( ) != ty:: VariantKind :: Tuple {
715- // Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
716- // is allowed for backward compatibility.
717- let is_special_case = variant. kind ( ) == ty:: VariantKind :: Unit ;
718- report_bad_struct_kind ( is_special_case) ;
719- return ;
720- }
721- ( variant. fields
722- . iter ( )
723- . map ( |f| fcx. instantiate_type_scheme ( pat. span ,
724- expected_substs,
725- & f. unsubst_ty ( ) ) )
726- . collect ( ) ,
727- "struct" )
695+ ( "struct" , variant, expected_substs)
728696 }
729697 _ => {
730698 report_bad_struct_kind ( false ) ;
731699 return ;
732700 }
733701 } ;
734702
703+ match ( is_tuple_struct_pat, variant. kind ( ) ) {
704+ ( true , ty:: VariantKind :: Unit ) => {
705+ // Matching unit structs with tuple variant patterns (`UnitVariant(..)`)
706+ // is allowed for backward compatibility.
707+ report_bad_struct_kind ( true ) ;
708+ }
709+ ( _, ty:: VariantKind :: Struct ) => {
710+ report_bad_struct_kind ( false ) ;
711+ return
712+ }
713+ _ => { }
714+ }
715+
735716 if let Some ( subpats) = subpats {
736- if subpats. len ( ) == arg_tys. len ( ) {
737- for ( subpat, arg_ty) in subpats. iter ( ) . zip ( arg_tys) {
738- check_pat ( pcx, & subpat, arg_ty) ;
717+ if subpats. len ( ) == variant. fields . len ( ) {
718+ for ( subpat, field) in subpats. iter ( ) . zip ( & variant. fields ) {
719+ let field_ty = fcx. field_ty ( subpat. span , field, expected_substs) ;
720+ check_pat ( pcx, & subpat, field_ty) ;
739721 }
740- } else if arg_tys . is_empty ( ) {
722+ } else if variant . fields . is_empty ( ) {
741723 span_err ! ( tcx. sess, pat. span, E0024 ,
742724 "this pattern has {} field{}, but the corresponding {} has no fields" ,
743725 subpats. len( ) , if subpats. len( ) == 1 { "" } else { "s" } , kind_name) ;
@@ -750,7 +732,7 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
750732 "this pattern has {} field{}, but the corresponding {} has {} field{}" ,
751733 subpats. len( ) , if subpats. len( ) == 1 { "" } else { "s" } ,
752734 kind_name,
753- arg_tys . len( ) , if arg_tys . len( ) == 1 { "" } else { "s" } ) ;
735+ variant . fields . len( ) , if variant . fields . len( ) == 1 { "" } else { "s" } ) ;
754736
755737 for pat in subpats {
756738 check_pat ( pcx, & pat, tcx. types . err ) ;
0 commit comments