Skip to content

Commit 2309110

Browse files
committed
change the InferenceValue to store the value directly
1 parent fcf23d2 commit 2309110

File tree

5 files changed

+26
-57
lines changed

5 files changed

+26
-57
lines changed

chalk-rust/src/solve/infer/mod.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use ena::unify as ena;
22
use errors::*;
33
use ir::*;
4-
use std::sync::Arc;
54

65
mod instantiate;
76
mod lifetime_var;
@@ -17,10 +16,8 @@ use self::lifetime_var::LifetimeInferenceVariable;
1716
#[derive(Clone)]
1817
pub struct InferenceTable {
1918
ty_unify: ena::UnificationTable<TyInferenceVariable>,
20-
ty_values: Vec<Arc<Ty>>,
2119

2220
krate_unify: ena::UnificationTable<KrateInferenceVariable>,
23-
krate_values: Vec<Krate>,
2421

2522
/// Unlike normal variables, we don't unify lifetime variables.
2623
/// Instead, we just keep track of the universe in which they were
@@ -30,7 +27,6 @@ pub struct InferenceTable {
3027

3128
pub struct InferenceSnapshot {
3229
ty_unify_snapshot: ena::Snapshot<TyInferenceVariable>,
33-
ty_values_len: usize,
3430

3531
krate_unify_snapshot: ena::Snapshot<KrateInferenceVariable>,
3632
}
@@ -44,8 +40,6 @@ impl InferenceTable {
4440
InferenceTable {
4541
ty_unify: ena::UnificationTable::new(),
4642
krate_unify: ena::UnificationTable::new(),
47-
ty_values: vec![],
48-
krate_values: vec![],
4943
lifetime_vars: vec![],
5044
}
5145
}
@@ -91,13 +85,12 @@ impl InferenceTable {
9185
InferenceSnapshot {
9286
ty_unify_snapshot,
9387
krate_unify_snapshot,
94-
ty_values_len: self.ty_values.len(),
9588
}
9689
}
9790

9891
pub fn rollback_to(&mut self, snapshot: InferenceSnapshot) {
9992
self.ty_unify.rollback_to(snapshot.ty_unify_snapshot);
100-
self.ty_values.truncate(snapshot.ty_values_len);
93+
self.krate_unify.rollback_to(snapshot.krate_unify_snapshot);
10194
}
10295

10396
fn commit(&mut self, snapshot: InferenceSnapshot) {
@@ -121,12 +114,12 @@ impl InferenceTable {
121114
}
122115
}
123116

124-
fn normalize_shallow(&mut self, leaf: &Ty) -> Option<Arc<Ty>> {
117+
fn normalize_shallow(&mut self, leaf: &Ty) -> Option<Ty> {
125118
leaf.inference_var()
126119
.and_then(|var| {
127120
match self.ty_unify.probe_value(var) {
128121
InferenceValue::Unbound(_) => None,
129-
InferenceValue::Bound(val) => Some(self.ty_values[val.as_usize()].clone()),
122+
InferenceValue::Bound(ref val) => Some(val.clone()),
130123
}
131124
})
132125
}
@@ -138,17 +131,17 @@ impl InferenceTable {
138131
}
139132
}
140133

141-
fn probe_var(&mut self, var: TyInferenceVariable) -> Option<Arc<Ty>> {
134+
fn probe_var(&mut self, var: TyInferenceVariable) -> Option<Ty> {
142135
match self.ty_unify.probe_value(var) {
143136
InferenceValue::Unbound(_) => None,
144-
InferenceValue::Bound(val) => Some(self.ty_values[val.as_usize()].clone()),
137+
InferenceValue::Bound(ref val) => Some(val.clone()),
145138
}
146139
}
147140

148141
fn probe_krate_var(&mut self, var: KrateInferenceVariable) -> Option<Krate> {
149142
match self.krate_unify.probe_value(var) {
150143
InferenceValue::Unbound(_) => None,
151-
InferenceValue::Bound(val) => Some(self.krate_values[val.as_usize()]),
144+
InferenceValue::Bound(val) => Some(val.clone()),
152145
}
153146
}
154147
}

chalk-rust/src/solve/infer/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'q> Folder for Querifier<'q> {
8989
// also have to shift *that* into the correct binder
9090
// depth.
9191
let mut folder = (self, Shifter::new(binders));
92-
(*ty).fold_with(&mut folder, 0)
92+
ty.fold_with(&mut folder, 0)
9393
}
9494
None => {
9595
// If this variable is not yet bound, find its

chalk-rust/src/solve/infer/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl<'q> Folder for Normalizer<'q> {
7171
assert_eq!(binders, 0);
7272
let var = TyInferenceVariable::from_depth(depth);
7373
match self.table.probe_var(var) {
74-
Some(ty) => (*ty).fold_with(self, 0),
74+
Some(ty) => ty.fold_with(self, 0),
7575
None => Ok(var.to_ty()),
7676
}
7777
}

chalk-rust/src/solve/infer/unify.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ impl<'t> Unifier<'t> {
207207

208208
OccursCheck::new(self, var, universe_index).check_ty(ty)?;
209209

210-
let value_index = ValueIndex::new(self.table.ty_values.len());
211-
self.table.ty_values.push(Arc::new(ty.clone()));
212-
self.table.ty_unify.unify_var_value(var, InferenceValue::Bound(value_index)).unwrap();
210+
self.table.ty_unify.unify_var_value(var, InferenceValue::Bound(ty.clone())).unwrap();
213211
debug!("unify_var_ty: var {:?} set to {:?}", var, ty);
214212

215213
Ok(())
@@ -234,9 +232,7 @@ impl<'t> Unifier<'t> {
234232
(&Krate::Var(depth), &Krate::Id(id)) |
235233
(&Krate::Id(id), &Krate::Var(depth)) => {
236234
let var = KrateInferenceVariable::from_depth(depth);
237-
let value_index = ValueIndex::new(self.table.krate_values.len());
238-
self.table.krate_values.push(Krate::Id(id));
239-
self.table.krate_unify.unify_var_value(var, InferenceValue::Bound(value_index))
235+
self.table.krate_unify.unify_var_value(var, InferenceValue::Bound(Krate::Id(id)))
240236
}
241237

242238
(&Krate::Id(a_id), &Krate::Id(b_id)) => {

chalk-rust/src/solve/infer/var.rs

+16-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ena::unify::{UnifyKey, UnifyValue};
22
use ir::*;
33
use std::cmp::min;
4-
use std::fmt;
4+
use std::fmt::{self, Debug};
55
use std::u32;
66

77
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
@@ -25,7 +25,7 @@ impl TyInferenceVariable {
2525
}
2626

2727
impl UnifyKey for TyInferenceVariable {
28-
type Value = InferenceValue;
28+
type Value = InferenceValue<Ty>;
2929

3030
fn index(&self) -> u32 {
3131
self.index
@@ -61,7 +61,7 @@ impl KrateInferenceVariable {
6161
}
6262

6363
impl UnifyKey for KrateInferenceVariable {
64-
type Value = InferenceValue;
64+
type Value = InferenceValue<Krate>;
6565

6666
fn index(&self) -> u32 {
6767
self.index
@@ -80,29 +80,25 @@ impl UnifyKey for KrateInferenceVariable {
8080
/// a universe index; when the inference variable is assigned a value,
8181
/// it becomes bound and refers to an entry in the
8282
/// `InferenceTable.value` vector.
83-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
84-
pub enum InferenceValue {
83+
#[derive(Clone, Debug, PartialEq, Eq)]
84+
pub enum InferenceValue<V: Clone + Debug> {
8585
Unbound(UniverseIndex),
86-
Bound(ValueIndex),
86+
Bound(V),
8787
}
8888

89-
impl UnifyValue for InferenceValue {
90-
fn unify_values(a: &InferenceValue, b: &InferenceValue)
91-
-> Result<InferenceValue, (InferenceValue, InferenceValue)> {
92-
match (*a, *b) {
93-
(InferenceValue::Unbound(ui_a), InferenceValue::Unbound(ui_b)) => {
89+
impl<V: Clone + Debug> UnifyValue for InferenceValue<V> {
90+
fn unify_values(a: &InferenceValue<V>, b: &InferenceValue<V>)
91+
-> Result<InferenceValue<V>, (InferenceValue<V>, InferenceValue<V>)> {
92+
match (a, b) {
93+
(&InferenceValue::Unbound(ui_a), &InferenceValue::Unbound(ui_b)) => {
9494
Ok(InferenceValue::Unbound(min(ui_a, ui_b)))
9595
}
96-
(bound @ InferenceValue::Bound(_), InferenceValue::Unbound(_)) |
97-
(InferenceValue::Unbound(_), bound @ InferenceValue::Bound(_)) => {
98-
Ok(bound)
96+
(bound @ &InferenceValue::Bound(_), &InferenceValue::Unbound(_)) |
97+
(&InferenceValue::Unbound(_), bound @ &InferenceValue::Bound(_)) => {
98+
Ok(bound.clone())
9999
}
100-
(InferenceValue::Bound(_), InferenceValue::Bound(_)) => {
101-
// we don't even try to allow unifying things that are
102-
// already bound; that is handled at a higher-level by
103-
// the `InferenceTable`; this could probably just be a
104-
// `panic!` actually
105-
Err((*a, *b))
100+
(&InferenceValue::Bound(_), &InferenceValue::Bound(_)) => {
101+
panic!("we should not be asked to unify two bound things")
106102
}
107103
}
108104
}
@@ -120,19 +116,3 @@ impl fmt::Debug for KrateInferenceVariable {
120116
}
121117
}
122118

123-
/// An index into the `InferenceTable.values` vector.
124-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
125-
pub struct ValueIndex {
126-
index: u32
127-
}
128-
129-
impl ValueIndex {
130-
pub fn new(value: usize) -> ValueIndex {
131-
ValueIndex { index: value as u32 }
132-
}
133-
134-
pub fn as_usize(&self) -> usize {
135-
self.index as usize
136-
}
137-
}
138-

0 commit comments

Comments
 (0)