1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
pub use self::RelationDir::*;
use self::TypeVariableValue::*;
use self::UndoEntry::*;
use middle::ty::{self, Ty};
use std::cmp::min;
use std::marker::PhantomData;
use std::mem;
use std::u32;
use util::snapshot_vec as sv;
pub struct TypeVariableTable<'tcx> {
values: sv::SnapshotVec<Delegate<'tcx>>,
}
struct TypeVariableData<'tcx> {
value: TypeVariableValue<'tcx>,
diverging: bool
}
enum TypeVariableValue<'tcx> {
Known(Ty<'tcx>),
Bounded(Vec<Relation>),
}
pub struct Snapshot {
snapshot: sv::Snapshot
}
enum UndoEntry {
SpecifyVar(ty::TyVid, Vec<Relation>),
Relate(ty::TyVid, ty::TyVid),
}
struct Delegate<'tcx>(PhantomData<&'tcx ()>);
type Relation = (RelationDir, ty::TyVid);
#[derive(Copy, Clone, PartialEq, 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 { values: sv::SnapshotVec::new(Delegate(PhantomData)) }
}
fn relations<'a>(&'a mut self, a: ty::TyVid) -> &'a mut Vec<Relation> {
relations(self.values.get_mut(a.index as usize))
}
pub fn var_diverges<'a>(&'a self, vid: ty::TyVid) -> bool {
self.values.get(vid.index as usize).diverging
}
pub fn relate_vars(&mut self, a: ty::TyVid, dir: RelationDir, b: ty::TyVid) {
if a != b {
self.relations(a).push((dir, b));
self.relations(b).push((dir.opposite(), a));
self.values.record(Relate(a, b));
}
}
pub fn instantiate_and_push(
&mut self,
vid: ty::TyVid,
ty: Ty<'tcx>,
stack: &mut Vec<(Ty<'tcx>, RelationDir, ty::TyVid)>)
{
let old_value = {
let value_ptr = &mut self.values.get_mut(vid.index as usize).value;
mem::replace(value_ptr, Known(ty))
};
let relations = match old_value {
Bounded(b) => b,
Known(_) => panic!("Asked to instantiate variable that is \
already instantiated")
};
for &(dir, vid) in &relations {
stack.push((ty, dir, vid));
}
self.values.record(SpecifyVar(vid, relations));
}
pub fn new_var(&mut self, diverging: bool) -> ty::TyVid {
let index = self.values.push(TypeVariableData {
value: Bounded(vec![]),
diverging: diverging
});
ty::TyVid { index: index as u32 }
}
pub fn probe(&self, vid: ty::TyVid) -> Option<Ty<'tcx>> {
match self.values.get(vid.index as usize).value {
Bounded(..) => None,
Known(t) => Some(t)
}
}
pub fn replace_if_possible(&self, t: Ty<'tcx>) -> Ty<'tcx> {
match t.sty {
ty::ty_infer(ty::TyVar(v)) => {
match self.probe(v) {
None => t,
Some(u) => u
}
}
_ => t,
}
}
pub fn snapshot(&mut self) -> Snapshot {
Snapshot { snapshot: self.values.start_snapshot() }
}
pub fn rollback_to(&mut self, s: Snapshot) {
self.values.rollback_to(s.snapshot);
}
pub fn commit(&mut self, s: Snapshot) {
self.values.commit(s.snapshot);
}
pub fn types_escaping_snapshot(&self, s: &Snapshot) -> Vec<Ty<'tcx>> {
let mut new_elem_threshold = u32::MAX;
let mut escaping_types = Vec::new();
let actions_since_snapshot = self.values.actions_since_snapshot(&s.snapshot);
debug!("actions_since_snapshot.len() = {}", actions_since_snapshot.len());
for action in actions_since_snapshot {
match *action {
sv::UndoLog::NewElem(index) => {
new_elem_threshold = min(new_elem_threshold, index as u32);
debug!("NewElem({}) new_elem_threshold={}", index, new_elem_threshold);
}
sv::UndoLog::Other(SpecifyVar(vid, _)) => {
if vid.index < new_elem_threshold {
let escaping_type = self.probe(vid).unwrap();
escaping_types.push(escaping_type);
}
debug!("SpecifyVar({:?}) new_elem_threshold={}", vid, new_elem_threshold);
}
_ => { }
}
}
escaping_types
}
}
impl<'tcx> sv::SnapshotVecDelegate for Delegate<'tcx> {
type Value = TypeVariableData<'tcx>;
type Undo = UndoEntry;
fn reverse(&mut self,
values: &mut Vec<TypeVariableData<'tcx>>,
action: UndoEntry) {
match action {
SpecifyVar(vid, relations) => {
values[vid.index as usize].value = Bounded(relations);
}
Relate(a, b) => {
relations(&mut (*values)[a.index as usize]).pop();
relations(&mut (*values)[b.index as usize]).pop();
}
}
}
}
fn relations<'a>(v: &'a mut TypeVariableData) -> &'a mut Vec<Relation> {
match v.value {
Known(_) => panic!("var_sub_var: variable is known"),
Bounded(ref mut relations) => relations
}
}