@@ -112,11 +112,9 @@ use rustc_hir::{HirIdMap, ImplicitSelfKind, Node};
112
112
use rustc_index:: bit_set:: BitSet ;
113
113
use rustc_index:: vec:: Idx ;
114
114
use rustc_infer:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
115
- use rustc_middle:: ty:: fold:: { TypeFoldable , TypeFolder } ;
116
115
use rustc_middle:: ty:: query:: Providers ;
117
- use rustc_middle:: ty:: subst:: GenericArgKind ;
118
116
use rustc_middle:: ty:: subst:: { InternalSubsts , Subst , SubstsRef } ;
119
- use rustc_middle:: ty:: { self , RegionKind , Ty , TyCtxt , UserType } ;
117
+ use rustc_middle:: ty:: { self , Ty , TyCtxt , UserType } ;
120
118
use rustc_session:: config;
121
119
use rustc_session:: parse:: feature_err;
122
120
use rustc_session:: Session ;
@@ -321,117 +319,6 @@ fn used_trait_imports(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &FxHashSet<LocalDe
321
319
& * tcx. typeck ( def_id) . used_trait_imports
322
320
}
323
321
324
- /// Inspects the substs of opaque types, replacing any inference variables
325
- /// with proper generic parameter from the identity substs.
326
- ///
327
- /// This is run after we normalize the function signature, to fix any inference
328
- /// variables introduced by the projection of associated types. This ensures that
329
- /// any opaque types used in the signature continue to refer to generic parameters,
330
- /// allowing them to be considered for defining uses in the function body
331
- ///
332
- /// For example, consider this code.
333
- ///
334
- /// ```rust
335
- /// trait MyTrait {
336
- /// type MyItem;
337
- /// fn use_it(self) -> Self::MyItem
338
- /// }
339
- /// impl<T, I> MyTrait for T where T: Iterator<Item = I> {
340
- /// type MyItem = impl Iterator<Item = I>;
341
- /// fn use_it(self) -> Self::MyItem {
342
- /// self
343
- /// }
344
- /// }
345
- /// ```
346
- ///
347
- /// When we normalize the signature of `use_it` from the impl block,
348
- /// we will normalize `Self::MyItem` to the opaque type `impl Iterator<Item = I>`
349
- /// However, this projection result may contain inference variables, due
350
- /// to the way that projection works. We didn't have any inference variables
351
- /// in the signature to begin with - leaving them in will cause us to incorrectly
352
- /// conclude that we don't have a defining use of `MyItem`. By mapping inference
353
- /// variables back to the actual generic parameters, we will correctly see that
354
- /// we have a defining use of `MyItem`
355
- fn fixup_opaque_types < ' tcx , T > ( tcx : TyCtxt < ' tcx > , val : T ) -> T
356
- where
357
- T : TypeFoldable < ' tcx > ,
358
- {
359
- struct FixupFolder < ' tcx > {
360
- tcx : TyCtxt < ' tcx > ,
361
- }
362
-
363
- impl < ' tcx > TypeFolder < ' tcx > for FixupFolder < ' tcx > {
364
- fn tcx < ' a > ( & ' a self ) -> TyCtxt < ' tcx > {
365
- self . tcx
366
- }
367
-
368
- fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
369
- match * ty. kind ( ) {
370
- ty:: Opaque ( def_id, substs) => {
371
- debug ! ( "fixup_opaque_types: found type {:?}" , ty) ;
372
- // Here, we replace any inference variables that occur within
373
- // the substs of an opaque type. By definition, any type occurring
374
- // in the substs has a corresponding generic parameter, which is what
375
- // we replace it with.
376
- // This replacement is only run on the function signature, so any
377
- // inference variables that we come across must be the rust of projection
378
- // (there's no other way for a user to get inference variables into
379
- // a function signature).
380
- if ty. needs_infer ( ) {
381
- let new_substs = InternalSubsts :: for_item ( self . tcx , def_id, |param, _| {
382
- let old_param = substs[ param. index as usize ] ;
383
- match old_param. unpack ( ) {
384
- GenericArgKind :: Type ( old_ty) => {
385
- if let ty:: Infer ( _) = old_ty. kind ( ) {
386
- // Replace inference type with a generic parameter
387
- self . tcx . mk_param_from_def ( param)
388
- } else {
389
- old_param. fold_with ( self )
390
- }
391
- }
392
- GenericArgKind :: Const ( old_const) => {
393
- if let ty:: ConstKind :: Infer ( _) = old_const. val {
394
- // This should never happen - we currently do not support
395
- // 'const projections', e.g.:
396
- // `impl<T: SomeTrait> MyTrait for T where <T as SomeTrait>::MyConst == 25`
397
- // which should be the only way for us to end up with a const inference
398
- // variable after projection. If Rust ever gains support for this kind
399
- // of projection, this should *probably* be changed to
400
- // `self.tcx.mk_param_from_def(param)`
401
- bug ! (
402
- "Found infer const: `{:?}` in opaque type: {:?}" ,
403
- old_const,
404
- ty
405
- ) ;
406
- } else {
407
- old_param. fold_with ( self )
408
- }
409
- }
410
- GenericArgKind :: Lifetime ( old_region) => {
411
- if let RegionKind :: ReVar ( _) = old_region {
412
- self . tcx . mk_param_from_def ( param)
413
- } else {
414
- old_param. fold_with ( self )
415
- }
416
- }
417
- }
418
- } ) ;
419
- let new_ty = self . tcx . mk_opaque ( def_id, new_substs) ;
420
- debug ! ( "fixup_opaque_types: new type: {:?}" , new_ty) ;
421
- new_ty
422
- } else {
423
- ty
424
- }
425
- }
426
- _ => ty. super_fold_with ( self ) ,
427
- }
428
- }
429
- }
430
-
431
- debug ! ( "fixup_opaque_types({:?})" , val) ;
432
- val. fold_with ( & mut FixupFolder { tcx } )
433
- }
434
-
435
322
fn typeck_const_arg < ' tcx > (
436
323
tcx : TyCtxt < ' tcx > ,
437
324
( did, param_did) : ( LocalDefId , DefId ) ,
@@ -510,8 +397,6 @@ fn typeck_with_fallback<'tcx>(
510
397
fn_sig,
511
398
) ;
512
399
513
- let fn_sig = fixup_opaque_types ( tcx, fn_sig) ;
514
-
515
400
let fcx = check_fn ( & inh, param_env, fn_sig, decl, id, body, None ) . 0 ;
516
401
fcx
517
402
} else {
0 commit comments