Skip to content

Commit 109ff73

Browse files
authored
Rollup merge of rust-lang#111132 - lcnr:nll-generalize, r=b-naber
cleanup nll generalizer followup to rust-lang#108861
2 parents 2ff943d + 0c5fe37 commit 109ff73

File tree

6 files changed

+20
-113
lines changed

6 files changed

+20
-113
lines changed

compiler/rustc_borrowck/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,11 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
498498
let next_region = self.infcx.next_region_var(origin);
499499
let vid = next_region.as_var();
500500

501-
if cfg!(debug_assertions) && !self.inside_canonicalization_ctxt() {
501+
if cfg!(debug_assertions) {
502502
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
503503
let ctxt = get_ctxt_fn();
504504
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
505-
var_to_origin.insert(vid, ctxt);
505+
assert_eq!(var_to_origin.insert(vid, ctxt), None);
506506
}
507507

508508
next_region
@@ -520,11 +520,11 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
520520
let next_region = self.infcx.next_nll_region_var(origin);
521521
let vid = next_region.as_var();
522522

523-
if cfg!(debug_assertions) && !self.inside_canonicalization_ctxt() {
523+
if cfg!(debug_assertions) {
524524
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
525525
let ctxt = get_ctxt_fn();
526526
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
527-
var_to_origin.insert(vid, ctxt);
527+
assert_eq!(var_to_origin.insert(vid, ctxt), None);
528528
}
529529

530530
next_region

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,13 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
131131
ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(sym::env),
132132
};
133133

134-
if cfg!(debug_assertions) && !self.type_checker.infcx.inside_canonicalization_ctxt() {
134+
if cfg!(debug_assertions) {
135135
let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut();
136-
var_to_origin.insert(reg.as_var(), RegionCtxt::Placeholder(reg_info));
136+
let new = RegionCtxt::Placeholder(reg_info);
137+
let prev = var_to_origin.insert(reg.as_var(), new);
138+
if let Some(prev) = prev {
139+
assert_eq!(new, prev);
140+
}
137141
}
138142

139143
reg
@@ -146,9 +150,10 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
146150
universe,
147151
);
148152

149-
if cfg!(debug_assertions) && !self.type_checker.infcx.inside_canonicalization_ctxt() {
153+
if cfg!(debug_assertions) {
150154
let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut();
151-
var_to_origin.insert(reg.as_var(), RegionCtxt::Existential(None));
155+
let prev = var_to_origin.insert(reg.as_var(), RegionCtxt::Existential(None));
156+
assert_eq!(prev, None);
152157
}
153158

154159
reg

compiler/rustc_infer/src/infer/at.rs

-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ use super::*;
3030
use rustc_middle::ty::relate::{Relate, TypeRelation};
3131
use rustc_middle::ty::{Const, ImplSubject};
3232

33-
use std::cell::Cell;
34-
3533
/// Whether we should define opaque types or just treat them opaquely.
3634
///
3735
/// Currently only used to prevent predicate matching from matching anything
@@ -84,7 +82,6 @@ impl<'tcx> InferCtxt<'tcx> {
8482
in_snapshot: self.in_snapshot.clone(),
8583
universe: self.universe.clone(),
8684
intercrate: self.intercrate,
87-
inside_canonicalization_ctxt: Cell::new(self.inside_canonicalization_ctxt()),
8885
}
8986
}
9087
}

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,6 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
561561
where
562562
V: TypeFoldable<TyCtxt<'tcx>>,
563563
{
564-
let _inside_canonical_ctxt_guard = infcx.set_canonicalization_ctxt();
565-
566564
let needs_canonical_flags = if canonicalize_region_mode.any() {
567565
TypeFlags::HAS_INFER |
568566
TypeFlags::HAS_FREE_REGIONS | // `HAS_RE_PLACEHOLDER` implies `HAS_FREE_REGIONS`

compiler/rustc_infer/src/infer/mod.rs

-32
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use rustc_span::Span;
3939

4040
use std::cell::{Cell, RefCell};
4141
use std::fmt;
42-
use std::ops::Drop;
4342

4443
use self::combine::CombineFields;
4544
use self::error_reporting::TypeErrCtxt;
@@ -342,11 +341,6 @@ pub struct InferCtxt<'tcx> {
342341
/// there is no type that the user could *actually name* that
343342
/// would satisfy it. This avoids crippling inference, basically.
344343
pub intercrate: bool,
345-
346-
/// Flag that is set when we enter canonicalization. Used for debugging to ensure
347-
/// that we only collect region information for `BorrowckInferCtxt::reg_var_to_origin`
348-
/// inside non-canonicalization contexts.
349-
inside_canonicalization_ctxt: Cell<bool>,
350344
}
351345

352346
/// See the `error_reporting` module for more details.
@@ -638,7 +632,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
638632
skip_leak_check: Cell::new(false),
639633
universe: Cell::new(ty::UniverseIndex::ROOT),
640634
intercrate,
641-
inside_canonicalization_ctxt: Cell::new(false),
642635
}
643636
}
644637
}
@@ -1636,31 +1629,6 @@ impl<'tcx> InferCtxt<'tcx> {
16361629
}
16371630
}
16381631
}
1639-
1640-
pub fn inside_canonicalization_ctxt(&self) -> bool {
1641-
self.inside_canonicalization_ctxt.get()
1642-
}
1643-
1644-
pub fn set_canonicalization_ctxt(&self) -> CanonicalizationCtxtGuard<'_, 'tcx> {
1645-
let prev_ctxt = self.inside_canonicalization_ctxt();
1646-
self.inside_canonicalization_ctxt.set(true);
1647-
CanonicalizationCtxtGuard { prev_ctxt, infcx: self }
1648-
}
1649-
1650-
fn set_canonicalization_ctxt_to(&self, ctxt: bool) {
1651-
self.inside_canonicalization_ctxt.set(ctxt);
1652-
}
1653-
}
1654-
1655-
pub struct CanonicalizationCtxtGuard<'cx, 'tcx> {
1656-
prev_ctxt: bool,
1657-
infcx: &'cx InferCtxt<'tcx>,
1658-
}
1659-
1660-
impl<'cx, 'tcx> Drop for CanonicalizationCtxtGuard<'cx, 'tcx> {
1661-
fn drop(&mut self) {
1662-
self.infcx.set_canonicalization_ctxt_to(self.prev_ctxt)
1663-
}
16641632
}
16651633

16661634
impl<'tcx> TypeErrCtxt<'_, 'tcx> {

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)