@@ -46,51 +46,56 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
4646 }
4747}
4848
49- /// The opportunistic type and region resolver is similar to the
50- /// opportunistic type resolver, but also opportunistically resolves
51- /// regions. It is useful for canonicalization.
52- pub struct OpportunisticTypeAndRegionResolver < ' a , ' tcx > {
49+ /// The opportunistic region resolver opportunistically resolves regions
50+ /// variables to the variable with the least variable id. It is used when
51+ /// normlizing projections to avoid hitting the recursion limit by creating
52+ /// many versions of a predicate for types that in the end have to unify.
53+ ///
54+ /// If you want to resolve type and const variables as well, call
55+ /// [InferCtxt::resolve_vars_if_possible] first.
56+ pub struct OpportunisticRegionResolver < ' a , ' tcx > {
5357 infcx : & ' a InferCtxt < ' a , ' tcx > ,
5458}
5559
56- impl < ' a , ' tcx > OpportunisticTypeAndRegionResolver < ' a , ' tcx > {
60+ impl < ' a , ' tcx > OpportunisticRegionResolver < ' a , ' tcx > {
5761 pub fn new ( infcx : & ' a InferCtxt < ' a , ' tcx > ) -> Self {
58- OpportunisticTypeAndRegionResolver { infcx }
62+ OpportunisticRegionResolver { infcx }
5963 }
6064}
6165
62- impl < ' a , ' tcx > TypeFolder < ' tcx > for OpportunisticTypeAndRegionResolver < ' a , ' tcx > {
66+ impl < ' a , ' tcx > TypeFolder < ' tcx > for OpportunisticRegionResolver < ' a , ' tcx > {
6367 fn tcx < ' b > ( & ' b self ) -> TyCtxt < ' tcx > {
6468 self . infcx . tcx
6569 }
6670
6771 fn fold_ty ( & mut self , t : Ty < ' tcx > ) -> Ty < ' tcx > {
68- if !t. needs_infer ( ) {
72+ if !t. has_infer_regions ( ) {
6973 t // micro-optimize -- if there is nothing in this type that this fold affects...
7074 } else {
71- let t0 = self . infcx . shallow_resolve ( t) ;
72- t0. super_fold_with ( self )
75+ t. super_fold_with ( self )
7376 }
7477 }
7578
7679 fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
7780 match * r {
78- ty:: ReVar ( rid) => self
79- . infcx
80- . inner
81- . borrow_mut ( )
82- . unwrap_region_constraints ( )
83- . opportunistic_resolve_var ( self . tcx ( ) , rid) ,
81+ ty:: ReVar ( rid) => {
82+ let resolved = self
83+ . infcx
84+ . inner
85+ . borrow_mut ( )
86+ . unwrap_region_constraints ( )
87+ . opportunistic_resolve_var ( rid) ;
88+ self . tcx ( ) . reuse_or_mk_region ( r, ty:: ReVar ( resolved) )
89+ }
8490 _ => r,
8591 }
8692 }
8793
8894 fn fold_const ( & mut self , ct : & ' tcx ty:: Const < ' tcx > ) -> & ' tcx ty:: Const < ' tcx > {
89- if !ct. needs_infer ( ) {
95+ if !ct. has_infer_regions ( ) {
9096 ct // micro-optimize -- if there is nothing in this const that this fold affects...
9197 } else {
92- let c0 = self . infcx . shallow_resolve ( ct) ;
93- c0. super_fold_with ( self )
98+ ct. super_fold_with ( self )
9499 }
95100 }
96101}
0 commit comments