Skip to content

Commit c7fe510

Browse files
nikomatsakissgrif
authored andcommitted
have probe() return TypeVariableValue
1 parent 4860579 commit c7fe510

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

src/librustc/infer/combine.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434

3535
use super::equate::Equate;
3636
use super::glb::Glb;
37+
use super::{InferCtxt, MiscVariable, TypeTrace};
3738
use super::lub::Lub;
3839
use super::sub::Sub;
39-
use super::InferCtxt;
40-
use super::{MiscVariable, TypeTrace};
40+
use super::type_variable::TypeVariableValue;
4141

4242
use hir::def_id::DefId;
4343
use ty::{IntType, UintType};
@@ -194,7 +194,7 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
194194
use self::RelationDir::*;
195195

196196
// Get the actual variable that b_vid has been inferred to
197-
debug_assert!(self.infcx.type_variables.borrow_mut().probe(b_vid).is_none());
197+
debug_assert!(self.infcx.type_variables.borrow_mut().probe(b_vid).is_unknown());
198198

199199
debug!("instantiate(a_ty={:?} dir={:?} b_vid={:?})", a_ty, dir, b_vid);
200200

@@ -403,11 +403,11 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
403403
return Err(TypeError::CyclicTy(self.root_ty));
404404
} else {
405405
match variables.probe(vid) {
406-
Some(u) => {
406+
TypeVariableValue::Known { value: u } => {
407407
drop(variables);
408408
self.relate(&u, &u)
409409
}
410-
None => {
410+
TypeVariableValue::Unknown { .. } => {
411411
match self.ambient_variance {
412412
// Invariant: no need to make a fresh type variable.
413413
ty::Invariant => return Ok(t),

src/librustc/infer/freshen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
133133

134134
match t.sty {
135135
ty::TyInfer(ty::TyVar(v)) => {
136-
let opt_ty = self.infcx.type_variables.borrow_mut().probe(v);
136+
let opt_ty = self.infcx.type_variables.borrow_mut().probe(v).known();
137137
self.freshen(
138138
opt_ty,
139139
ty::TyVar(v),

src/librustc/infer/fudge.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionFudger<'a, 'gcx, 'tcx> {
131131
// variables to their binding anyhow, we know
132132
// that it is unbound, so we can just return
133133
// it.
134-
debug_assert!(self.infcx.type_variables.borrow_mut().probe(vid).is_none());
134+
debug_assert!(self.infcx.type_variables.borrow_mut()
135+
.probe(vid)
136+
.is_unknown());
135137
ty
136138
}
137139

src/librustc/infer/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,9 +1259,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12591259
// so this recursion should always be of very limited
12601260
// depth.
12611261
self.type_variables.borrow_mut()
1262-
.probe(v)
1263-
.map(|t| self.shallow_resolve(t))
1264-
.unwrap_or(typ)
1262+
.probe(v)
1263+
.known()
1264+
.map(|t| self.shallow_resolve(t))
1265+
.unwrap_or(typ)
12651266
}
12661267

12671268
ty::TyInfer(ty::IntVar(v)) => {

src/librustc/infer/type_variable.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,28 @@ struct TypeVariableData {
7878
diverging: bool
7979
}
8080

81-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
82-
enum TypeVariableValue<'tcx> {
81+
#[derive(Copy, Clone, Debug)]
82+
pub enum TypeVariableValue<'tcx> {
8383
Known { value: Ty<'tcx> },
8484
Unknown,
8585
}
8686

87+
impl<'tcx> TypeVariableValue<'tcx> {
88+
pub fn known(&self) -> Option<Ty<'tcx>> {
89+
match *self {
90+
TypeVariableValue::Unknown { .. } => None,
91+
TypeVariableValue::Known { value } => Some(value),
92+
}
93+
}
94+
95+
pub fn is_unknown(&self) -> bool {
96+
match *self {
97+
TypeVariableValue::Unknown { .. } => true,
98+
TypeVariableValue::Known { .. } => false,
99+
}
100+
}
101+
}
102+
87103
pub struct Snapshot<'tcx> {
88104
/// number of variables at the time of the snapshot
89105
num_vars: usize,
@@ -124,8 +140,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
124140
///
125141
/// Precondition: neither `a` nor `b` are known.
126142
pub fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
127-
debug_assert!(self.probe(a).is_none());
128-
debug_assert!(self.probe(b).is_none());
143+
debug_assert!(self.probe(a).is_unknown());
144+
debug_assert!(self.probe(b).is_unknown());
129145
self.eq_relations.union(a, b);
130146
self.sub_relations.union(a, b);
131147
}
@@ -134,8 +150,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
134150
///
135151
/// Precondition: neither `a` nor `b` are known.
136152
pub fn sub(&mut self, a: ty::TyVid, b: ty::TyVid) {
137-
debug_assert!(self.probe(a).is_none());
138-
debug_assert!(self.probe(b).is_none());
153+
debug_assert!(self.probe(a).is_unknown());
154+
debug_assert!(self.probe(b).is_unknown());
139155
self.sub_relations.union(a, b);
140156
}
141157

@@ -144,8 +160,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
144160
/// Precondition: `vid` must not have been previously instantiated.
145161
pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
146162
let vid = self.root_var(vid);
147-
debug_assert!(self.probe(vid).is_none());
148-
debug_assert!(self.eq_relations.probe_value(vid) == TypeVariableValue::Unknown,
163+
debug_assert!(self.probe(vid).is_unknown());
164+
debug_assert!(self.eq_relations.probe_value(vid).is_unknown(),
149165
"instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}",
150166
vid, ty, self.eq_relations.probe_value(vid));
151167
self.eq_relations.union_value(vid, TypeVariableValue::Known { value: ty });
@@ -211,12 +227,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
211227

212228
/// Retrieves the type to which `vid` has been instantiated, if
213229
/// any.
214-
pub fn probe(&mut self, vid: ty::TyVid) -> Option<Ty<'tcx>> {
215-
let vid = self.root_var(vid);
216-
match self.eq_relations.probe_value(vid) {
217-
TypeVariableValue::Unknown => None,
218-
TypeVariableValue::Known { value } => Some(value)
219-
}
230+
pub fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
231+
self.eq_relations.probe_value(vid)
220232
}
221233

222234
/// If `t` is a type-inference variable, and it has been
@@ -226,8 +238,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
226238
match t.sty {
227239
ty::TyInfer(ty::TyVar(v)) => {
228240
match self.probe(v) {
229-
None => t,
230-
Some(u) => u
241+
TypeVariableValue::Unknown { .. } => t,
242+
TypeVariableValue::Known { value } => value,
231243
}
232244
}
233245
_ => t,
@@ -313,12 +325,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
313325
// use the less efficient algorithm for now.
314326
let mut escaping_types = Vec::with_capacity(snapshot.num_vars);
315327
escaping_types.extend(
316-
(0..snapshot.num_vars) // for all variables that pre-exist the snapshot...
328+
(0..snapshot.num_vars) // for all variables that pre-exist the snapshot, collect..
317329
.map(|i| ty::TyVid { index: i as u32 })
318-
.filter_map(|vid| match self.eq_relations.probe_value(vid) {
319-
TypeVariableValue::Unknown => None,
320-
TypeVariableValue::Known { value } => Some(value),
321-
})); // ...collect what types they've been instantiated with.
330+
.filter_map(|vid| self.probe(vid).known())); // ..types they are instantiated with.
322331
debug!("types_escaping_snapshot = {:?}", escaping_types);
323332
escaping_types
324333
}
@@ -329,10 +338,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
329338
(0..self.var_data.len())
330339
.filter_map(|i| {
331340
let vid = ty::TyVid { index: i as u32 };
332-
if self.probe(vid).is_some() {
333-
None
334-
} else {
335-
Some(vid)
341+
match self.probe(vid) {
342+
TypeVariableValue::Unknown { .. } => Some(vid),
343+
TypeVariableValue::Known { .. } => None,
336344
}
337345
})
338346
.collect()

0 commit comments

Comments
 (0)