use super::sub::Sub;
use super::InferCtxt;
use super::{MiscVariable, TypeTrace};
-use super::type_variable::{RelationDir, BiTo, EqTo, SubtypeOf, SupertypeOf};
use ty::{IntType, UintType};
use ty::{self, Ty, TyCtxt};
pub obligations: PredicateObligations<'tcx>,
}
+#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
+pub enum RelationDir {
+ SubtypeOf, SupertypeOf, EqTo
+}
+
impl<'infcx, 'gcx, 'tcx> InferCtxt<'infcx, 'gcx, 'tcx> {
pub fn super_combine_tys<R>(&self,
relation: &mut R,
a_is_expected: bool)
-> RelateResult<'tcx, ()>
{
+ use self::RelationDir::*;
+
// We use SmallVector here instead of Vec because this code is hot and
// it's rare that the stack length exceeds 1.
let mut stack = SmallVector::new();
// Generalize type if necessary.
let generalized_ty = match dir {
EqTo => self.generalize(a_ty, b_vid, false),
- BiTo | SupertypeOf | SubtypeOf => self.generalize(a_ty, b_vid, true),
+ SupertypeOf | SubtypeOf => self.generalize(a_ty, b_vid, true),
}?;
debug!("instantiate(a_ty={:?}, dir={:?}, \
b_vid={:?}, generalized_ty={:?})",
generalized_ty);
self.infcx.type_variables
.borrow_mut()
- .instantiate_and_push(
- b_vid, generalized_ty, &mut stack);
+ .instantiate(b_vid, generalized_ty);
generalized_ty
}
};
// to associate causes/spans with each of the relations in
// the stack to get this right.
match dir {
- BiTo => Ok(a_ty),
EqTo => self.equate(a_is_expected).relate(&a_ty, &b_ty),
SubtypeOf => self.sub(a_is_expected).relate(&a_ty, &b_ty),
SupertypeOf => self.sub(a_is_expected).relate_with_variance(
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-pub use self::RelationDir::*;
use self::TypeVariableValue::*;
-use self::UndoEntry::*;
use hir::def_id::{DefId};
-use syntax::util::small_vector::SmallVector;
use syntax::ast;
use syntax_pos::Span;
use ty::{self, Ty};
enum TypeVariableValue<'tcx> {
Known(Ty<'tcx>),
Bounded {
- relations: Vec<Relation>,
default: Option<Default<'tcx>>
}
}
eq_snapshot: ut::Snapshot<ty::TyVid>,
}
-enum UndoEntry<'tcx> {
- // The type of the var was specified.
- SpecifyVar(ty::TyVid, Vec<Relation>, Option<Default<'tcx>>),
- Relate(ty::TyVid, ty::TyVid),
- RelateRange(ty::TyVid, usize),
+struct Instantiate<'tcx> {
+ vid: ty::TyVid,
+ default: Option<Default<'tcx>>,
}
struct Delegate<'tcx>(PhantomData<&'tcx ()>);
-type Relation = (RelationDir, ty::TyVid);
-
-#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
-pub enum RelationDir {
- SubtypeOf, SupertypeOf, EqTo, BiTo
-}
-
-impl RelationDir {
- fn opposite(self) -> RelationDir {
- match self {
- SubtypeOf => SupertypeOf,
- SupertypeOf => SubtypeOf,
- EqTo => EqTo,
- BiTo => BiTo,
- }
- }
-}
-
impl<'tcx> TypeVariableTable<'tcx> {
pub fn new() -> TypeVariableTable<'tcx> {
TypeVariableTable {
}
}
- fn relations<'a>(&'a mut self, a: ty::TyVid) -> &'a mut Vec<Relation> {
- relations(self.values.get_mut(a.index as usize))
- }
-
pub fn default(&self, vid: ty::TyVid) -> Option<Default<'tcx>> {
match &self.values.get(vid.index as usize).value {
&Known(_) => None,
&self.values.get(vid.index as usize).origin
}
- /// Records that `a <: b`, `a :> b`, or `a == b`, depending on `dir`.
+ /// Records that `a == b`, depending on `dir`.
///
/// Precondition: neither `a` nor `b` are known.
- pub fn relate_vars(&mut self, a: ty::TyVid, dir: RelationDir, b: ty::TyVid) {
- let a = self.root_var(a);
- let b = self.root_var(b);
- if a != b {
- if dir == EqTo {
- // a and b must be equal which we mark in the unification table
- let root = self.eq_relations.union(a, b);
- // In addition to being equal, all relations from the variable which is no longer
- // the root must be added to the root so they are not forgotten as the other
- // variable should no longer be referenced (other than to get the root)
- let other = if a == root { b } else { a };
- let count = {
- let (relations, root_relations) = if other.index < root.index {
- let (pre, post) = self.values.split_at_mut(root.index as usize);
- (relations(&mut pre[other.index as usize]), relations(&mut post[0]))
- } else {
- let (pre, post) = self.values.split_at_mut(other.index as usize);
- (relations(&mut post[0]), relations(&mut pre[root.index as usize]))
- };
- root_relations.extend_from_slice(relations);
- relations.len()
- };
- self.values.record(RelateRange(root, count));
- } else {
- self.relations(a).push((dir, b));
- self.relations(b).push((dir.opposite(), a));
- self.values.record(Relate(a, b));
- }
- }
+ pub fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
+ debug_assert!(self.probe(a).is_none());
+ debug_assert!(self.probe(b).is_none());
+ self.eq_relations.union(a, b);
}
- /// Instantiates `vid` with the type `ty` and then pushes an entry onto `stack` for each of the
- /// relations of `vid` to other variables. The relations will have the form `(ty, dir, vid1)`
- /// where `vid1` is some other variable id.
+ /// Instantiates `vid` with the type `ty`.
///
/// Precondition: `vid` must be a root in the unification table
- pub fn instantiate_and_push(
- &mut self,
- vid: ty::TyVid,
- ty: Ty<'tcx>,
- stack: &mut SmallVector<(Ty<'tcx>, RelationDir, ty::TyVid)>)
- {
+ /// and has not previously been instantiated.
+ pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
debug_assert!(self.root_var(vid) == vid);
- let old_value = {
- let value_ptr = &mut self.values.get_mut(vid.index as usize).value;
- mem::replace(value_ptr, Known(ty))
- };
+ debug_assert!(self.probe(vid).is_none());
- let (relations, default) = match old_value {
- Bounded { relations, default } => (relations, default),
- Known(_) => bug!("Asked to instantiate variable that is \
- already instantiated")
+ let old_value = {
+ let vid_data = &mut self.values[vid.index as usize];
+ mem::replace(&mut vid_data.value, TypeVariableValue::Known(ty))
};
- for &(dir, vid) in &relations {
- stack.push((ty, dir, vid));
+ match old_value {
+ TypeVariableValue::Bounded { default } => {
+ self.values.record(Instantiate { vid: vid, default: default });
+ }
+ TypeVariableValue::Known(old_ty) => {
+ bug!("instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}",
+ vid, ty, old_ty)
+ }
}
-
- self.values.record(SpecifyVar(vid, relations, default));
}
pub fn new_var(&mut self,
debug!("new_var(diverging={:?}, origin={:?})", diverging, origin);
self.eq_relations.new_key(());
let index = self.values.push(TypeVariableData {
- value: Bounded { relations: vec![], default: default },
+ value: Bounded { default: default },
origin: origin,
diverging: diverging
});
debug!("NewElem({}) new_elem_threshold={}", index, new_elem_threshold);
}
- sv::UndoLog::Other(SpecifyVar(vid, ..)) => {
+ sv::UndoLog::Other(Instantiate { vid, .. }) => {
if vid.index < new_elem_threshold {
// quick check to see if this variable was
// created since the snapshot started or not.
impl<'tcx> sv::SnapshotVecDelegate for Delegate<'tcx> {
type Value = TypeVariableData<'tcx>;
- type Undo = UndoEntry<'tcx>;
-
- fn reverse(values: &mut Vec<TypeVariableData<'tcx>>, action: UndoEntry<'tcx>) {
- match action {
- SpecifyVar(vid, relations, default) => {
- values[vid.index as usize].value = Bounded {
- relations: relations,
- default: default
- };
- }
+ type Undo = Instantiate<'tcx>;
- Relate(a, b) => {
- relations(&mut (*values)[a.index as usize]).pop();
- relations(&mut (*values)[b.index as usize]).pop();
- }
-
- RelateRange(i, n) => {
- let relations = relations(&mut (*values)[i.index as usize]);
- for _ in 0..n {
- relations.pop();
- }
- }
- }
- }
-}
-
-fn relations<'a>(v: &'a mut TypeVariableData) -> &'a mut Vec<Relation> {
- match v.value {
- Known(_) => bug!("var_sub_var: variable is known"),
- Bounded { ref mut relations, .. } => relations
+ fn reverse(values: &mut Vec<TypeVariableData<'tcx>>, action: Instantiate<'tcx>) {
+ let Instantiate { vid, default } = action;
+ values[vid.index as usize].value = Bounded {
+ default: default
+ };
}
}