1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 pub use self::RelationDir::*;
12 use self::TypeVariableValue::*;
13 use self::UndoEntry::*;
15 use middle::ty::{self, Ty};
17 use std::marker::PhantomData;
20 use rustc_data_structures::snapshot_vec as sv;
22 pub struct TypeVariableTable<'tcx> {
23 values: sv::SnapshotVec<Delegate<'tcx>>,
26 struct TypeVariableData<'tcx> {
27 value: TypeVariableValue<'tcx>,
31 enum TypeVariableValue<'tcx> {
34 relations: Vec<Relation>,
35 default: Option<Default<'tcx>>
39 // We will use this to store the required information to recapitulate what happened when
42 pub struct Default<'tcx> {
47 snapshot: sv::Snapshot
50 enum UndoEntry<'tcx> {
51 // The type of the var was specified.
52 SpecifyVar(ty::TyVid, Vec<Relation>, Option<Default<'tcx>>),
53 Relate(ty::TyVid, ty::TyVid),
56 struct Delegate<'tcx>(PhantomData<&'tcx ()>);
58 type Relation = (RelationDir, ty::TyVid);
60 #[derive(Copy, Clone, PartialEq, Debug)]
61 pub enum RelationDir {
62 SubtypeOf, SupertypeOf, EqTo, BiTo
66 fn opposite(self) -> RelationDir {
68 SubtypeOf => SupertypeOf,
69 SupertypeOf => SubtypeOf,
76 impl<'tcx> TypeVariableTable<'tcx> {
77 pub fn new() -> TypeVariableTable<'tcx> {
78 TypeVariableTable { values: sv::SnapshotVec::new() }
81 fn relations<'a>(&'a mut self, a: ty::TyVid) -> &'a mut Vec<Relation> {
82 relations(self.values.get_mut(a.index as usize))
85 pub fn default(&self, vid: ty::TyVid) -> Option<Default<'tcx>> {
86 match &self.values.get(vid.index as usize).value {
88 &Bounded { ref default, .. } => default.clone()
92 pub fn var_diverges<'a>(&'a self, vid: ty::TyVid) -> bool {
93 self.values.get(vid.index as usize).diverging
96 /// Records that `a <: b`, `a :> b`, or `a == b`, depending on `dir`.
98 /// Precondition: neither `a` nor `b` are known.
99 pub fn relate_vars(&mut self, a: ty::TyVid, dir: RelationDir, b: ty::TyVid) {
101 self.relations(a).push((dir, b));
102 self.relations(b).push((dir.opposite(), a));
103 self.values.record(Relate(a, b));
107 /// Instantiates `vid` with the type `ty` and then pushes an entry onto `stack` for each of the
108 /// relations of `vid` to other variables. The relations will have the form `(ty, dir, vid1)`
109 /// where `vid1` is some other variable id.
110 pub fn instantiate_and_push(
114 stack: &mut Vec<(Ty<'tcx>, RelationDir, ty::TyVid)>)
117 let value_ptr = &mut self.values.get_mut(vid.index as usize).value;
118 mem::replace(value_ptr, Known(ty))
121 let (relations, default) = match old_value {
122 Bounded { relations, default } => (relations, default),
123 Known(_) => panic!("Asked to instantiate variable that is \
124 already instantiated")
127 for &(dir, vid) in &relations {
128 stack.push((ty, dir, vid));
131 self.values.record(SpecifyVar(vid, relations, default));
134 pub fn new_var(&mut self,
136 default: Option<Default<'tcx>>) -> ty::TyVid {
137 let index = self.values.push(TypeVariableData {
138 value: Bounded { relations: vec![], default: default },
141 ty::TyVid { index: index as u32 }
144 pub fn probe(&self, vid: ty::TyVid) -> Option<Ty<'tcx>> {
145 match self.values.get(vid.index as usize).value {
146 Bounded { .. } => None,
151 pub fn replace_if_possible(&self, t: Ty<'tcx>) -> Ty<'tcx> {
153 ty::TyInfer(ty::TyVar(v)) => {
154 match self.probe(v) {
163 pub fn snapshot(&mut self) -> Snapshot {
164 Snapshot { snapshot: self.values.start_snapshot() }
167 pub fn rollback_to(&mut self, s: Snapshot) {
168 self.values.rollback_to(s.snapshot);
171 pub fn commit(&mut self, s: Snapshot) {
172 self.values.commit(s.snapshot);
175 pub fn types_escaping_snapshot(&self, s: &Snapshot) -> Vec<Ty<'tcx>> {
177 * Find the set of type variables that existed *before* `s`
178 * but which have only been unified since `s` started, and
179 * return the types with which they were unified. So if we had
180 * a type variable `V0`, then we started the snapshot, then we
181 * created a type variable `V1`, unifed `V0` with `T0`, and
182 * unified `V1` with `T1`, this function would return `{T0}`.
185 let mut new_elem_threshold = u32::MAX;
186 let mut escaping_types = Vec::new();
187 let actions_since_snapshot = self.values.actions_since_snapshot(&s.snapshot);
188 debug!("actions_since_snapshot.len() = {}", actions_since_snapshot.len());
189 for action in actions_since_snapshot {
191 sv::UndoLog::NewElem(index) => {
192 // if any new variables were created during the
193 // snapshot, remember the lower index (which will
194 // always be the first one we see). Note that this
195 // action must precede those variables being
197 new_elem_threshold = min(new_elem_threshold, index as u32);
198 debug!("NewElem({}) new_elem_threshold={}", index, new_elem_threshold);
201 sv::UndoLog::Other(SpecifyVar(vid, _, _)) => {
202 if vid.index < new_elem_threshold {
203 // quick check to see if this variable was
204 // created since the snapshot started or not.
205 let escaping_type = self.probe(vid).unwrap();
206 escaping_types.push(escaping_type);
208 debug!("SpecifyVar({:?}) new_elem_threshold={}", vid, new_elem_threshold);
218 pub fn unsolved_variables(&self) -> Vec<ty::TyVid> {
222 .filter_map(|(i, value)| match &value.value {
223 &TypeVariableValue::Known(_) => None,
224 &TypeVariableValue::Bounded { .. } => Some(ty::TyVid { index: i as u32 })
230 impl<'tcx> sv::SnapshotVecDelegate for Delegate<'tcx> {
231 type Value = TypeVariableData<'tcx>;
232 type Undo = UndoEntry<'tcx>;
234 fn reverse(values: &mut Vec<TypeVariableData<'tcx>>, action: UndoEntry<'tcx>) {
236 SpecifyVar(vid, relations, default) => {
237 values[vid.index as usize].value = Bounded {
238 relations: relations,
244 relations(&mut (*values)[a.index as usize]).pop();
245 relations(&mut (*values)[b.index as usize]).pop();
251 fn relations<'a>(v: &'a mut TypeVariableData) -> &'a mut Vec<Relation> {
253 Known(_) => panic!("var_sub_var: variable is known"),
254 Bounded { ref mut relations, .. } => relations