@@ -18,6 +18,8 @@ pub(crate) use self::check_match::check_match;
18
18
19
19
use const_eval:: { const_field, const_variant_index} ;
20
20
21
+ use hair:: util:: UserAnnotatedTyHelpers ;
22
+
21
23
use rustc:: mir:: { fmt_const_val, Field , BorrowKind , Mutability } ;
22
24
use rustc:: mir:: interpret:: { Scalar , GlobalId , ConstValue , sign_extend} ;
23
25
use rustc:: ty:: { self , CanonicalTy , TyCtxt , AdtDef , Ty , Region } ;
@@ -529,8 +531,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
529
531
field : Field :: new ( i) ,
530
532
pattern : self . lower_pattern ( field) ,
531
533
} )
532
- . collect ( ) ;
533
- self . lower_variant_or_leaf ( def, pat. span , ty, subpatterns)
534
+ . collect ( ) ;
535
+
536
+ self . lower_variant_or_leaf ( def, pat. hir_id , pat. span , ty, subpatterns)
534
537
}
535
538
536
539
PatKind :: Struct ( ref qpath, ref fields, _) => {
@@ -546,7 +549,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
546
549
} )
547
550
. collect ( ) ;
548
551
549
- self . lower_variant_or_leaf ( def, pat. span , ty, subpatterns)
552
+ self . lower_variant_or_leaf ( def, pat. hir_id , pat . span , ty, subpatterns)
550
553
}
551
554
} ;
552
555
@@ -637,12 +640,12 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
637
640
fn lower_variant_or_leaf (
638
641
& mut self ,
639
642
def : Def ,
643
+ hir_id : hir:: HirId ,
640
644
span : Span ,
641
645
ty : Ty < ' tcx > ,
642
- subpatterns : Vec < FieldPattern < ' tcx > > )
643
- -> PatternKind < ' tcx >
644
- {
645
- match def {
646
+ subpatterns : Vec < FieldPattern < ' tcx > > ,
647
+ ) -> PatternKind < ' tcx > {
648
+ let mut kind = match def {
646
649
Def :: Variant ( variant_id) | Def :: VariantCtor ( variant_id, ..) => {
647
650
let enum_id = self . tcx . parent_def_id ( variant_id) . unwrap ( ) ;
648
651
let adt_def = self . tcx . adt_def ( enum_id) ;
@@ -675,7 +678,24 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
675
678
self . errors . push ( PatternError :: NonConstPath ( span) ) ;
676
679
PatternKind :: Wild
677
680
}
681
+ } ;
682
+
683
+ if let Some ( user_ty) = self . user_substs_applied_to_ty_of_hir_id ( hir_id) {
684
+ let subpattern = Pattern {
685
+ span,
686
+ ty,
687
+ kind : Box :: new ( kind) ,
688
+ } ;
689
+
690
+ debug ! ( "pattern user_ty = {:?} for pattern at {:?}" , user_ty, span) ;
691
+
692
+ kind = PatternKind :: AscribeUserType {
693
+ subpattern,
694
+ user_ty,
695
+ } ;
678
696
}
697
+
698
+ kind
679
699
}
680
700
681
701
/// Takes a HIR Path. If the path is a constant, evaluates it and feeds
@@ -729,7 +749,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
729
749
} ,
730
750
}
731
751
}
732
- _ => self . lower_variant_or_leaf ( def, span, ty, vec ! [ ] ) ,
752
+ _ => self . lower_variant_or_leaf ( def, id , span, ty, vec ! [ ] ) ,
733
753
} ;
734
754
735
755
Pattern {
@@ -894,6 +914,17 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
894
914
}
895
915
}
896
916
917
+ impl UserAnnotatedTyHelpers < ' tcx , ' tcx > for PatternContext < ' _ , ' tcx > {
918
+ fn tcx ( & self ) -> TyCtxt < ' _ , ' tcx , ' tcx > {
919
+ self . tcx
920
+ }
921
+
922
+ fn tables ( & self ) -> & ty:: TypeckTables < ' tcx > {
923
+ self . tables
924
+ }
925
+ }
926
+
927
+
897
928
pub trait PatternFoldable < ' tcx > : Sized {
898
929
fn fold_with < F : PatternFolder < ' tcx > > ( & self , folder : & mut F ) -> Self {
899
930
self . super_fold_with ( folder)
0 commit comments