@@ -60,45 +60,35 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
6060 }
6161 }
6262
63- fn freshen_ty < F > ( & mut self , input : Result < Ty < ' tcx > , ty:: InferTy > , mk_fresh : F ) -> Ty < ' tcx >
63+ fn freshen_ty < F > ( & mut self , input : ty:: InferTy , mk_fresh : F ) -> Ty < ' tcx >
6464 where
6565 F : FnOnce ( u32 ) -> Ty < ' tcx > ,
6666 {
67- match input {
68- Ok ( ty) => ty. fold_with ( self ) ,
69- Err ( key) => match self . ty_freshen_map . entry ( key) {
70- Entry :: Occupied ( entry) => * entry. get ( ) ,
71- Entry :: Vacant ( entry) => {
72- let index = self . ty_freshen_count ;
73- self . ty_freshen_count += 1 ;
74- let t = mk_fresh ( index) ;
75- entry. insert ( t) ;
76- t
77- }
78- } ,
67+ match self . ty_freshen_map . entry ( input) {
68+ Entry :: Occupied ( entry) => * entry. get ( ) ,
69+ Entry :: Vacant ( entry) => {
70+ let index = self . ty_freshen_count ;
71+ self . ty_freshen_count += 1 ;
72+ let t = mk_fresh ( index) ;
73+ entry. insert ( t) ;
74+ t
75+ }
7976 }
8077 }
8178
82- fn freshen_const < F > (
83- & mut self ,
84- input : Result < ty:: Const < ' tcx > , ty:: InferConst > ,
85- freshener : F ,
86- ) -> ty:: Const < ' tcx >
79+ fn freshen_const < F > ( & mut self , input : ty:: InferConst , freshener : F ) -> ty:: Const < ' tcx >
8780 where
8881 F : FnOnce ( u32 ) -> ty:: InferConst ,
8982 {
90- match input {
91- Ok ( ct) => ct. fold_with ( self ) ,
92- Err ( key) => match self . const_freshen_map . entry ( key) {
93- Entry :: Occupied ( entry) => * entry. get ( ) ,
94- Entry :: Vacant ( entry) => {
95- let index = self . const_freshen_count ;
96- self . const_freshen_count += 1 ;
97- let ct = ty:: Const :: new_infer ( self . infcx . tcx , freshener ( index) ) ;
98- entry. insert ( ct) ;
99- ct
100- }
101- } ,
83+ match self . const_freshen_map . entry ( input) {
84+ Entry :: Occupied ( entry) => * entry. get ( ) ,
85+ Entry :: Vacant ( entry) => {
86+ let index = self . const_freshen_count ;
87+ self . const_freshen_count += 1 ;
88+ let ct = ty:: Const :: new_infer ( self . infcx . tcx , freshener ( index) ) ;
89+ entry. insert ( ct) ;
90+ ct
91+ }
10292 }
10393 }
10494}
@@ -130,7 +120,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
130120 t
131121 } else {
132122 match * t. kind ( ) {
133- ty:: Infer ( v) => self . fold_infer_ty ( v) . unwrap_or ( t ) ,
123+ ty:: Infer ( v) => self . fold_infer_ty ( v) ,
134124
135125 // This code is hot enough that a non-debug assertion here makes a noticeable
136126 // difference on benchmarks like `wg-grammar`.
@@ -146,27 +136,24 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
146136 match ct. kind ( ) {
147137 ty:: ConstKind :: Infer ( ty:: InferConst :: Var ( v) ) => {
148138 let mut inner = self . infcx . inner . borrow_mut ( ) ;
149- let input =
150- inner. const_unification_table ( ) . probe_value ( v) . known ( ) . ok_or_else ( || {
151- ty:: InferConst :: Var ( inner. const_unification_table ( ) . find ( v) . vid )
152- } ) ;
153- drop ( inner) ;
154- self . freshen_const ( input, ty:: InferConst :: Fresh )
155- }
156- ty:: ConstKind :: Infer ( ty:: InferConst :: Fresh ( i) ) => {
157- if i >= self . const_freshen_count {
158- bug ! (
159- "Encountered a freshend const with id {} \
160- but our counter is only at {}",
161- i,
162- self . const_freshen_count,
163- ) ;
139+ match inner. const_unification_table ( ) . probe_value ( v) . known ( ) {
140+ Some ( const_) => {
141+ drop ( inner) ;
142+ const_. fold_with ( self )
143+ }
144+ None => {
145+ let input =
146+ ty:: InferConst :: Var ( inner. const_unification_table ( ) . find ( v) . vid ) ;
147+ self . freshen_const ( input, ty:: InferConst :: Fresh )
148+ }
164149 }
165- ct
150+ }
151+ ty:: ConstKind :: Infer ( ty:: InferConst :: Fresh ( _) ) => {
152+ bug ! ( "trying to freshen already-freshened const {ct:?}" ) ;
166153 }
167154
168155 ty:: ConstKind :: Bound ( ..) | ty:: ConstKind :: Placeholder ( _) => {
169- bug ! ( "unexpected const {:?}" , ct )
156+ bug ! ( "unexpected const {ct :?}" )
170157 }
171158
172159 ty:: ConstKind :: Param ( _)
@@ -181,56 +168,49 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
181168impl < ' a , ' tcx > TypeFreshener < ' a , ' tcx > {
182169 // This is separate from `fold_ty` to keep that method small and inlinable.
183170 #[ inline( never) ]
184- fn fold_infer_ty ( & mut self , v : ty:: InferTy ) -> Option < Ty < ' tcx > > {
185- match v {
171+ fn fold_infer_ty ( & mut self , ty : ty:: InferTy ) -> Ty < ' tcx > {
172+ match ty {
186173 ty:: TyVar ( v) => {
187174 let mut inner = self . infcx . inner . borrow_mut ( ) ;
188- let input = inner
189- . type_variables ( )
190- . probe ( v)
191- . known ( )
192- . ok_or_else ( || ty:: TyVar ( inner. type_variables ( ) . root_var ( v) ) ) ;
193- drop ( inner) ;
194- Some ( self . freshen_ty ( input, |n| Ty :: new_fresh ( self . infcx . tcx , n) ) )
175+ match inner. type_variables ( ) . probe ( v) . known ( ) {
176+ Some ( ty) => {
177+ drop ( inner) ;
178+ ty. fold_with ( self )
179+ }
180+ None => {
181+ let input = ty:: TyVar ( inner. type_variables ( ) . root_var ( v) ) ;
182+ self . freshen_ty ( input, |n| Ty :: new_fresh ( self . infcx . tcx , n) )
183+ }
184+ }
195185 }
196186
197187 ty:: IntVar ( v) => {
198188 let mut inner = self . infcx . inner . borrow_mut ( ) ;
199189 let value = inner. int_unification_table ( ) . probe_value ( v) ;
200- let input = match value {
201- ty:: IntVarValue :: IntType ( ty) => Ok ( Ty :: new_int ( self . infcx . tcx , ty) ) ,
202- ty:: IntVarValue :: UintType ( ty) => Ok ( Ty :: new_uint ( self . infcx . tcx , ty) ) ,
190+ match value {
191+ ty:: IntVarValue :: IntType ( ty) => Ty :: new_int ( self . infcx . tcx , ty) ,
192+ ty:: IntVarValue :: UintType ( ty) => Ty :: new_uint ( self . infcx . tcx , ty) ,
203193 ty:: IntVarValue :: Unknown => {
204- Err ( ty:: IntVar ( inner. int_unification_table ( ) . find ( v) ) )
194+ let input = ty:: IntVar ( inner. int_unification_table ( ) . find ( v) ) ;
195+ self . freshen_ty ( input, |n| Ty :: new_fresh_int ( self . infcx . tcx , n) )
205196 }
206- } ;
207- drop ( inner) ;
208- Some ( self . freshen_ty ( input, |n| Ty :: new_fresh_int ( self . infcx . tcx , n) ) )
197+ }
209198 }
210199
211200 ty:: FloatVar ( v) => {
212201 let mut inner = self . infcx . inner . borrow_mut ( ) ;
213202 let value = inner. float_unification_table ( ) . probe_value ( v) ;
214- let input = match value {
215- ty:: FloatVarValue :: Known ( ty) => Ok ( Ty :: new_float ( self . infcx . tcx , ty) ) ,
203+ match value {
204+ ty:: FloatVarValue :: Known ( ty) => Ty :: new_float ( self . infcx . tcx , ty) ,
216205 ty:: FloatVarValue :: Unknown => {
217- Err ( ty:: FloatVar ( inner. float_unification_table ( ) . find ( v) ) )
206+ let input = ty:: FloatVar ( inner. float_unification_table ( ) . find ( v) ) ;
207+ self . freshen_ty ( input, |n| Ty :: new_fresh_float ( self . infcx . tcx , n) )
218208 }
219- } ;
220- drop ( inner) ;
221- Some ( self . freshen_ty ( input, |n| Ty :: new_fresh_float ( self . infcx . tcx , n) ) )
209+ }
222210 }
223211
224- ty:: FreshTy ( ct) | ty:: FreshIntTy ( ct) | ty:: FreshFloatTy ( ct) => {
225- if ct >= self . ty_freshen_count {
226- bug ! (
227- "Encountered a freshend type with id {} \
228- but our counter is only at {}",
229- ct,
230- self . ty_freshen_count
231- ) ;
232- }
233- None
212+ ty:: FreshTy ( _) | ty:: FreshIntTy ( _) | ty:: FreshFloatTy ( _) => {
213+ bug ! ( "trying to freshen already-freshened type {ty:?}" ) ;
234214 }
235215 }
236216 }
0 commit comments