Skip to content

Commit ff17bd7

Browse files
committed
add universes to type inference variables
This gives each type inference variable a notion of universe but doesn't do anything with it. We can always get the "current universe" from infer_ctxt. This relies on the property of type variables that they can never interact with siblings.
1 parent 3d4791b commit ff17bd7

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/librustc/infer/combine.rs

Lines changed: 2 additions & 2 deletions
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/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
852852
pub fn next_ty_var_id(&self, diverging: bool, origin: TypeVariableOrigin) -> TyVid {
853853
self.type_variables
854854
.borrow_mut()
855-
.new_var(diverging, origin)
855+
.new_var(self.universe, diverging, origin)
856856
}
857857

858858
pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
@@ -920,7 +920,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
920920
-> Ty<'tcx> {
921921
let ty_var_id = self.type_variables
922922
.borrow_mut()
923-
.new_var(false,
923+
.new_var(self.universe,
924+
false,
924925
TypeVariableOrigin::TypeParameterDefinition(span, def.name));
925926

926927
self.tcx.mk_var(ty_var_id)

src/librustc/infer/type_variable.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ struct TypeVariableData {
7878
#[derive(Copy, Clone, Debug)]
7979
pub enum TypeVariableValue<'tcx> {
8080
Known { value: Ty<'tcx> },
81-
Unknown,
81+
Unknown { universe: ty::UniverseIndex },
8282
}
8383

8484
impl<'tcx> TypeVariableValue<'tcx> {
85+
/// If this value is known, returns the type it is known to be.
86+
/// Otherwise, `None`.
8587
pub fn known(&self) -> Option<Ty<'tcx>> {
8688
match *self {
8789
TypeVariableValue::Unknown { .. } => None,
@@ -181,10 +183,11 @@ impl<'tcx> TypeVariableTable<'tcx> {
181183
/// The code in this module doesn't care, but it can be useful
182184
/// for improving error messages.
183185
pub fn new_var(&mut self,
186+
universe: ty::UniverseIndex,
184187
diverging: bool,
185188
origin: TypeVariableOrigin)
186189
-> ty::TyVid {
187-
let eq_key = self.eq_relations.new_key(TypeVariableValue::Unknown);
190+
let eq_key = self.eq_relations.new_key(TypeVariableValue::Unknown { universe });
188191

189192
let sub_key = self.sub_relations.new_key(());
190193
assert_eq!(eq_key.vid, sub_key);
@@ -437,7 +440,11 @@ impl<'tcx> ut::UnifyValue for TypeVariableValue<'tcx> {
437440
(&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
438441

439442
// If both sides are *unknown*, it hardly matters, does it?
440-
(&TypeVariableValue::Unknown, &TypeVariableValue::Unknown) => Ok(*value1),
443+
(&TypeVariableValue::Unknown { universe: universe1 },
444+
&TypeVariableValue::Unknown { universe: universe2 }) => {
445+
let universe = cmp::min(universe1, universe2);
446+
Ok(TypeVariableValue::Unknown { universe })
447+
}
441448
}
442449
}
443450
}

0 commit comments

Comments
 (0)