Skip to content

Commit

Permalink
Auto merge of #49313 - sgrif:sg-revert-stuff, r=nikomatsakis
Browse files Browse the repository at this point in the history
Remove universes from `ty::ParamEnv`

This change was never meant to land. #48407 takes an alternate approach. However, that PR is now blocked on some issues with canonicalization, and rebasing these reverts gets harder each time, so let's just get this bit out of the way now.

r? @nikomatsakis
  • Loading branch information
bors committed Mar 29, 2018
2 parents 3615093 + dd60bea commit dca1470
Show file tree
Hide file tree
Showing 32 changed files with 97 additions and 244 deletions.
10 changes: 0 additions & 10 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,6 @@ for ty::steal::Steal<T>

impl_stable_hash_for!(struct ty::ParamEnv<'tcx> {
caller_bounds,
universe,
reveal
});

Expand Down Expand Up @@ -1282,15 +1281,6 @@ for traits::VtableGeneratorData<'gcx, N> where N: HashStable<StableHashingContex
}
}

impl<'a> HashStable<StableHashingContext<'a>>
for ty::UniverseIndex {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
self.depth().hash_stable(hcx, hasher);
}
}

impl_stable_hash_for!(
impl<'tcx, V> for struct infer::canonical::Canonical<'tcx, V> {
variables, value
Expand Down
5 changes: 1 addition & 4 deletions src/librustc/infer/anon_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,7 @@ impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
return anon_defn.concrete_ty;
}
let span = tcx.def_span(def_id);
let ty_var = infcx.next_ty_var(
ty::UniverseIndex::ROOT,
TypeVariableOrigin::TypeInference(span),
);
let ty_var = infcx.next_ty_var(TypeVariableOrigin::TypeInference(span));

let predicates_of = tcx.predicates_of(def_id);
let bounds = predicates_of.instantiate(tcx, substs);
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/infer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,6 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
let ty = match ty_kind {
CanonicalTyVarKind::General => {
self.next_ty_var(
// FIXME(#48696) this handling of universes is not right.
ty::UniverseIndex::ROOT,
TypeVariableOrigin::MiscVariable(span),
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
drop(variables);
self.relate(&u, &u)
}
TypeVariableValue::Unknown { universe } => {
TypeVariableValue::Unknown { .. } => {
match self.ambient_variance {
// Invariant: no need to make a fresh type variable.
ty::Invariant => return Ok(t),
Expand All @@ -424,7 +424,7 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
}

let origin = *variables.var_origin(vid);
let new_var_id = variables.new_var(universe, false, origin);
let new_var_id = variables.new_var(false, origin);
let u = self.tcx().mk_var(new_var_id);
debug!("generalize: replacing original vid={:?} with new={:?}",
vid, u);
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/infer/fudge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionFudger<'a, 'gcx, 'tcx> {
// This variable was created during the
// fudging. Recreate it with a fresh variable
// here.
//
// The ROOT universe is fine because we only
// ever invoke this routine at the
// "item-level" of inference.
self.infcx.next_ty_var(ty::UniverseIndex::ROOT, origin)
self.infcx.next_ty_var(origin)
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/librustc/infer/lattice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,13 @@ pub fn super_lattice_tys<'a, 'gcx, 'tcx, L>(this: &mut L,
// is (e.g.) `Box<i32>`. A more obvious solution might be to
// iterate on the subtype obligations that are returned, but I
// think this suffices. -nmatsakis
(&ty::TyInfer(TyVar(a_vid)), _) => {
let universe = infcx.type_variables.borrow_mut().probe(a_vid).universe().unwrap();
let v = infcx.next_ty_var(universe,
TypeVariableOrigin::LatticeVariable(this.cause().span));
(&ty::TyInfer(TyVar(..)), _) => {
let v = infcx.next_ty_var(TypeVariableOrigin::LatticeVariable(this.cause().span));
this.relate_bound(v, b, a)?;
Ok(v)
}
(_, &ty::TyInfer(TyVar(b_vid))) => {
let universe = infcx.type_variables.borrow_mut().probe(b_vid).universe().unwrap();
let v = infcx.next_ty_var(universe,
TypeVariableOrigin::LatticeVariable(this.cause().span));
(_, &ty::TyInfer(TyVar(..))) => {
let v = infcx.next_ty_var(TypeVariableOrigin::LatticeVariable(this.cause().span));
this.relate_bound(v, a, b)?;
Ok(v)
}
Expand Down
26 changes: 8 additions & 18 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,25 +837,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
})
}

pub fn next_ty_var_id(&self,
universe: ty::UniverseIndex,
diverging: bool,
origin: TypeVariableOrigin)
-> TyVid {
pub fn next_ty_var_id(&self, diverging: bool, origin: TypeVariableOrigin) -> TyVid {
self.type_variables
.borrow_mut()
.new_var(universe, diverging, origin)
.new_var(diverging, origin)
}

pub fn next_ty_var(&self, universe: ty::UniverseIndex, origin: TypeVariableOrigin) -> Ty<'tcx> {
self.tcx.mk_var(self.next_ty_var_id(universe, false, origin))
pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
self.tcx.mk_var(self.next_ty_var_id(false, origin))
}

pub fn next_diverging_ty_var(&self,
universe: ty::UniverseIndex,
origin: TypeVariableOrigin)
-> Ty<'tcx> {
self.tcx.mk_var(self.next_ty_var_id(universe, true, origin))
pub fn next_diverging_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
self.tcx.mk_var(self.next_ty_var_id(true, origin))
}

pub fn next_int_var_id(&self) -> IntVid {
Expand Down Expand Up @@ -910,14 +903,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
/// use an inference variable for `C` with `[T, U]`
/// as the substitutions for the default, `(T, U)`.
pub fn type_var_for_def(&self,
universe: ty::UniverseIndex,
span: Span,
def: &ty::TypeParameterDef)
-> Ty<'tcx> {
let ty_var_id = self.type_variables
.borrow_mut()
.new_var(universe,
false,
.new_var(false,
TypeVariableOrigin::TypeParameterDefinition(span, def.name));

self.tcx.mk_var(ty_var_id)
Expand All @@ -926,14 +917,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
/// Given a set of generics defined on a type or impl, returns a substitution mapping each
/// type/region parameter to a fresh inference variable.
pub fn fresh_substs_for_item(&self,
universe: ty::UniverseIndex,
span: Span,
def_id: DefId)
-> &'tcx Substs<'tcx> {
Substs::for_item(self.tcx, def_id, |def, _| {
self.region_var_for_def(span, def)
}, |def, _| {
self.type_var_for_def(universe, span, def)
self.type_var_for_def(span, def)
})
}

Expand Down
36 changes: 18 additions & 18 deletions src/librustc/infer/region_constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct RegionConstraintCollector<'tcx> {
glbs: CombineMap<'tcx>,

/// Number of skolemized variables currently active.
skolemization_count: ty::UniverseIndex,
skolemization_count: u32,

/// Global counter used during the GLB algorithm to create unique
/// names for fresh bound regions
Expand Down Expand Up @@ -233,7 +233,7 @@ type CombineMap<'tcx> = FxHashMap<TwoRegions<'tcx>, RegionVid>;
pub struct RegionSnapshot {
length: usize,
region_snapshot: ut::Snapshot<ut::InPlace<ty::RegionVid>>,
skolemization_count: ty::UniverseIndex,
skolemization_count: u32,
}

/// When working with skolemized regions, we often wish to find all of
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
data: RegionConstraintData::default(),
lubs: FxHashMap(),
glbs: FxHashMap(),
skolemization_count: ty::UniverseIndex::ROOT,
skolemization_count: 0,
bound_count: 0,
undo_log: Vec::new(),
unification_table: ut::UnificationTable::new(),
Expand Down Expand Up @@ -329,7 +329,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
unification_table,
} = self;

assert_eq!(skolemization_count.as_usize(), 0);
assert_eq!(*skolemization_count, 0);

// Clear the tables of (lubs, glbs), so that we will create
// fresh regions if we do a LUB operation. As it happens,
Expand Down Expand Up @@ -375,7 +375,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
assert!(
self.skolemization_count == snapshot.skolemization_count,
"failed to pop skolemized regions: {:?} now vs {:?} at start",
"failed to pop skolemized regions: {} now vs {} at start",
self.skolemization_count,
snapshot.skolemization_count
);
Expand Down Expand Up @@ -485,9 +485,9 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
assert!(self.in_snapshot());
assert!(self.undo_log[snapshot.length] == OpenSnapshot);

let universe = self.skolemization_count.subuniverse();
self.skolemization_count = universe;
tcx.mk_region(ReSkolemized(universe, br))
let sc = self.skolemization_count;
self.skolemization_count = sc + 1;
tcx.mk_region(ReSkolemized(ty::SkolemizedRegionVid { index: sc }, br))
}

/// Removes all the edges to/from the skolemized regions that are
Expand All @@ -505,34 +505,34 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
assert!(self.in_snapshot());
assert!(self.undo_log[snapshot.length] == OpenSnapshot);
assert!(
self.skolemization_count.as_usize() >= skols.len(),
self.skolemization_count as usize >= skols.len(),
"popping more skolemized variables than actually exist, \
sc now = {}, skols.len = {}",
self.skolemization_count.as_usize(),
self.skolemization_count,
skols.len()
);

let last_to_pop = self.skolemization_count.subuniverse();
let first_to_pop = ty::UniverseIndex::from(last_to_pop.as_u32() - (skols.len() as u32));
let last_to_pop = self.skolemization_count;
let first_to_pop = last_to_pop - (skols.len() as u32);

assert!(
first_to_pop >= snapshot.skolemization_count,
"popping more regions than snapshot contains, \
sc now = {:?}, sc then = {:?}, skols.len = {}",
sc now = {}, sc then = {}, skols.len = {}",
self.skolemization_count,
snapshot.skolemization_count,
skols.len()
);
debug_assert! {
skols.iter()
.all(|&k| match *k {
ty::ReSkolemized(universe, _) =>
universe >= first_to_pop &&
universe < last_to_pop,
ty::ReSkolemized(index, _) =>
index.index >= first_to_pop &&
index.index < last_to_pop,
_ =>
false
}),
"invalid skolemization keys or keys out of range ({:?}..{:?}): {:?}",
"invalid skolemization keys or keys out of range ({}..{}): {:?}",
snapshot.skolemization_count,
self.skolemization_count,
skols
Expand Down Expand Up @@ -867,7 +867,7 @@ impl fmt::Debug for RegionSnapshot {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"RegionSnapshot(length={},skolemization={:?})",
"RegionSnapshot(length={},skolemization={})",
self.length,
self.skolemization_count
)
Expand Down
29 changes: 4 additions & 25 deletions src/librustc/infer/type_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,17 @@ struct TypeVariableData {
#[derive(Copy, Clone, Debug)]
pub enum TypeVariableValue<'tcx> {
Known { value: Ty<'tcx> },
Unknown { universe: ty::UniverseIndex },
}

#[derive(Copy, Clone, Debug)]
pub enum ProbeTyValue<'tcx> {
Ty(Ty<'tcx>),
Vid(ty::TyVid),
Unknown,
}

impl<'tcx> TypeVariableValue<'tcx> {
/// If this value is known, returns the type it is known to be.
/// Otherwise, `None`.
pub fn known(&self) -> Option<Ty<'tcx>> {
match *self {
TypeVariableValue::Unknown { .. } => None,
TypeVariableValue::Known { value } => Some(value),
}
}

/// If this value is unknown, returns the universe, otherwise `None`.
pub fn universe(&self) -> Option<ty::UniverseIndex> {
match *self {
TypeVariableValue::Unknown { universe } => Some(universe),
TypeVariableValue::Known { .. } => None,
}
}

pub fn is_unknown(&self) -> bool {
match *self {
TypeVariableValue::Unknown { .. } => true,
Expand Down Expand Up @@ -197,11 +181,10 @@ impl<'tcx> TypeVariableTable<'tcx> {
/// The code in this module doesn't care, but it can be useful
/// for improving error messages.
pub fn new_var(&mut self,
universe: ty::UniverseIndex,
diverging: bool,
origin: TypeVariableOrigin)
-> ty::TyVid {
let eq_key = self.eq_relations.new_key(TypeVariableValue::Unknown { universe });
let eq_key = self.eq_relations.new_key(TypeVariableValue::Unknown);

let sub_key = self.sub_relations.new_key(());
assert_eq!(eq_key.vid, sub_key);
Expand Down Expand Up @@ -453,12 +436,8 @@ impl<'tcx> ut::UnifyValue for TypeVariableValue<'tcx> {
(&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
(&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),

// If both sides are unknown, we need to pick the most restrictive universe.
(&TypeVariableValue::Unknown { universe: universe1 },
&TypeVariableValue::Unknown { universe: universe2 }) => {
let universe = cmp::min(universe1, universe2);
Ok(TypeVariableValue::Unknown { universe })
}
// If both sides are *unknown*, it hardly matters, does it?
(&TypeVariableValue::Unknown, &TypeVariableValue::Unknown) => Ok(*value1),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ fn with_fresh_ty_vars<'cx, 'gcx, 'tcx>(selcx: &mut SelectionContext<'cx, 'gcx, '
-> ty::ImplHeader<'tcx>
{
let tcx = selcx.tcx();
let impl_substs = selcx.infcx().fresh_substs_for_item(param_env.universe,
DUMMY_SP,
impl_def_id);
let impl_substs = selcx.infcx().fresh_substs_for_item(DUMMY_SP, impl_def_id);

let header = ty::ImplHeader {
impl_def_id,
Expand Down
17 changes: 4 additions & 13 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {

self.tcx.for_each_relevant_impl(
trait_ref.def_id, trait_self_ty, |def_id| {
let impl_substs = self.fresh_substs_for_item(param_env.universe,
obligation.cause.span,
def_id);
let impl_substs = self.fresh_substs_for_item(obligation.cause.span, def_id);
let impl_trait_ref = tcx
.impl_trait_ref(def_id)
.unwrap()
Expand Down Expand Up @@ -1285,7 +1283,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
-> bool {
struct ParamToVarFolder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
var_map: FxHashMap<Ty<'tcx>, Ty<'tcx>>
}

Expand All @@ -1295,14 +1292,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
if let ty::TyParam(ty::ParamTy {name, ..}) = ty.sty {
let infcx = self.infcx;
let param_env = self.param_env;
self.var_map
.entry(ty)
.or_insert_with(|| {
let origin = TypeVariableOrigin::TypeParameterDefinition(DUMMY_SP,
name);
infcx.next_ty_var(param_env.universe, origin)
})
self.var_map.entry(ty).or_insert_with(||
infcx.next_ty_var(
TypeVariableOrigin::TypeParameterDefinition(DUMMY_SP, name)))
} else {
ty.super_fold_with(self)
}
Expand All @@ -1314,7 +1306,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {

let cleaned_pred = pred.fold_with(&mut ParamToVarFolder {
infcx: self,
param_env,
var_map: FxHashMap()
});

Expand Down
Loading

0 comments on commit dca1470

Please sign in to comment.