Skip to content

Commit 6bb1f79

Browse files
committed
cleanup nll generalizer
1 parent 71af5c4 commit 6bb1f79

File tree

1 file changed

+7
-68
lines changed
  • compiler/rustc_infer/src/infer/nll_relate

1 file changed

+7
-68
lines changed

compiler/rustc_infer/src/infer/nll_relate/mod.rs

+7-68
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ use rustc_middle::traits::ObligationCause;
3030
use rustc_middle::ty::error::TypeError;
3131
use rustc_middle::ty::fold::FnMutDelegate;
3232
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
33-
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
33+
use rustc_middle::ty::visit::TypeVisitableExt;
3434
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt};
3535
use rustc_span::{Span, Symbol};
3636
use std::fmt::Debug;
37-
use std::ops::ControlFlow;
3837

3938
use super::combine::ObligationEmittingRelation;
4039

@@ -115,11 +114,6 @@ pub trait TypeRelatingDelegate<'tcx> {
115114
fn forbid_inference_vars() -> bool;
116115
}
117116

118-
#[derive(Clone, Debug, Default)]
119-
struct BoundRegionScope<'tcx> {
120-
map: FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
121-
}
122-
123117
#[derive(Copy, Clone)]
124118
struct UniversallyQuantified(bool);
125119

@@ -230,10 +224,13 @@ where
230224
) -> RelateResult<'tcx, T> {
231225
let universe = self.infcx.probe_ty_var(for_vid).unwrap_err();
232226

227+
if value.has_escaping_bound_vars() {
228+
bug!("trying to instantiate {for_vid:?} with escaping bound vars: {value:?}");
229+
}
230+
233231
let mut generalizer = TypeGeneralizer {
234232
infcx: self.infcx,
235233
delegate: &mut self.delegate,
236-
first_free_index: ty::INNERMOST,
237234
ambient_variance: self.ambient_variance,
238235
for_vid_sub_root: self.infcx.inner.borrow_mut().type_variables().sub_root_var(for_vid),
239236
universe,
@@ -488,13 +485,7 @@ where
488485
}
489486

490487
if a == b {
491-
// Subtle: if a or b has a bound variable that we are lazily
492-
// substituting, then even if a == b, it could be that the values we
493-
// will substitute for those bound variables are *not* the same, and
494-
// hence returning `Ok(a)` is incorrect.
495-
if !a.has_escaping_bound_vars() && !b.has_escaping_bound_vars() {
496-
return Ok(a);
497-
}
488+
return Ok(a);
498489
}
499490

500491
match (a.kind(), b.kind()) {
@@ -726,47 +717,6 @@ where
726717
}
727718
}
728719

729-
/// When we encounter a binder like `for<..> fn(..)`, we actually have
730-
/// to walk the `fn` value to find all the values bound by the `for`
731-
/// (these are not explicitly present in the ty representation right
732-
/// now). This visitor handles that: it descends the type, tracking
733-
/// binder depth, and finds late-bound regions targeting the
734-
/// `for<..`>. For each of those, it creates an entry in
735-
/// `bound_region_scope`.
736-
struct ScopeInstantiator<'me, 'tcx> {
737-
next_region: &'me mut dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
738-
// The debruijn index of the scope we are instantiating.
739-
target_index: ty::DebruijnIndex,
740-
bound_region_scope: &'me mut BoundRegionScope<'tcx>,
741-
}
742-
743-
impl<'me, 'tcx> TypeVisitor<TyCtxt<'tcx>> for ScopeInstantiator<'me, 'tcx> {
744-
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(
745-
&mut self,
746-
t: &ty::Binder<'tcx, T>,
747-
) -> ControlFlow<Self::BreakTy> {
748-
self.target_index.shift_in(1);
749-
t.super_visit_with(self);
750-
self.target_index.shift_out(1);
751-
752-
ControlFlow::Continue(())
753-
}
754-
755-
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
756-
let ScopeInstantiator { bound_region_scope, next_region, .. } = self;
757-
758-
match *r {
759-
ty::ReLateBound(debruijn, br) if debruijn == self.target_index => {
760-
bound_region_scope.map.entry(br).or_insert_with(|| next_region(br));
761-
}
762-
763-
_ => {}
764-
}
765-
766-
ControlFlow::Continue(())
767-
}
768-
}
769-
770720
/// The "type generalizer" is used when handling inference variables.
771721
///
772722
/// The basic strategy for handling a constraint like `?A <: B` is to
@@ -780,11 +730,6 @@ impl<'me, 'tcx> TypeVisitor<TyCtxt<'tcx>> for ScopeInstantiator<'me, 'tcx> {
780730
/// value of `A`. Finally, we relate `&'0 u32 <: &'x u32`, which
781731
/// establishes `'0: 'x` as a constraint.
782732
///
783-
/// As a side-effect of this generalization procedure, we also replace
784-
/// all the bound regions that we have traversed with concrete values,
785-
/// so that the resulting generalized type is independent from the
786-
/// scopes.
787-
///
788733
/// [blog post]: https://is.gd/0hKvIr
789734
struct TypeGeneralizer<'me, 'tcx, D>
790735
where
@@ -798,8 +743,6 @@ where
798743
/// some other type. What will be the variance at this point?
799744
ambient_variance: ty::Variance,
800745

801-
first_free_index: ty::DebruijnIndex,
802-
803746
/// The vid of the type variable that is in the process of being
804747
/// instantiated. If we find this within the value we are folding,
805748
/// that means we would have created a cyclic value.
@@ -939,7 +882,7 @@ where
939882
) -> RelateResult<'tcx, ty::Region<'tcx>> {
940883
debug!("TypeGeneralizer::regions(a={:?})", a);
941884

942-
if let ty::ReLateBound(debruijn, _) = *a && debruijn < self.first_free_index {
885+
if let ty::ReLateBound(..) = *a {
943886
return Ok(a);
944887
}
945888

@@ -958,7 +901,6 @@ where
958901
// FIXME(#54105) -- if the ambient variance is bivariant,
959902
// though, we may however need to check well-formedness or
960903
// risk a problem like #41677 again.
961-
962904
let replacement_region_vid = self.delegate.generalize_existential(self.universe);
963905

964906
Ok(replacement_region_vid)
@@ -1002,10 +944,7 @@ where
1002944
T: Relate<'tcx>,
1003945
{
1004946
debug!("TypeGeneralizer::binders(a={:?})", a);
1005-
1006-
self.first_free_index.shift_in(1);
1007947
let result = self.relate(a.skip_binder(), a.skip_binder())?;
1008-
self.first_free_index.shift_out(1);
1009948
Ok(a.rebind(result))
1010949
}
1011950
}

0 commit comments

Comments
 (0)