Skip to content

Commit d4df52c

Browse files
nikomatsakissgrif
authored andcommitted
introduce UniverseIndex into ParamEnv
Always using root environment for now.
1 parent e7efce2 commit d4df52c

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

src/librustc/ich/impls_ty.rs

+10
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ for ty::steal::Steal<T>
870870

871871
impl_stable_hash_for!(struct ty::ParamEnv<'tcx> {
872872
caller_bounds,
873+
universe,
873874
reveal
874875
});
875876

@@ -1039,3 +1040,12 @@ for traits::VtableGeneratorData<'gcx, N> where N: HashStable<StableHashingContex
10391040
nested.hash_stable(hcx, hasher);
10401041
}
10411042
}
1043+
1044+
impl<'gcx> HashStable<StableHashingContext<'gcx>>
1045+
for ty::UniverseIndex {
1046+
fn hash_stable<W: StableHasherResult>(&self,
1047+
hcx: &mut StableHashingContext<'gcx>,
1048+
hasher: &mut StableHasher<W>) {
1049+
self.depth().hash_stable(hcx, hasher);
1050+
}
1051+
}

src/librustc/traits/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
546546
predicates);
547547

548548
let elaborated_env = ty::ParamEnv::new(tcx.intern_predicates(&predicates),
549-
unnormalized_env.reveal);
549+
unnormalized_env.reveal,
550+
unnormalized_env.universe);
550551

551552
tcx.infer_ctxt().enter(|infcx| {
552553
// FIXME. We should really... do something with these region
@@ -620,7 +621,9 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
620621
debug!("normalize_param_env_or_error: resolved predicates={:?}",
621622
predicates);
622623

623-
ty::ParamEnv::new(tcx.intern_predicates(&predicates), unnormalized_env.reveal)
624+
ty::ParamEnv::new(tcx.intern_predicates(&predicates),
625+
unnormalized_env.reveal,
626+
unnormalized_env.universe)
624627
})
625628
}
626629

src/librustc/ty/mod.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -1348,9 +1348,7 @@ pub struct UniverseIndex(u32);
13481348
impl UniverseIndex {
13491349
/// The root universe, where things that the user defined are
13501350
/// visible.
1351-
pub fn root() -> UniverseIndex {
1352-
UniverseIndex(0)
1353-
}
1351+
pub const ROOT: UniverseIndex = UniverseIndex(0);
13541352

13551353
/// A "subuniverse" corresponds to being inside a `forall` quantifier.
13561354
/// So, for example, suppose we have this type in universe `U`:
@@ -1366,6 +1364,13 @@ impl UniverseIndex {
13661364
pub fn subuniverse(self) -> UniverseIndex {
13671365
UniverseIndex(self.0 + 1)
13681366
}
1367+
1368+
/// Gets the "depth" of this universe in the universe tree. This
1369+
/// is not really useful except for e.g. the `HashStable`
1370+
/// implementation
1371+
pub fn depth(&self) -> u32 {
1372+
self.0
1373+
}
13691374
}
13701375

13711376
/// When type checking, we use the `ParamEnv` to track
@@ -1382,6 +1387,17 @@ pub struct ParamEnv<'tcx> {
13821387
/// want `Reveal::All` -- note that this is always paired with an
13831388
/// empty environment. To get that, use `ParamEnv::reveal()`.
13841389
pub reveal: traits::Reveal,
1390+
1391+
/// What is the innermost universe we have created? Starts out as
1392+
/// `UniverseIndex::root()` but grows from there as we enter
1393+
/// universal quantifiers.
1394+
///
1395+
/// NB: At present, we exclude the universal quantifiers on the
1396+
/// item we are type-checking, and just consider those names as
1397+
/// part of the root universe. So this would only get incremented
1398+
/// when we enter into a higher-ranked (`for<..>`) type or trait
1399+
/// bound.
1400+
pub universe: UniverseIndex,
13851401
}
13861402

13871403
impl<'tcx> ParamEnv<'tcx> {
@@ -2657,7 +2673,8 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26572673
// sure that this will succeed without errors anyway.
26582674

26592675
let unnormalized_env = ty::ParamEnv::new(tcx.intern_predicates(&predicates),
2660-
traits::Reveal::UserFacing);
2676+
traits::Reveal::UserFacing,
2677+
ty::UniverseIndex::ROOT);
26612678

26622679
let body_id = tcx.hir.as_local_node_id(def_id).map_or(DUMMY_NODE_ID, |id| {
26632680
tcx.hir.maybe_body_owned_by(id).map_or(id, |body| body.node_id)

src/librustc/ty/structural_impls.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
405405
tcx.lift(&self.caller_bounds).map(|caller_bounds| {
406406
ty::ParamEnv {
407407
reveal: self.reveal,
408+
universe: self.universe,
408409
caller_bounds,
409410
}
410411
})
@@ -733,8 +734,29 @@ impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
733734
}
734735
}
735736

736-
BraceStructTypeFoldableImpl! {
737-
impl<'tcx> TypeFoldable<'tcx> for ty::ParamEnv<'tcx> { reveal, caller_bounds }
737+
impl<'tcx> TypeFoldable<'tcx> for ty::ParamEnv<'tcx> {
738+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
739+
ty::ParamEnv {
740+
reveal: self.reveal,
741+
caller_bounds: self.caller_bounds.fold_with(folder),
742+
universe: self.universe.fold_with(folder),
743+
}
744+
}
745+
746+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
747+
let &ty::ParamEnv { reveal: _, ref universe, ref caller_bounds } = self;
748+
universe.super_visit_with(visitor) || caller_bounds.super_visit_with(visitor)
749+
}
750+
}
751+
752+
impl<'tcx> TypeFoldable<'tcx> for ty::UniverseIndex {
753+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
754+
*self
755+
}
756+
757+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
758+
false
759+
}
738760
}
739761

740762
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {

src/librustc/ty/util.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,15 @@ impl<'tcx> ty::ParamEnv<'tcx> {
153153
/// Construct a trait environment suitable for contexts where
154154
/// there are no where clauses in scope.
155155
pub fn empty(reveal: Reveal) -> Self {
156-
Self::new(ty::Slice::empty(), reveal)
156+
Self::new(ty::Slice::empty(), reveal, ty::UniverseIndex::ROOT)
157157
}
158158

159159
/// Construct a trait environment with the given set of predicates.
160160
pub fn new(caller_bounds: &'tcx ty::Slice<ty::Predicate<'tcx>>,
161-
reveal: Reveal)
161+
reveal: Reveal,
162+
universe: ty::UniverseIndex)
162163
-> Self {
163-
ty::ParamEnv { caller_bounds, reveal }
164+
ty::ParamEnv { caller_bounds, reveal, universe }
164165
}
165166

166167
/// Returns a new parameter environment with the same clauses, but

src/librustc_typeck/check/compare_method.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
218218
// the new hybrid bounds we computed.
219219
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_node_id);
220220
let param_env = ty::ParamEnv::new(tcx.intern_predicates(&hybrid_preds.predicates),
221-
Reveal::UserFacing);
221+
Reveal::UserFacing,
222+
ty::UniverseIndex::ROOT);
222223
let param_env = traits::normalize_param_env_or_error(tcx,
223224
impl_m.def_id,
224225
param_env,

0 commit comments

Comments
 (0)