@@ -524,90 +524,10 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
524
524
}
525
525
526
526
pub ( crate ) fn cat_pattern < F > (
527
- & self ,
528
- place : PlaceWithHirId < ' tcx > ,
529
- pat : & hir:: Pat < ' _ > ,
530
- mut op : F ,
531
- ) -> McResult < ( ) >
532
- where
533
- F : FnMut ( & PlaceWithHirId < ' tcx > , & hir:: Pat < ' _ > ) ,
534
- {
535
- self . cat_pattern_ ( place, pat, & mut op)
536
- }
537
-
538
- /// Returns the variant index for an ADT used within a Struct or TupleStruct pattern
539
- /// Here `pat_hir_id` is the HirId of the pattern itself.
540
- fn variant_index_for_adt (
541
- & self ,
542
- qpath : & hir:: QPath < ' _ > ,
543
- pat_hir_id : hir:: HirId ,
544
- span : Span ,
545
- ) -> McResult < VariantIdx > {
546
- let res = self . typeck_results . qpath_res ( qpath, pat_hir_id) ;
547
- let ty = self . typeck_results . node_type ( pat_hir_id) ;
548
- let ty:: Adt ( adt_def, _) = ty. kind ( ) else {
549
- self . tcx ( )
550
- . sess
551
- . delay_span_bug ( span, "struct or tuple struct pattern not applied to an ADT" ) ;
552
- return Err ( ( ) ) ;
553
- } ;
554
-
555
- match res {
556
- Res :: Def ( DefKind :: Variant , variant_id) => Ok ( adt_def. variant_index_with_id ( variant_id) ) ,
557
- Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , ..) , variant_ctor_id) => {
558
- Ok ( adt_def. variant_index_with_ctor_id ( variant_ctor_id) )
559
- }
560
- Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , ..) , _)
561
- | Res :: Def ( DefKind :: Struct | DefKind :: Union | DefKind :: TyAlias | DefKind :: AssocTy , _)
562
- | Res :: SelfCtor ( ..)
563
- | Res :: SelfTyParam { .. }
564
- | Res :: SelfTyAlias { .. } => {
565
- // Structs and Unions have only have one variant.
566
- Ok ( VariantIdx :: new ( 0 ) )
567
- }
568
- _ => bug ! ( "expected ADT path, found={:?}" , res) ,
569
- }
570
- }
571
-
572
- /// Returns the total number of fields in an ADT variant used within a pattern.
573
- /// Here `pat_hir_id` is the HirId of the pattern itself.
574
- fn total_fields_in_adt_variant (
575
- & self ,
576
- pat_hir_id : hir:: HirId ,
577
- variant_index : VariantIdx ,
578
- span : Span ,
579
- ) -> McResult < usize > {
580
- let ty = self . typeck_results . node_type ( pat_hir_id) ;
581
- match ty. kind ( ) {
582
- ty:: Adt ( adt_def, _) => Ok ( adt_def. variant ( variant_index) . fields . len ( ) ) ,
583
- _ => {
584
- self . tcx ( )
585
- . sess
586
- . delay_span_bug ( span, "struct or tuple struct pattern not applied to an ADT" ) ;
587
- Err ( ( ) )
588
- }
589
- }
590
- }
591
-
592
- /// Returns the total number of fields in a tuple used within a Tuple pattern.
593
- /// Here `pat_hir_id` is the HirId of the pattern itself.
594
- fn total_fields_in_tuple ( & self , pat_hir_id : hir:: HirId , span : Span ) -> McResult < usize > {
595
- let ty = self . typeck_results . node_type ( pat_hir_id) ;
596
- match ty. kind ( ) {
597
- ty:: Tuple ( substs) => Ok ( substs. len ( ) ) ,
598
- _ => {
599
- self . tcx ( ) . sess . delay_span_bug ( span, "tuple pattern not applied to a tuple" ) ;
600
- Err ( ( ) )
601
- }
602
- }
603
- }
604
-
605
- // FIXME(#19596) This is a workaround, but there should be a better way to do this
606
- fn cat_pattern_ < F > (
607
527
& self ,
608
528
mut place_with_id : PlaceWithHirId < ' tcx > ,
609
529
pat : & hir:: Pat < ' _ > ,
610
- op : & mut F ,
530
+ mut op : F ,
611
531
) -> McResult < ( ) >
612
532
where
613
533
F : FnMut ( & PlaceWithHirId < ' tcx > , & hir:: Pat < ' _ > ) ,
@@ -681,7 +601,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
681
601
let projection_kind = ProjectionKind :: Field ( i as u32 , VariantIdx :: new ( 0 ) ) ;
682
602
let sub_place =
683
603
self . cat_projection ( pat, place_with_id. clone ( ) , subpat_ty, projection_kind) ;
684
- self . cat_pattern_ ( sub_place, subpat, op) ?;
604
+ self . cat_pattern ( sub_place, subpat, & mut op) ?;
685
605
}
686
606
}
687
607
@@ -696,7 +616,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
696
616
let projection_kind = ProjectionKind :: Field ( i as u32 , variant_index) ;
697
617
let sub_place =
698
618
self . cat_projection ( pat, place_with_id. clone ( ) , subpat_ty, projection_kind) ;
699
- self . cat_pattern_ ( sub_place, subpat, op) ?;
619
+ self . cat_pattern ( sub_place, subpat, & mut op) ?;
700
620
}
701
621
}
702
622
@@ -720,26 +640,26 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
720
640
field_ty,
721
641
ProjectionKind :: Field ( field_index as u32 , variant_index) ,
722
642
) ;
723
- self . cat_pattern_ ( field_place, fp. pat , op) ?;
643
+ self . cat_pattern ( field_place, fp. pat , & mut op) ?;
724
644
}
725
645
}
726
646
727
647
PatKind :: Or ( pats) => {
728
648
for pat in pats {
729
- self . cat_pattern_ ( place_with_id. clone ( ) , pat, op) ?;
649
+ self . cat_pattern ( place_with_id. clone ( ) , pat, & mut op) ?;
730
650
}
731
651
}
732
652
733
653
PatKind :: Binding ( .., Some ( ref subpat) ) => {
734
- self . cat_pattern_ ( place_with_id, subpat, op) ?;
654
+ self . cat_pattern ( place_with_id, subpat, op) ?;
735
655
}
736
656
737
657
PatKind :: Box ( ref subpat) | PatKind :: Ref ( ref subpat, _) => {
738
658
// box p1, &p1, &mut p1. we can ignore the mutability of
739
659
// PatKind::Ref since that information is already contained
740
660
// in the type.
741
661
let subplace = self . cat_deref ( pat, place_with_id) ?;
742
- self . cat_pattern_ ( subplace, subpat, op) ?;
662
+ self . cat_pattern ( subplace, subpat, op) ?;
743
663
}
744
664
745
665
PatKind :: Slice ( before, ref slice, after) => {
@@ -754,7 +674,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
754
674
ProjectionKind :: Index ,
755
675
) ;
756
676
for before_pat in before {
757
- self . cat_pattern_ ( elt_place. clone ( ) , before_pat, op) ?;
677
+ self . cat_pattern ( elt_place. clone ( ) , before_pat, & mut op) ?;
758
678
}
759
679
if let Some ( ref slice_pat) = * slice {
760
680
let slice_pat_ty = self . pat_ty_adjusted ( slice_pat) ?;
@@ -764,10 +684,10 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
764
684
slice_pat_ty,
765
685
ProjectionKind :: Subslice ,
766
686
) ;
767
- self . cat_pattern_ ( slice_place, slice_pat, op) ?;
687
+ self . cat_pattern ( slice_place, slice_pat, & mut op) ?;
768
688
}
769
689
for after_pat in after {
770
- self . cat_pattern_ ( elt_place. clone ( ) , after_pat, op) ?;
690
+ self . cat_pattern ( elt_place. clone ( ) , after_pat, & mut op) ?;
771
691
}
772
692
}
773
693
@@ -782,4 +702,71 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
782
702
783
703
Ok ( ( ) )
784
704
}
705
+
706
+ /// Returns the variant index for an ADT used within a Struct or TupleStruct pattern
707
+ /// Here `pat_hir_id` is the HirId of the pattern itself.
708
+ fn variant_index_for_adt (
709
+ & self ,
710
+ qpath : & hir:: QPath < ' _ > ,
711
+ pat_hir_id : hir:: HirId ,
712
+ span : Span ,
713
+ ) -> McResult < VariantIdx > {
714
+ let res = self . typeck_results . qpath_res ( qpath, pat_hir_id) ;
715
+ let ty = self . typeck_results . node_type ( pat_hir_id) ;
716
+ let ty:: Adt ( adt_def, _) = ty. kind ( ) else {
717
+ self . tcx ( )
718
+ . sess
719
+ . delay_span_bug ( span, "struct or tuple struct pattern not applied to an ADT" ) ;
720
+ return Err ( ( ) ) ;
721
+ } ;
722
+
723
+ match res {
724
+ Res :: Def ( DefKind :: Variant , variant_id) => Ok ( adt_def. variant_index_with_id ( variant_id) ) ,
725
+ Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , ..) , variant_ctor_id) => {
726
+ Ok ( adt_def. variant_index_with_ctor_id ( variant_ctor_id) )
727
+ }
728
+ Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , ..) , _)
729
+ | Res :: Def ( DefKind :: Struct | DefKind :: Union | DefKind :: TyAlias | DefKind :: AssocTy , _)
730
+ | Res :: SelfCtor ( ..)
731
+ | Res :: SelfTyParam { .. }
732
+ | Res :: SelfTyAlias { .. } => {
733
+ // Structs and Unions have only have one variant.
734
+ Ok ( VariantIdx :: new ( 0 ) )
735
+ }
736
+ _ => bug ! ( "expected ADT path, found={:?}" , res) ,
737
+ }
738
+ }
739
+
740
+ /// Returns the total number of fields in an ADT variant used within a pattern.
741
+ /// Here `pat_hir_id` is the HirId of the pattern itself.
742
+ fn total_fields_in_adt_variant (
743
+ & self ,
744
+ pat_hir_id : hir:: HirId ,
745
+ variant_index : VariantIdx ,
746
+ span : Span ,
747
+ ) -> McResult < usize > {
748
+ let ty = self . typeck_results . node_type ( pat_hir_id) ;
749
+ match ty. kind ( ) {
750
+ ty:: Adt ( adt_def, _) => Ok ( adt_def. variant ( variant_index) . fields . len ( ) ) ,
751
+ _ => {
752
+ self . tcx ( )
753
+ . sess
754
+ . delay_span_bug ( span, "struct or tuple struct pattern not applied to an ADT" ) ;
755
+ Err ( ( ) )
756
+ }
757
+ }
758
+ }
759
+
760
+ /// Returns the total number of fields in a tuple used within a Tuple pattern.
761
+ /// Here `pat_hir_id` is the HirId of the pattern itself.
762
+ fn total_fields_in_tuple ( & self , pat_hir_id : hir:: HirId , span : Span ) -> McResult < usize > {
763
+ let ty = self . typeck_results . node_type ( pat_hir_id) ;
764
+ match ty. kind ( ) {
765
+ ty:: Tuple ( substs) => Ok ( substs. len ( ) ) ,
766
+ _ => {
767
+ self . tcx ( ) . sess . delay_span_bug ( span, "tuple pattern not applied to a tuple" ) ;
768
+ Err ( ( ) )
769
+ }
770
+ }
771
+ }
785
772
}
0 commit comments