Skip to content

Commit 7148ff7

Browse files
committed
have probe() return TypeVariableValue
1 parent 66b961b commit 7148ff7

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

@@ -389,11 +389,11 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
389389
return Err(TypeError::CyclicTy);
390390
} else {
391391
match variables.probe(vid) {
392-
Some(u) => {
392+
TypeVariableValue::Known { value: u } => {
393393
drop(variables);
394394
self.relate(&u, &u)
395395
}
396-
None => {
396+
TypeVariableValue::Unknown { .. } => {
397397
match self.ambient_variance {
398398
// Invariant: no need to make a fresh type variable.
399399
ty::Invariant => return Ok(t),

src/librustc/infer/freshen.rs

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

213213
match t.sty {
214214
ty::TyInfer(ty::TyVar(v)) => {
215-
let opt_ty = self.infcx.type_variables.borrow_mut().probe(v);
215+
let opt_ty = self.infcx.type_variables.borrow_mut().probe(v).known();
216216
self.freshen(
217217
opt_ty,
218218
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
@@ -1090,9 +1090,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10901090
// so this recursion should always be of very limited
10911091
// depth.
10921092
self.type_variables.borrow_mut()
1093-
.probe(v)
1094-
.map(|t| self.shallow_resolve(t))
1095-
.unwrap_or(typ)
1093+
.probe(v)
1094+
.known()
1095+
.map(|t| self.shallow_resolve(t))
1096+
.unwrap_or(typ)
10961097
}
10971098

10981099
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
@@ -75,12 +75,28 @@ struct TypeVariableData {
7575
diverging: bool
7676
}
7777

78-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
79-
enum TypeVariableValue<'tcx> {
78+
#[derive(Copy, Clone, Debug)]
79+
pub enum TypeVariableValue<'tcx> {
8080
Known { value: Ty<'tcx> },
8181
Unknown,
8282
}
8383

84+
impl<'tcx> TypeVariableValue<'tcx> {
85+
pub fn known(&self) -> Option<Ty<'tcx>> {
86+
match *self {
87+
TypeVariableValue::Unknown { .. } => None,
88+
TypeVariableValue::Known { value } => Some(value),
89+
}
90+
}
91+
92+
pub fn is_unknown(&self) -> bool {
93+
match *self {
94+
TypeVariableValue::Unknown { .. } => true,
95+
TypeVariableValue::Known { .. } => false,
96+
}
97+
}
98+
}
99+
84100
pub struct Snapshot<'tcx> {
85101
/// number of variables at the time of the snapshot
86102
num_vars: usize,
@@ -121,8 +137,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
121137
///
122138
/// Precondition: neither `a` nor `b` are known.
123139
pub fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
124-
debug_assert!(self.probe(a).is_none());
125-
debug_assert!(self.probe(b).is_none());
140+
debug_assert!(self.probe(a).is_unknown());
141+
debug_assert!(self.probe(b).is_unknown());
126142
self.eq_relations.union(a, b);
127143
self.sub_relations.union(a, b);
128144
}
@@ -131,8 +147,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
131147
///
132148
/// Precondition: neither `a` nor `b` are known.
133149
pub fn sub(&mut self, a: ty::TyVid, b: ty::TyVid) {
134-
debug_assert!(self.probe(a).is_none());
135-
debug_assert!(self.probe(b).is_none());
150+
debug_assert!(self.probe(a).is_unknown());
151+
debug_assert!(self.probe(b).is_unknown());
136152
self.sub_relations.union(a, b);
137153
}
138154

@@ -141,8 +157,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
141157
/// Precondition: `vid` must not have been previously instantiated.
142158
pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
143159
let vid = self.root_var(vid);
144-
debug_assert!(self.probe(vid).is_none());
145-
debug_assert!(self.eq_relations.probe_value(vid) == TypeVariableValue::Unknown,
160+
debug_assert!(self.probe(vid).is_unknown());
161+
debug_assert!(self.eq_relations.probe_value(vid).is_unknown(),
146162
"instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}",
147163
vid, ty, self.eq_relations.probe_value(vid));
148164
self.eq_relations.union_value(vid, TypeVariableValue::Known { value: ty });
@@ -208,12 +224,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
208224

209225
/// Retrieves the type to which `vid` has been instantiated, if
210226
/// any.
211-
pub fn probe(&mut self, vid: ty::TyVid) -> Option<Ty<'tcx>> {
212-
let vid = self.root_var(vid);
213-
match self.eq_relations.probe_value(vid) {
214-
TypeVariableValue::Unknown => None,
215-
TypeVariableValue::Known { value } => Some(value)
216-
}
227+
pub fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
228+
self.eq_relations.probe_value(vid)
217229
}
218230

219231
/// If `t` is a type-inference variable, and it has been
@@ -223,8 +235,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
223235
match t.sty {
224236
ty::TyInfer(ty::TyVar(v)) => {
225237
match self.probe(v) {
226-
None => t,
227-
Some(u) => u
238+
TypeVariableValue::Unknown { .. } => t,
239+
TypeVariableValue::Known { value } => value,
228240
}
229241
}
230242
_ => t,
@@ -310,12 +322,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
310322
// use the less efficient algorithm for now.
311323
let mut escaping_types = Vec::with_capacity(snapshot.num_vars);
312324
escaping_types.extend(
313-
(0..snapshot.num_vars) // for all variables that pre-exist the snapshot...
325+
(0..snapshot.num_vars) // for all variables that pre-exist the snapshot, collect..
314326
.map(|i| ty::TyVid { index: i as u32 })
315-
.filter_map(|vid| match self.eq_relations.probe_value(vid) {
316-
TypeVariableValue::Unknown => None,
317-
TypeVariableValue::Known { value } => Some(value),
318-
})); // ...collect what types they've been instantiated with.
327+
.filter_map(|vid| self.probe(vid).known())); // ..types they are instantiated with.
319328
debug!("types_escaping_snapshot = {:?}", escaping_types);
320329
escaping_types
321330
}
@@ -326,10 +335,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
326335
(0..self.var_data.len())
327336
.filter_map(|i| {
328337
let vid = ty::TyVid { index: i as u32 };
329-
if self.probe(vid).is_some() {
330-
None
331-
} else {
332-
Some(vid)
338+
match self.probe(vid) {
339+
TypeVariableValue::Unknown { .. } => Some(vid),
340+
TypeVariableValue::Known { .. } => None,
333341
}
334342
})
335343
.collect()

0 commit comments

Comments
 (0)