Skip to content

Commit 1eab9c5

Browse files
committed
Auto merge of #50397 - sgrif:sg-smaller-universe-refactorings, r=nikomatsakis
Refactorings in preparation for the removal of the leak check This contains all of the commits from #48407 that I was able to pull out on their own. This has most of the refactoring/ground work to unblock other work, but without the behavior changes that still need a crater run and NLL changes. r? @nikomatsakis
2 parents e82261d + 68a1fdf commit 1eab9c5

File tree

12 files changed

+168
-156
lines changed

12 files changed

+168
-156
lines changed

src/librustc/infer/combine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
407407
drop(variables);
408408
self.relate(&u, &u)
409409
}
410-
TypeVariableValue::Unknown { .. } => {
410+
TypeVariableValue::Unknown { universe } => {
411411
match self.ambient_variance {
412412
// Invariant: no need to make a fresh type variable.
413413
ty::Invariant => return Ok(t),
@@ -424,7 +424,7 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
424424
}
425425

426426
let origin = *variables.var_origin(vid);
427-
let new_var_id = variables.new_var(false, origin);
427+
let new_var_id = variables.new_var(universe, false, origin);
428428
let u = self.tcx().mk_var(new_var_id);
429429
debug!("generalize: replacing original vid={:?} with new={:?}",
430430
vid, u);

src/librustc/infer/higher_ranked/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
6262
// Second, we instantiate each bound region in the supertype with a
6363
// fresh concrete region.
6464
let (b_prime, skol_map) =
65-
self.infcx.skolemize_late_bound_regions(b, snapshot);
65+
self.infcx.skolemize_late_bound_regions(b);
6666

6767
debug!("a_prime={:?}", a_prime);
6868
debug!("b_prime={:?}", b_prime);
@@ -114,7 +114,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
114114
// First, we instantiate each bound region in the matcher
115115
// with a skolemized region.
116116
let ((a_match, a_value), skol_map) =
117-
self.infcx.skolemize_late_bound_regions(a_pair, snapshot);
117+
self.infcx.skolemize_late_bound_regions(a_pair);
118118

119119
debug!("higher_ranked_match: a_match={:?}", a_match);
120120
debug!("higher_ranked_match: skol_map={:?}", skol_map);
@@ -587,14 +587,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
587587
///
588588
/// [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/trait-hrtb.html
589589
pub fn skolemize_late_bound_regions<T>(&self,
590-
binder: &ty::Binder<T>,
591-
snapshot: &CombinedSnapshot<'a, 'tcx>)
590+
binder: &ty::Binder<T>)
592591
-> (T, SkolemizationMap<'tcx>)
593592
where T : TypeFoldable<'tcx>
594593
{
595594
let (result, map) = self.tcx.replace_late_bound_regions(binder, |br| {
596-
self.borrow_region_constraints()
597-
.push_skolemized(self.tcx, br, &snapshot.region_constraints_snapshot)
595+
self.universe.set(self.universe().subuniverse());
596+
self.tcx.mk_region(ty::ReSkolemized(self.universe(), br))
598597
});
599598

600599
debug!("skolemize_bound_regions(binder={:?}, result={:?}, map={:?})",
@@ -779,7 +778,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
779778
debug!("pop_skolemized({:?})", skol_map);
780779
let skol_regions: FxHashSet<_> = skol_map.values().cloned().collect();
781780
self.borrow_region_constraints()
782-
.pop_skolemized(self.tcx, &skol_regions, &snapshot.region_constraints_snapshot);
781+
.pop_skolemized(self.universe(), &skol_regions, &snapshot.region_constraints_snapshot);
782+
self.universe.set(snapshot.universe);
783783
if !skol_map.is_empty() {
784784
self.projection_cache.borrow_mut().rollback_skolemized(
785785
&snapshot.projection_cache_snapshot);

src/librustc/infer/lexical_region_resolve/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use infer::RegionVariableOrigin;
1515
use infer::region_constraints::Constraint;
1616
use infer::region_constraints::GenericKind;
1717
use infer::region_constraints::RegionConstraintData;
18-
use infer::region_constraints::VarOrigins;
18+
use infer::region_constraints::VarInfos;
1919
use infer::region_constraints::VerifyBound;
2020
use middle::free_region::RegionRelations;
2121
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
@@ -37,7 +37,7 @@ mod graphviz;
3737
/// all the variables as well as a set of errors that must be reported.
3838
pub fn resolve<'tcx>(
3939
region_rels: &RegionRelations<'_, '_, 'tcx>,
40-
var_origins: VarOrigins,
40+
var_infos: VarInfos,
4141
data: RegionConstraintData<'tcx>,
4242
) -> (
4343
LexicalRegionResolutions<'tcx>,
@@ -47,7 +47,7 @@ pub fn resolve<'tcx>(
4747
let mut errors = vec![];
4848
let mut resolver = LexicalResolver {
4949
region_rels,
50-
var_origins,
50+
var_infos,
5151
data,
5252
};
5353
let values = resolver.infer_variable_values(&mut errors);
@@ -103,7 +103,7 @@ type RegionGraph<'tcx> = graph::Graph<(), Constraint<'tcx>>;
103103

104104
struct LexicalResolver<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
105105
region_rels: &'cx RegionRelations<'cx, 'gcx, 'tcx>,
106-
var_origins: VarOrigins,
106+
var_infos: VarInfos,
107107
data: RegionConstraintData<'tcx>,
108108
}
109109

@@ -132,7 +132,7 @@ impl<'cx, 'gcx, 'tcx> LexicalResolver<'cx, 'gcx, 'tcx> {
132132
}
133133

134134
fn num_vars(&self) -> usize {
135-
self.var_origins.len()
135+
self.var_infos.len()
136136
}
137137

138138
/// Initially, the value for all variables is set to `'empty`, the
@@ -279,7 +279,7 @@ impl<'cx, 'gcx, 'tcx> LexicalResolver<'cx, 'gcx, 'tcx> {
279279

280280
(&ReVar(v_id), _) | (_, &ReVar(v_id)) => {
281281
span_bug!(
282-
self.var_origins[v_id].span(),
282+
self.var_infos[v_id].origin.span(),
283283
"lub_concrete_regions invoked with non-concrete \
284284
regions: {:?}, {:?}",
285285
a,
@@ -576,7 +576,7 @@ impl<'cx, 'gcx, 'tcx> LexicalResolver<'cx, 'gcx, 'tcx> {
576576
if !self.region_rels
577577
.is_subregion_of(lower_bound.region, upper_bound.region)
578578
{
579-
let origin = self.var_origins[node_idx].clone();
579+
let origin = self.var_infos[node_idx].origin.clone();
580580
debug!(
581581
"region inference error at {:?} for {:?}: SubSupConflict sub: {:?} \
582582
sup: {:?}",
@@ -598,7 +598,7 @@ impl<'cx, 'gcx, 'tcx> LexicalResolver<'cx, 'gcx, 'tcx> {
598598
}
599599

600600
span_bug!(
601-
self.var_origins[node_idx].span(),
601+
self.var_infos[node_idx].origin.span(),
602602
"collect_error_for_expanding_node() could not find \
603603
error for var {:?}, lower_bounds={:?}, \
604604
upper_bounds={:?}",

src/librustc/infer/mod.rs

+38-14
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use arena::SyncDroplessArena;
4242
use self::combine::CombineFields;
4343
use self::higher_ranked::HrMatchResult;
4444
use self::region_constraints::{RegionConstraintCollector, RegionSnapshot};
45-
use self::region_constraints::{GenericKind, VerifyBound, RegionConstraintData, VarOrigins};
45+
use self::region_constraints::{GenericKind, VerifyBound, RegionConstraintData, VarInfos};
4646
use self::lexical_region_resolve::LexicalRegionResolutions;
4747
use self::outlives::env::OutlivesEnvironment;
4848
use self::type_variable::TypeVariableOrigin;
@@ -183,6 +183,17 @@ pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
183183
// obligations within. This is expected to be done 'late enough'
184184
// that all type inference variables have been bound and so forth.
185185
pub region_obligations: RefCell<Vec<(ast::NodeId, RegionObligation<'tcx>)>>,
186+
187+
/// What is the innermost universe we have created? Starts out as
188+
/// `UniverseIndex::root()` but grows from there as we enter
189+
/// universal quantifiers.
190+
///
191+
/// NB: At present, we exclude the universal quantifiers on the
192+
/// item we are type-checking, and just consider those names as
193+
/// part of the root universe. So this would only get incremented
194+
/// when we enter into a higher-ranked (`for<..>`) type or trait
195+
/// bound.
196+
universe: Cell<ty::UniverseIndex>,
186197
}
187198

188199
/// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
@@ -455,6 +466,7 @@ impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
455466
err_count_on_creation: tcx.sess.err_count(),
456467
in_snapshot: Cell::new(false),
457468
region_obligations: RefCell::new(vec![]),
469+
universe: Cell::new(ty::UniverseIndex::ROOT),
458470
}))
459471
}
460472
}
@@ -489,6 +501,7 @@ pub struct CombinedSnapshot<'a, 'tcx:'a> {
489501
float_snapshot: ut::Snapshot<ut::InPlace<ty::FloatVid>>,
490502
region_constraints_snapshot: RegionSnapshot,
491503
region_obligations_snapshot: usize,
504+
universe: ty::UniverseIndex,
492505
was_in_snapshot: bool,
493506
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
494507
}
@@ -618,6 +631,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
618631
float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
619632
region_constraints_snapshot: self.borrow_region_constraints().start_snapshot(),
620633
region_obligations_snapshot: self.region_obligations.borrow().len(),
634+
universe: self.universe(),
621635
was_in_snapshot: in_snapshot,
622636
// Borrow tables "in progress" (i.e. during typeck)
623637
// to ban writes from within a snapshot to them.
@@ -635,10 +649,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
635649
float_snapshot,
636650
region_constraints_snapshot,
637651
region_obligations_snapshot,
652+
universe,
638653
was_in_snapshot,
639654
_in_progress_tables } = snapshot;
640655

641656
self.in_snapshot.set(was_in_snapshot);
657+
self.universe.set(universe);
642658

643659
self.projection_cache
644660
.borrow_mut()
@@ -667,6 +683,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
667683
float_snapshot,
668684
region_constraints_snapshot,
669685
region_obligations_snapshot: _,
686+
universe: _,
670687
was_in_snapshot,
671688
_in_progress_tables } = snapshot;
672689

@@ -811,7 +828,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
811828

812829
Some(self.commit_if_ok(|snapshot| {
813830
let (ty::SubtypePredicate { a_is_expected, a, b}, skol_map) =
814-
self.skolemize_late_bound_regions(predicate, snapshot);
831+
self.skolemize_late_bound_regions(predicate);
815832

816833
let cause_span = cause.span;
817834
let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
@@ -828,7 +845,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
828845
{
829846
self.commit_if_ok(|snapshot| {
830847
let (ty::OutlivesPredicate(r_a, r_b), skol_map) =
831-
self.skolemize_late_bound_regions(predicate, snapshot);
848+
self.skolemize_late_bound_regions(predicate);
832849
let origin =
833850
SubregionOrigin::from_obligation_cause(cause,
834851
|| RelateRegionParamBound(cause.span));
@@ -841,7 +858,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
841858
pub fn next_ty_var_id(&self, diverging: bool, origin: TypeVariableOrigin) -> TyVid {
842859
self.type_variables
843860
.borrow_mut()
844-
.new_var(diverging, origin)
861+
.new_var(self.universe(), diverging, origin)
845862
}
846863

847864
pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
@@ -872,12 +889,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
872889
/// during diagnostics / error-reporting.
873890
pub fn next_region_var(&self, origin: RegionVariableOrigin)
874891
-> ty::Region<'tcx> {
875-
self.tcx.mk_region(ty::ReVar(self.borrow_region_constraints().new_region_var(origin)))
892+
let region_var = self.borrow_region_constraints()
893+
.new_region_var(self.universe(), origin);
894+
self.tcx.mk_region(ty::ReVar(region_var))
876895
}
877896

878897
/// Number of region variables created so far.
879898
pub fn num_region_vars(&self) -> usize {
880-
self.borrow_region_constraints().var_origins().len()
899+
self.borrow_region_constraints().num_region_vars()
881900
}
882901

883902
/// Just a convenient wrapper of `next_region_var` for using during NLL.
@@ -909,7 +928,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
909928
-> Ty<'tcx> {
910929
let ty_var_id = self.type_variables
911930
.borrow_mut()
912-
.new_var(false,
931+
.new_var(self.universe(),
932+
false,
913933
TypeVariableOrigin::TypeParameterDefinition(span, def.name));
914934

915935
self.tcx.mk_var(ty_var_id)
@@ -1004,12 +1024,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10041024
region_context,
10051025
region_map,
10061026
outlives_env.free_region_map());
1007-
let (var_origins, data) = self.region_constraints.borrow_mut()
1027+
let (var_infos, data) = self.region_constraints.borrow_mut()
10081028
.take()
10091029
.expect("regions already resolved")
1010-
.into_origins_and_data();
1030+
.into_infos_and_data();
10111031
let (lexical_region_resolutions, errors) =
1012-
lexical_region_resolve::resolve(region_rels, var_origins, data);
1032+
lexical_region_resolve::resolve(region_rels, var_infos, data);
10131033

10141034
let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
10151035
assert!(old_value.is_none());
@@ -1057,13 +1077,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10571077
/// hence that `resolve_regions_and_report_errors` can never be
10581078
/// called. This is used only during NLL processing to "hand off" ownership
10591079
/// of the set of region vairables into the NLL region context.
1060-
pub fn take_region_var_origins(&self) -> VarOrigins {
1061-
let (var_origins, data) = self.region_constraints.borrow_mut()
1080+
pub fn take_region_var_origins(&self) -> VarInfos {
1081+
let (var_infos, data) = self.region_constraints.borrow_mut()
10621082
.take()
10631083
.expect("regions already resolved")
1064-
.into_origins_and_data();
1084+
.into_infos_and_data();
10651085
assert!(data.is_empty());
1066-
var_origins
1086+
var_infos
10671087
}
10681088

10691089
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
@@ -1356,6 +1376,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13561376
self.evaluation_cache.clear();
13571377
self.projection_cache.borrow_mut().clear();
13581378
}
1379+
1380+
fn universe(&self) -> ty::UniverseIndex {
1381+
self.universe.get()
1382+
}
13591383
}
13601384

13611385
impl<'a, 'gcx, 'tcx> TypeTrace<'tcx> {

0 commit comments

Comments
 (0)