1
1
use std:: mem;
2
2
3
- use crate :: infer:: nll_relate:: TypeRelatingDelegate ;
4
3
use crate :: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind , TypeVariableValue } ;
5
4
use crate :: infer:: { InferCtxt , ObligationEmittingRelation , RegionVariableOrigin } ;
6
5
use rustc_data_structures:: sso:: SsoHashMap ;
@@ -43,13 +42,7 @@ impl<'tcx> InferCtxt<'tcx> {
43
42
//
44
43
// We then relate `generalized_ty <: source_ty`,adding constraints like `'x: '?2` and `?1 <: ?3`.
45
44
let Generalization { value_may_be_infer : generalized_ty, has_unconstrained_ty_var } =
46
- generalize (
47
- self ,
48
- & mut CombineDelegate { infcx : self , span : relation. span ( ) } ,
49
- source_ty,
50
- target_vid,
51
- ambient_variance,
52
- ) ?;
45
+ self . generalize ( relation. span ( ) , target_vid, ambient_variance, source_ty) ?;
53
46
54
47
// Constrain `b_vid` to the generalized type `generalized_ty`.
55
48
if let & ty:: Infer ( ty:: TyVar ( generalized_vid) ) = generalized_ty. kind ( ) {
@@ -180,13 +173,7 @@ impl<'tcx> InferCtxt<'tcx> {
180
173
// FIXME(generic_const_exprs): Occurs check failures for unevaluated
181
174
// constants and generic expressions are not yet handled correctly.
182
175
let Generalization { value_may_be_infer : generalized_ct, has_unconstrained_ty_var } =
183
- generalize (
184
- self ,
185
- & mut CombineDelegate { infcx : self , span } ,
186
- source_ct,
187
- target_vid,
188
- ty:: Variance :: Invariant ,
189
- ) ?;
176
+ self . generalize ( span, target_vid, ty:: Variance :: Invariant , source_ct) ?;
190
177
191
178
debug_assert ! ( !generalized_ct. is_ct_infer( ) ) ;
192
179
if has_unconstrained_ty_var {
@@ -202,93 +189,45 @@ impl<'tcx> InferCtxt<'tcx> {
202
189
// `generalized_ct` and `source_ct` here.`
203
190
Ok ( generalized_ct)
204
191
}
205
- }
206
-
207
- /// Attempts to generalize `term` for the type variable `for_vid`.
208
- /// This checks for cycles -- that is, whether the type `term`
209
- /// references `for_vid`.
210
- pub ( super ) fn generalize < ' tcx , D : GeneralizerDelegate < ' tcx > , T : Into < Term < ' tcx > > + Relate < ' tcx > > (
211
- infcx : & InferCtxt < ' tcx > ,
212
- delegate : & mut D ,
213
- term : T ,
214
- for_vid : impl Into < ty:: TermVid > ,
215
- ambient_variance : ty:: Variance ,
216
- ) -> RelateResult < ' tcx , Generalization < T > > {
217
- let ( for_universe, root_vid) = match for_vid. into ( ) {
218
- ty:: TermVid :: Ty ( ty_vid) => (
219
- infcx. probe_ty_var ( ty_vid) . unwrap_err ( ) ,
220
- ty:: TermVid :: Ty ( infcx. inner . borrow_mut ( ) . type_variables ( ) . sub_root_var ( ty_vid) ) ,
221
- ) ,
222
- ty:: TermVid :: Const ( ct_vid) => (
223
- infcx. probe_const_var ( ct_vid) . unwrap_err ( ) ,
224
- ty:: TermVid :: Const ( infcx. inner . borrow_mut ( ) . const_unification_table ( ) . find ( ct_vid) . vid ) ,
225
- ) ,
226
- } ;
227
-
228
- let mut generalizer = Generalizer {
229
- infcx,
230
- delegate,
231
- ambient_variance,
232
- root_vid,
233
- for_universe,
234
- root_term : term. into ( ) ,
235
- in_alias : false ,
236
- has_unconstrained_ty_var : false ,
237
- cache : Default :: default ( ) ,
238
- } ;
239
-
240
- assert ! ( !term. has_escaping_bound_vars( ) ) ;
241
- let value_may_be_infer = generalizer. relate ( term, term) ?;
242
- let has_unconstrained_ty_var = generalizer. has_unconstrained_ty_var ;
243
- Ok ( Generalization { value_may_be_infer, has_unconstrained_ty_var } )
244
- }
245
-
246
- /// Abstracts the handling of region vars between HIR and MIR/NLL typechecking
247
- /// in the generalizer code.
248
- pub trait GeneralizerDelegate < ' tcx > {
249
- fn forbid_inference_vars ( ) -> bool ;
250
-
251
- fn span ( & self ) -> Span ;
252
-
253
- fn generalize_region ( & mut self , universe : ty:: UniverseIndex ) -> ty:: Region < ' tcx > ;
254
- }
255
-
256
- pub struct CombineDelegate < ' cx , ' tcx > {
257
- pub infcx : & ' cx InferCtxt < ' tcx > ,
258
- pub span : Span ,
259
- }
260
-
261
- impl < ' tcx > GeneralizerDelegate < ' tcx > for CombineDelegate < ' _ , ' tcx > {
262
- fn forbid_inference_vars ( ) -> bool {
263
- false
264
- }
265
-
266
- fn span ( & self ) -> Span {
267
- self . span
268
- }
269
-
270
- fn generalize_region ( & mut self , universe : ty:: UniverseIndex ) -> ty:: Region < ' tcx > {
271
- // FIXME: This is non-ideal because we don't give a
272
- // very descriptive origin for this region variable.
273
- self . infcx
274
- . next_region_var_in_universe ( RegionVariableOrigin :: MiscVariable ( self . span ) , universe)
275
- }
276
- }
277
192
278
- impl < ' tcx , T > GeneralizerDelegate < ' tcx > for T
279
- where
280
- T : TypeRelatingDelegate < ' tcx > ,
281
- {
282
- fn forbid_inference_vars ( ) -> bool {
283
- <Self as TypeRelatingDelegate < ' tcx > >:: forbid_inference_vars ( )
284
- }
193
+ /// Attempts to generalize `source_term` for the type variable `target_vid`.
194
+ /// This checks for cycles -- that is, whether `source_term` references `target_vid`.
195
+ fn generalize < T : Into < Term < ' tcx > > + Relate < ' tcx > > (
196
+ & self ,
197
+ span : Span ,
198
+ target_vid : impl Into < ty:: TermVid > ,
199
+ ambient_variance : ty:: Variance ,
200
+ source_term : T ,
201
+ ) -> RelateResult < ' tcx , Generalization < T > > {
202
+ assert ! ( !source_term. has_escaping_bound_vars( ) ) ;
203
+ let ( for_universe, root_vid) = match target_vid. into ( ) {
204
+ ty:: TermVid :: Ty ( ty_vid) => (
205
+ self . probe_ty_var ( ty_vid) . unwrap_err ( ) ,
206
+ ty:: TermVid :: Ty ( self . inner . borrow_mut ( ) . type_variables ( ) . sub_root_var ( ty_vid) ) ,
207
+ ) ,
208
+ ty:: TermVid :: Const ( ct_vid) => (
209
+ self . probe_const_var ( ct_vid) . unwrap_err ( ) ,
210
+ ty:: TermVid :: Const (
211
+ self . inner . borrow_mut ( ) . const_unification_table ( ) . find ( ct_vid) . vid ,
212
+ ) ,
213
+ ) ,
214
+ } ;
285
215
286
- fn span ( & self ) -> Span {
287
- <Self as TypeRelatingDelegate < ' tcx > >:: span ( & self )
288
- }
216
+ let mut generalizer = Generalizer {
217
+ infcx : self ,
218
+ span,
219
+ root_vid,
220
+ for_universe,
221
+ ambient_variance,
222
+ root_term : source_term. into ( ) ,
223
+ in_alias : false ,
224
+ has_unconstrained_ty_var : false ,
225
+ cache : Default :: default ( ) ,
226
+ } ;
289
227
290
- fn generalize_region ( & mut self , universe : ty:: UniverseIndex ) -> ty:: Region < ' tcx > {
291
- <Self as TypeRelatingDelegate < ' tcx > >:: generalize_existential ( self , universe)
228
+ let value_may_be_infer = generalizer. relate ( source_term, source_term) ?;
229
+ let has_unconstrained_ty_var = generalizer. has_unconstrained_ty_var ;
230
+ Ok ( Generalization { value_may_be_infer, has_unconstrained_ty_var } )
292
231
}
293
232
}
294
233
@@ -305,18 +244,10 @@ where
305
244
/// establishes `'0: 'x` as a constraint.
306
245
///
307
246
/// [blog post]: https://is.gd/0hKvIr
308
- struct Generalizer < ' me , ' tcx , D > {
247
+ struct Generalizer < ' me , ' tcx > {
309
248
infcx : & ' me InferCtxt < ' tcx > ,
310
249
311
- /// This is used to abstract the behaviors of the three previous
312
- /// generalizer-like implementations (`Generalizer`, `TypeGeneralizer`,
313
- /// and `ConstInferUnifier`). See [`GeneralizerDelegate`] for more
314
- /// information.
315
- delegate : & ' me mut D ,
316
-
317
- /// After we generalize this type, we are going to relate it to
318
- /// some other type. What will be the variance at this point?
319
- ambient_variance : ty:: Variance ,
250
+ span : Span ,
320
251
321
252
/// The vid of the type variable that is in the process of being
322
253
/// instantiated. If we find this within the value we are folding,
@@ -328,6 +259,10 @@ struct Generalizer<'me, 'tcx, D> {
328
259
/// we reject the relation.
329
260
for_universe : ty:: UniverseIndex ,
330
261
262
+ /// After we generalize this type, we are going to relate it to
263
+ /// some other type. What will be the variance at this point?
264
+ ambient_variance : ty:: Variance ,
265
+
331
266
/// The root term (const or type) we're generalizing. Used for cycle errors.
332
267
root_term : Term < ' tcx > ,
333
268
@@ -344,7 +279,7 @@ struct Generalizer<'me, 'tcx, D> {
344
279
has_unconstrained_ty_var : bool ,
345
280
}
346
281
347
- impl < ' tcx , D > Generalizer < ' _ , ' tcx , D > {
282
+ impl < ' tcx > Generalizer < ' _ , ' tcx > {
348
283
/// Create an error that corresponds to the term kind in `root_term`
349
284
fn cyclic_term_error ( & self ) -> TypeError < ' tcx > {
350
285
match self . root_term . unpack ( ) {
@@ -354,10 +289,7 @@ impl<'tcx, D> Generalizer<'_, 'tcx, D> {
354
289
}
355
290
}
356
291
357
- impl < ' tcx , D > TypeRelation < ' tcx > for Generalizer < ' _ , ' tcx , D >
358
- where
359
- D : GeneralizerDelegate < ' tcx > ,
360
- {
292
+ impl < ' tcx > TypeRelation < ' tcx > for Generalizer < ' _ , ' tcx > {
361
293
fn tcx ( & self ) -> TyCtxt < ' tcx > {
362
294
self . infcx . tcx
363
295
}
@@ -426,12 +358,6 @@ where
426
358
// subtyping. This is basically our "occurs check", preventing
427
359
// us from creating infinitely sized types.
428
360
let g = match * t. kind ( ) {
429
- ty:: Infer ( ty:: TyVar ( _) ) | ty:: Infer ( ty:: IntVar ( _) ) | ty:: Infer ( ty:: FloatVar ( _) )
430
- if D :: forbid_inference_vars ( ) =>
431
- {
432
- bug ! ( "unexpected inference variable encountered in NLL generalization: {t}" ) ;
433
- }
434
-
435
361
ty:: Infer ( ty:: FreshTy ( _) | ty:: FreshIntTy ( _) | ty:: FreshFloatTy ( _) ) => {
436
362
bug ! ( "unexpected infer type: {t}" )
437
363
}
@@ -534,7 +460,7 @@ where
534
460
Ok ( self . infcx . next_ty_var_in_universe (
535
461
TypeVariableOrigin {
536
462
kind : TypeVariableOriginKind :: MiscVariable ,
537
- span : self . delegate . span ( ) ,
463
+ span : self . span ,
538
464
} ,
539
465
self . for_universe ,
540
466
) )
@@ -592,7 +518,10 @@ where
592
518
}
593
519
}
594
520
595
- Ok ( self . delegate . generalize_region ( self . for_universe ) )
521
+ Ok ( self . infcx . next_region_var_in_universe (
522
+ RegionVariableOrigin :: MiscVariable ( self . span ) ,
523
+ self . for_universe ,
524
+ ) )
596
525
}
597
526
598
527
#[ instrument( level = "debug" , skip( self , c2) , ret) ]
@@ -604,9 +533,6 @@ where
604
533
assert_eq ! ( c, c2) ; // we are misusing TypeRelation here; both LHS and RHS ought to be ==
605
534
606
535
match c. kind ( ) {
607
- ty:: ConstKind :: Infer ( InferConst :: Var ( _) ) if D :: forbid_inference_vars ( ) => {
608
- bug ! ( "unexpected inference variable encountered in NLL generalization: {:?}" , c) ;
609
- }
610
536
ty:: ConstKind :: Infer ( InferConst :: Var ( vid) ) => {
611
537
// If root const vids are equal, then `root_vid` and
612
538
// `vid` are related and we'd be inferring an infinitely
0 commit comments