@@ -203,7 +203,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
203
203
check_pat_enum ( pcx, pat, path, subpats. as_ref ( ) . map ( |v| & v[ ..] ) , expected, true ) ;
204
204
}
205
205
PatKind :: Path ( ref path) => {
206
- check_pat_enum ( pcx, pat, path, None , expected, false ) ;
206
+ check_pat_enum ( pcx, pat, path, Some ( & [ ] ) , expected, false ) ;
207
207
}
208
208
PatKind :: QPath ( ref qself, ref path) => {
209
209
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
597
597
}
598
598
}
599
599
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 )
606
606
{
607
607
// Typecheck the path.
608
608
let fcx = pcx. fcx ;
@@ -685,59 +685,41 @@ pub fn check_pat_enum<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
685
685
demand:: eqtype ( fcx, pat. span , expected, pat_ty) ;
686
686
687
687
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 {
689
689
ty:: TyEnum ( enum_def, expected_substs) => {
690
690
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)
711
692
}
712
693
ty:: TyStruct ( struct_def, expected_substs) => {
713
694
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)
728
696
}
729
697
_ => {
730
698
report_bad_struct_kind ( false ) ;
731
699
return ;
732
700
}
733
701
} ;
734
702
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
+
735
716
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) ;
739
721
}
740
- } else if arg_tys . is_empty ( ) {
722
+ } else if variant . fields . is_empty ( ) {
741
723
span_err ! ( tcx. sess, pat. span, E0024 ,
742
724
"this pattern has {} field{}, but the corresponding {} has no fields" ,
743
725
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>,
750
732
"this pattern has {} field{}, but the corresponding {} has {} field{}" ,
751
733
subpats. len( ) , if subpats. len( ) == 1 { "" } else { "s" } ,
752
734
kind_name,
753
- arg_tys . len( ) , if arg_tys . len( ) == 1 { "" } else { "s" } ) ;
735
+ variant . fields . len( ) , if variant . fields . len( ) == 1 { "" } else { "s" } ) ;
754
736
755
737
for pat in subpats {
756
738
check_pat ( pcx, & pat, tcx. types . err ) ;
0 commit comments