Skip to content

Commit 744f102

Browse files
committed
obtain UnificationTable and snapshot_vec from ena instead
The ena version has an improved interface. I suspect `librustc_data_structures` should start migrating out to crates.io in general.
1 parent 3251227 commit 744f102

File tree

11 files changed

+103
-70
lines changed

11 files changed

+103
-70
lines changed

src/Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/infer/combine.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'infcx, 'gcx, 'tcx> InferCtxt<'infcx, 'gcx, 'tcx> {
132132
{
133133
self.int_unification_table
134134
.borrow_mut()
135-
.unify_var_value(vid, val)
135+
.unify_var_value(vid, Some(val))
136136
.map_err(|e| int_unification_error(vid_is_expected, e))?;
137137
match val {
138138
IntType(v) => Ok(self.tcx.mk_mach_int(v)),
@@ -148,7 +148,7 @@ impl<'infcx, 'gcx, 'tcx> InferCtxt<'infcx, 'gcx, 'tcx> {
148148
{
149149
self.float_unification_table
150150
.borrow_mut()
151-
.unify_var_value(vid, val)
151+
.unify_var_value(vid, Some(ty::FloatVarValue(val)))
152152
.map_err(|e| float_unification_error(vid_is_expected, e))?;
153153
Ok(self.tcx.mk_mach_float(val))
154154
}
@@ -496,9 +496,9 @@ fn int_unification_error<'tcx>(a_is_expected: bool, v: (ty::IntVarValue, ty::Int
496496
}
497497

498498
fn float_unification_error<'tcx>(a_is_expected: bool,
499-
v: (ast::FloatTy, ast::FloatTy))
499+
v: (ty::FloatVarValue, ty::FloatVarValue))
500500
-> TypeError<'tcx>
501501
{
502-
let (a, b) = v;
502+
let (ty::FloatVarValue(a), ty::FloatVarValue(b)) = v;
503503
TypeError::FloatMismatch(ty::relate::expected_found_bool(a_is_expected, &a, &b))
504504
}

src/librustc/infer/freshen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
222222
ty::TyInfer(ty::IntVar(v)) => {
223223
self.freshen(
224224
self.infcx.int_unification_table.borrow_mut()
225-
.probe(v)
225+
.probe_value(v)
226226
.map(|v| v.to_type(tcx)),
227227
ty::IntVar(v),
228228
ty::FreshIntTy)
@@ -231,7 +231,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
231231
ty::TyInfer(ty::FloatVar(v)) => {
232232
self.freshen(
233233
self.infcx.float_unification_table.borrow_mut()
234-
.probe(v)
234+
.probe_value(v)
235235
.map(|v| v.to_type(tcx)),
236236
ty::FloatVar(v),
237237
ty::FreshFloatTy)

src/librustc/infer/mod.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
3030
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
3131
use ty::relate::RelateResult;
3232
use traits::{self, ObligationCause, PredicateObligations, Reveal};
33-
use rustc_data_structures::unify::{self, UnificationTable};
33+
use rustc_data_structures::unify as ut;
3434
use std::cell::{Cell, RefCell, Ref};
3535
use std::fmt;
3636
use syntax::ast;
@@ -93,10 +93,10 @@ pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
9393
pub type_variables: RefCell<type_variable::TypeVariableTable<'tcx>>,
9494

9595
// Map from integral variable to the kind of integer it represents
96-
int_unification_table: RefCell<UnificationTable<ty::IntVid>>,
96+
int_unification_table: RefCell<ut::UnificationTable<ut::InPlace<ty::IntVid>>>,
9797

9898
// Map from floating variable to the kind of float it represents
99-
float_unification_table: RefCell<UnificationTable<ty::FloatVid>>,
99+
float_unification_table: RefCell<ut::UnificationTable<ut::InPlace<ty::FloatVid>>>,
100100

101101
// For region variables.
102102
region_vars: RegionVarBindings<'a, 'gcx, 'tcx>,
@@ -377,8 +377,8 @@ impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
377377
in_progress_tables,
378378
projection_cache: RefCell::new(traits::ProjectionCache::new()),
379379
type_variables: RefCell::new(type_variable::TypeVariableTable::new()),
380-
int_unification_table: RefCell::new(UnificationTable::new()),
381-
float_unification_table: RefCell::new(UnificationTable::new()),
380+
int_unification_table: RefCell::new(ut::UnificationTable::new()),
381+
float_unification_table: RefCell::new(ut::UnificationTable::new()),
382382
region_vars: RegionVarBindings::new(tcx),
383383
selection_cache: traits::SelectionCache::new(),
384384
evaluation_cache: traits::EvaluationCache::new(),
@@ -410,8 +410,8 @@ impl<'tcx, T> InferOk<'tcx, T> {
410410
pub struct CombinedSnapshot<'a, 'tcx:'a> {
411411
projection_cache_snapshot: traits::ProjectionCacheSnapshot,
412412
type_snapshot: type_variable::Snapshot,
413-
int_snapshot: unify::Snapshot<ty::IntVid>,
414-
float_snapshot: unify::Snapshot<ty::FloatVid>,
413+
int_snapshot: ut::Snapshot<ut::InPlace<ty::IntVid>>,
414+
float_snapshot: ut::Snapshot<ut::InPlace<ty::FloatVid>>,
415415
region_vars_snapshot: RegionSnapshot,
416416
was_in_snapshot: bool,
417417
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
@@ -611,14 +611,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
611611
use ty::error::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat};
612612
match ty.sty {
613613
ty::TyInfer(ty::IntVar(vid)) => {
614-
if self.int_unification_table.borrow_mut().has_value(vid) {
614+
if self.int_unification_table.borrow_mut().probe_value(vid).is_some() {
615615
Neither
616616
} else {
617617
UnconstrainedInt
618618
}
619619
},
620620
ty::TyInfer(ty::FloatVar(vid)) => {
621-
if self.float_unification_table.borrow_mut().has_value(vid) {
621+
if self.float_unification_table.borrow_mut().probe_value(vid).is_some() {
622622
Neither
623623
} else {
624624
UnconstrainedFloat
@@ -631,27 +631,32 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
631631
pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
632632
let mut variables = Vec::new();
633633

634-
let unbound_ty_vars = self.type_variables
635-
.borrow_mut()
636-
.unsolved_variables()
637-
.into_iter()
638-
.map(|t| self.tcx.mk_var(t));
639-
640-
let unbound_int_vars = self.int_unification_table
641-
.borrow_mut()
642-
.unsolved_variables()
643-
.into_iter()
644-
.map(|v| self.tcx.mk_int_var(v));
634+
{
635+
let mut type_variables = self.type_variables.borrow_mut();
636+
variables.extend(
637+
type_variables
638+
.unsolved_variables()
639+
.into_iter()
640+
.map(|t| self.tcx.mk_var(t)));
641+
}
645642

646-
let unbound_float_vars = self.float_unification_table
647-
.borrow_mut()
648-
.unsolved_variables()
649-
.into_iter()
650-
.map(|v| self.tcx.mk_float_var(v));
643+
{
644+
let mut int_unification_table = self.int_unification_table.borrow_mut();
645+
variables.extend(
646+
(0..int_unification_table.len())
647+
.map(|i| ty::IntVid { index: i as u32 })
648+
.filter(|&vid| int_unification_table.probe_value(vid).is_none())
649+
.map(|v| self.tcx.mk_int_var(v)));
650+
}
651651

652-
variables.extend(unbound_ty_vars);
653-
variables.extend(unbound_int_vars);
654-
variables.extend(unbound_float_vars);
652+
{
653+
let mut float_unification_table = self.float_unification_table.borrow_mut();
654+
variables.extend(
655+
(0..float_unification_table.len())
656+
.map(|i| ty::FloatVid { index: i as u32 })
657+
.filter(|&vid| float_unification_table.probe_value(vid).is_none())
658+
.map(|v| self.tcx.mk_float_var(v)));
659+
}
655660

656661
return variables;
657662
}
@@ -1093,15 +1098,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10931098
ty::TyInfer(ty::IntVar(v)) => {
10941099
self.int_unification_table
10951100
.borrow_mut()
1096-
.probe(v)
1101+
.probe_value(v)
10971102
.map(|v| v.to_type(self.tcx))
10981103
.unwrap_or(typ)
10991104
}
11001105

11011106
ty::TyInfer(ty::FloatVar(v)) => {
11021107
self.float_unification_table
11031108
.borrow_mut()
1104-
.probe(v)
1109+
.probe_value(v)
11051110
.map(|v| v.to_type(self.tcx))
11061111
.unwrap_or(typ)
11071112
}

src/librustc/infer/region_inference/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::unify_key;
2121

2222
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2323
use rustc_data_structures::graph::{self, Direction, NodeIndex, OUTGOING};
24-
use rustc_data_structures::unify::{self, UnificationTable};
24+
use rustc_data_structures::unify as ut;
2525
use middle::free_region::RegionRelations;
2626
use ty::{self, Ty, TyCtxt};
2727
use ty::{Region, RegionVid};
@@ -230,7 +230,7 @@ pub struct RegionVarBindings<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
230230
/// back.
231231
undo_log: RefCell<Vec<UndoLogEntry<'tcx>>>,
232232

233-
unification_table: RefCell<UnificationTable<ty::RegionVid>>,
233+
unification_table: RefCell<ut::UnificationTable<ut::InPlace<ty::RegionVid>>>,
234234

235235
/// This contains the results of inference. It begins as an empty
236236
/// option and only acquires a value after inference is complete.
@@ -239,7 +239,7 @@ pub struct RegionVarBindings<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
239239

240240
pub struct RegionSnapshot {
241241
length: usize,
242-
region_snapshot: unify::Snapshot<ty::RegionVid>,
242+
region_snapshot: ut::Snapshot<ut::InPlace<ty::RegionVid>>,
243243
skolemization_count: u32,
244244
}
245245

@@ -365,7 +365,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
365365
skolemization_count: Cell::new(0),
366366
bound_count: Cell::new(0),
367367
undo_log: RefCell::new(Vec::new()),
368-
unification_table: RefCell::new(UnificationTable::new()),
368+
unification_table: RefCell::new(ut::UnificationTable::new()),
369369
}
370370
}
371371

@@ -808,7 +808,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
808808
}
809809

810810
pub fn opportunistic_resolve_var(&self, rid: RegionVid) -> ty::Region<'tcx> {
811-
let vid = self.unification_table.borrow_mut().find_value(rid).min_vid;
811+
let vid = self.unification_table.borrow_mut().probe_value(rid).min_vid;
812812
self.tcx.mk_region(ty::ReVar(vid))
813813
}
814814

src/librustc/infer/type_variable.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct TypeVariableTable<'tcx> {
2626

2727
/// Two variables are unified in `eq_relations` when we have a
2828
/// constraint `?X == ?Y`.
29-
eq_relations: ut::UnificationTable<ty::TyVid>,
29+
eq_relations: ut::UnificationTable<ut::InPlace<ty::TyVid>>,
3030

3131
/// Two variables are unified in `eq_relations` when we have a
3232
/// constraint `?X <: ?Y` *or* a constraint `?Y <: ?X`. This second
@@ -45,7 +45,7 @@ pub struct TypeVariableTable<'tcx> {
4545
/// This is reasonable because, in Rust, subtypes have the same
4646
/// "skeleton" and hence there is no possible type such that
4747
/// (e.g.) `Box<?3> <: ?3` for any `?3`.
48-
sub_relations: ut::UnificationTable<ty::TyVid>,
48+
sub_relations: ut::UnificationTable<ut::InPlace<ty::TyVid>>,
4949
}
5050

5151
/// Reasons to create a type inference variable
@@ -83,8 +83,8 @@ enum TypeVariableValue<'tcx> {
8383

8484
pub struct Snapshot {
8585
snapshot: sv::Snapshot,
86-
eq_snapshot: ut::Snapshot<ty::TyVid>,
87-
sub_snapshot: ut::Snapshot<ty::TyVid>,
86+
eq_snapshot: ut::Snapshot<ut::InPlace<ty::TyVid>>,
87+
sub_snapshot: ut::Snapshot<ut::InPlace<ty::TyVid>>,
8888
}
8989

9090
struct Instantiate {
@@ -351,3 +351,10 @@ impl<'tcx> sv::SnapshotVecDelegate for Delegate<'tcx> {
351351
values[vid.index as usize].value = Unknown;
352352
}
353353
}
354+
355+
impl ut::UnifyKey for ty::TyVid {
356+
type Value = ();
357+
fn index(&self) -> u32 { self.index }
358+
fn from_index(i: u32) -> ty::TyVid { ty::TyVid { index: i } }
359+
fn tag() -> &'static str { "TyVid" }
360+
}

src/librustc/infer/unify_key.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::ast;
12-
use ty::{self, IntVarValue, Ty, TyCtxt};
13-
use rustc_data_structures::unify::{Combine, UnifyKey};
11+
use ty::{self, FloatVarValue, IntVarValue, Ty, TyCtxt};
12+
use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue};
1413

1514
pub trait ToType {
1615
fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>;
@@ -20,7 +19,10 @@ impl UnifyKey for ty::IntVid {
2019
type Value = Option<IntVarValue>;
2120
fn index(&self) -> u32 { self.index }
2221
fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } }
23-
fn tag(_: Option<ty::IntVid>) -> &'static str { "IntVid" }
22+
fn tag() -> &'static str { "IntVid" }
23+
}
24+
25+
impl EqUnifyValue for IntVarValue {
2426
}
2527

