@@ -400,23 +400,26 @@ where
400
400
}
401
401
402
402
/// Replaces the escaping bound vars (late bound regions or bound types) in a type.
403
- struct BoundVarReplacer < ' a , ' tcx > {
403
+ struct BoundVarReplacer < ' tcx , D > {
404
404
tcx : TyCtxt < ' tcx > ,
405
405
406
406
/// As with `RegionFolder`, represents the index of a binder *just outside*
407
407
/// the ones we have visited.
408
408
current_index : ty:: DebruijnIndex ,
409
409
410
- delegate : & ' a mut dyn BoundVarReplacerDelegate < ' tcx > ,
410
+ delegate : D ,
411
411
}
412
412
413
- impl < ' a , ' tcx > BoundVarReplacer < ' a , ' tcx > {
414
- fn new ( tcx : TyCtxt < ' tcx > , delegate : & ' a mut dyn BoundVarReplacerDelegate < ' tcx > ) -> Self {
413
+ impl < ' tcx , D : BoundVarReplacerDelegate < ' tcx > > BoundVarReplacer < ' tcx , D > {
414
+ fn new ( tcx : TyCtxt < ' tcx > , delegate : D ) -> Self {
415
415
BoundVarReplacer { tcx, current_index : ty:: INNERMOST , delegate }
416
416
}
417
417
}
418
418
419
- impl < ' a , ' tcx > TypeFolder < ' tcx > for BoundVarReplacer < ' a , ' tcx > {
419
+ impl < ' tcx , D > TypeFolder < ' tcx > for BoundVarReplacer < ' tcx , D >
420
+ where
421
+ D : BoundVarReplacerDelegate < ' tcx > ,
422
+ {
420
423
fn tcx < ' b > ( & ' b self ) -> TyCtxt < ' tcx > {
421
424
self . tcx
422
425
}
@@ -452,7 +455,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for BoundVarReplacer<'a, 'tcx> {
452
455
// debruijn index. Then we adjust it to the
453
456
// correct depth.
454
457
assert_eq ! ( debruijn1, ty:: INNERMOST ) ;
455
- self . tcx . mk_region ( ty:: ReLateBound ( debruijn, br) )
458
+ self . tcx . reuse_or_mk_region ( region , ty:: ReLateBound ( debruijn, br) )
456
459
} else {
457
460
region
458
461
}
@@ -518,12 +521,12 @@ impl<'tcx> TyCtxt<'tcx> {
518
521
if !value. has_escaping_bound_vars ( ) {
519
522
value
520
523
} else {
521
- let mut delegate = FnMutDelegate {
524
+ let delegate = FnMutDelegate {
522
525
regions : replace_regions,
523
526
types : |b| bug ! ( "unexpected bound ty in binder: {b:?}" ) ,
524
527
consts : |b, ty| bug ! ( "unexpected bound ct in binder: {b:?} {ty}" ) ,
525
528
} ;
526
- let mut replacer = BoundVarReplacer :: new ( self , & mut delegate) ;
529
+ let mut replacer = BoundVarReplacer :: new ( self , delegate) ;
527
530
value. fold_with ( & mut replacer)
528
531
}
529
532
}
@@ -534,7 +537,7 @@ impl<'tcx> TyCtxt<'tcx> {
534
537
pub fn replace_escaping_bound_vars_uncached < T : TypeFoldable < ' tcx > > (
535
538
self ,
536
539
value : T ,
537
- delegate : & mut impl BoundVarReplacerDelegate < ' tcx > ,
540
+ delegate : impl BoundVarReplacerDelegate < ' tcx > ,
538
541
) -> T {
539
542
if !value. has_escaping_bound_vars ( ) {
540
543
value
@@ -550,9 +553,9 @@ impl<'tcx> TyCtxt<'tcx> {
550
553
pub fn replace_bound_vars_uncached < T : TypeFoldable < ' tcx > > (
551
554
self ,
552
555
value : Binder < ' tcx , T > ,
553
- mut delegate : impl BoundVarReplacerDelegate < ' tcx > ,
556
+ delegate : impl BoundVarReplacerDelegate < ' tcx > ,
554
557
) -> T {
555
- self . replace_escaping_bound_vars_uncached ( value. skip_binder ( ) , & mut delegate)
558
+ self . replace_escaping_bound_vars_uncached ( value. skip_binder ( ) , delegate)
556
559
}
557
560
558
561
/// Replaces any late-bound regions bound in `value` with
@@ -580,7 +583,7 @@ impl<'tcx> TyCtxt<'tcx> {
580
583
let shift_bv = |bv : ty:: BoundVar | ty:: BoundVar :: from_usize ( bv. as_usize ( ) + bound_vars) ;
581
584
self . replace_escaping_bound_vars_uncached (
582
585
value,
583
- & mut FnMutDelegate {
586
+ FnMutDelegate {
584
587
regions : |r : ty:: BoundRegion | {
585
588
self . mk_region ( ty:: ReLateBound (
586
589
ty:: INNERMOST ,
@@ -647,11 +650,11 @@ impl<'tcx> TyCtxt<'tcx> {
647
650
where
648
651
T : TypeFoldable < ' tcx > ,
649
652
{
650
- struct Anonymize < ' tcx > {
653
+ struct Anonymize < ' a , ' tcx > {
651
654
tcx : TyCtxt < ' tcx > ,
652
- map : FxIndexMap < ty:: BoundVar , ty:: BoundVariableKind > ,
655
+ map : & ' a mut FxIndexMap < ty:: BoundVar , ty:: BoundVariableKind > ,
653
656
}
654
- impl < ' tcx > BoundVarReplacerDelegate < ' tcx > for Anonymize < ' tcx > {
657
+ impl < ' tcx > BoundVarReplacerDelegate < ' tcx > for Anonymize < ' _ , ' tcx > {
655
658
fn replace_region ( & mut self , br : ty:: BoundRegion ) -> ty:: Region < ' tcx > {
656
659
let entry = self . map . entry ( br. var ) ;
657
660
let index = entry. index ( ) ;
@@ -680,9 +683,10 @@ impl<'tcx> TyCtxt<'tcx> {
680
683
}
681
684
}
682
685
683
- let mut delegate = Anonymize { tcx : self , map : Default :: default ( ) } ;
684
- let inner = self . replace_escaping_bound_vars_uncached ( value. skip_binder ( ) , & mut delegate) ;
685
- let bound_vars = self . mk_bound_variable_kinds ( delegate. map . into_values ( ) ) ;
686
+ let mut map = Default :: default ( ) ;
687
+ let delegate = Anonymize { tcx : self , map : & mut map } ;
688
+ let inner = self . replace_escaping_bound_vars_uncached ( value. skip_binder ( ) , delegate) ;
689
+ let bound_vars = self . mk_bound_variable_kinds ( map. into_values ( ) ) ;
686
690
Binder :: bind_with_vars ( inner, bound_vars)
687
691
}
688
692
}
0 commit comments