2628
#[derive(PartialEq, Copy, Clone, Debug)]
@@ -31,23 +33,25 @@ pub struct RegionVidKey {
3133
pub min_vid: ty::RegionVid
3234
}
3335

34-
impl Combine for RegionVidKey {
35-
fn combine(&self, other: &RegionVidKey) -> RegionVidKey {
36-
let min_vid = if self.min_vid.index < other.min_vid.index {
37-
self.min_vid
36+
impl UnifyValue for RegionVidKey {
37+
type Error = NoError;
38+
39+
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
40+
let min_vid = if value1.min_vid.index < value2.min_vid.index {
41+
value1.min_vid
3842
} else {
39-
other.min_vid
43+
value2.min_vid
4044
};
4145

42-
RegionVidKey { min_vid: min_vid }
46+
Ok(RegionVidKey { min_vid: min_vid })
4347
}
4448
}
4549

4650
impl UnifyKey for ty::RegionVid {
4751
type Value = RegionVidKey;
4852
fn index(&self) -> u32 { self.index }
4953
fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid { index: i } }
50-
fn tag(_: Option<ty::RegionVid>) -> &'static str { "RegionVid" }
54+
fn tag() -> &'static str { "RegionVid" }
5155
}
5256

5357
impl ToType for IntVarValue {
@@ -62,21 +66,17 @@ impl ToType for IntVarValue {
6266
// Floating point type keys
6367

6468
impl UnifyKey for ty::FloatVid {
65-
type Value = Option<ast::FloatTy>;
69+
type Value = Option<FloatVarValue>;
6670
fn index(&self) -> u32 { self.index }
6771
fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
68-
fn tag(_: Option<ty::FloatVid>) -> &'static str { "FloatVid" }
72+
fn tag() -> &'static str { "FloatVid" }
6973
}
7074

71-
impl ToType for ast::FloatTy {
72-
fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
73-
tcx.mk_mach_float(*self)
74-
}
75+
impl EqUnifyValue for FloatVarValue {
7576
}
7677

77-
impl UnifyKey for ty::TyVid {
78-
type Value = ();
79-
fn index(&self) -> u32 { self.index }
80-
fn from_index(i: u32) -> ty::TyVid { ty::TyVid { index: i } }
81-
fn tag(_: Option<ty::TyVid>) -> &'static str { "TyVid" }
78+
impl ToType for FloatVarValue {
79+
fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
80+
tcx.mk_mach_float(self.0)
81+
}
8282
}

src/librustc/ty/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,15 @@ pub struct ClosureUpvar<'tcx> {
655655
pub ty: Ty<'tcx>,
656656
}
657657

658-
#[derive(Clone, Copy, PartialEq)]
658+
#[derive(Clone, Copy, PartialEq, Eq)]
659659
pub enum IntVarValue {
660660
IntType(ast::IntTy),
661661
UintType(ast::UintTy),
662662
}
663663

664+
#[derive(Clone, Copy, PartialEq, Eq)]
665+
pub struct FloatVarValue(pub ast::FloatTy);
666+
664667
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
665668
pub struct TypeParameterDef {
666669
pub name: Name,

0 commit comments

Comments
 (0)