Skip to content

Commit e58e2b4

Browse files
committed
remove the subtyping relations from TypeVariable
1 parent 105ec7e commit e58e2b4

File tree

6 files changed

+51
-130
lines changed

6 files changed

+51
-130
lines changed

src/librustc/infer/combine.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use super::lub::Lub;
3838
use super::sub::Sub;
3939
use super::InferCtxt;
4040
use super::{MiscVariable, TypeTrace};
41-
use super::type_variable::{RelationDir, BiTo, EqTo, SubtypeOf, SupertypeOf};
4241

4342
use ty::{IntType, UintType};
4443
use ty::{self, Ty, TyCtxt};
@@ -59,6 +58,11 @@ pub struct CombineFields<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
5958
pub obligations: PredicateObligations<'tcx>,
6059
}
6160

61+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
62+
pub enum RelationDir {
63+
SubtypeOf, SupertypeOf, EqTo
64+
}
65+
6266
impl<'infcx, 'gcx, 'tcx> InferCtxt<'infcx, 'gcx, 'tcx> {
6367
pub fn super_combine_tys<R>(&self,
6468
relation: &mut R,
@@ -177,6 +181,8 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
177181
a_is_expected: bool)
178182
-> RelateResult<'tcx, ()>
179183
{
184+
use self::RelationDir::*;
185+
180186
// We use SmallVector here instead of Vec because this code is hot and
181187
// it's rare that the stack length exceeds 1.
182188
let mut stack = SmallVector::new();
@@ -224,16 +230,15 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
224230
// Generalize type if necessary.
225231
let generalized_ty = match dir {
226232
EqTo => self.generalize(a_ty, b_vid, false),
227-
BiTo | SupertypeOf | SubtypeOf => self.generalize(a_ty, b_vid, true),
233+
SupertypeOf | SubtypeOf => self.generalize(a_ty, b_vid, true),
228234
}?;
229235
debug!("instantiate(a_ty={:?}, dir={:?}, \
230236
b_vid={:?}, generalized_ty={:?})",
231237
a_ty, dir, b_vid,
232238
generalized_ty);
233239
self.infcx.type_variables
234240
.borrow_mut()
235-
.instantiate_and_push(
236-
b_vid, generalized_ty, &mut stack);
241+
.instantiate(b_vid, generalized_ty);
237242
generalized_ty
238243
}
239244
};
@@ -246,7 +251,6 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
246251
// to associate causes/spans with each of the relations in
247252
// the stack to get this right.
248253
match dir {
249-
BiTo => Ok(a_ty),
250254
EqTo => self.equate(a_is_expected).relate(&a_ty, &b_ty),
251255
SubtypeOf => self.sub(a_is_expected).relate(&a_ty, &b_ty),
252256
SupertypeOf => self.sub(a_is_expected).relate_with_variance(

src/librustc/infer/equate.rs

Lines changed: 4 additions & 5 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 super::combine::CombineFields;
11+
use super::combine::{CombineFields, RelationDir};
1212
use super::{Subtype};
13-
use super::type_variable::{EqTo};
1413

1514
use ty::{self, Ty, TyCtxt};
1615
use ty::TyVar;
@@ -58,17 +57,17 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
5857
let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
5958
match (&a.sty, &b.sty) {
6059
(&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => {
61-
infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id);
60+
infcx.type_variables.borrow_mut().equate(a_id, b_id);
6261
Ok(a)
6362
}
6463

6564
(&ty::TyInfer(TyVar(a_id)), _) => {
66-
self.fields.instantiate(b, EqTo, a_id, self.a_is_expected)?;
65+
self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
6766
Ok(a)
6867
}
6968

7069
(_, &ty::TyInfer(TyVar(b_id))) => {
71-
self.fields.instantiate(a, EqTo, b_id, self.a_is_expected)?;
70+
self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
7271
Ok(a)
7372
}
7473

src/librustc/infer/sub.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
// except according to those terms.
1010

1111
use super::SubregionOrigin;
12-
use super::combine::CombineFields;
13-
use super::type_variable::{SubtypeOf, SupertypeOf};
12+
use super::combine::{CombineFields, RelationDir};
1413

1514
use traits::Obligation;
1615
use ty::{self, Ty, TyCtxt};
@@ -104,11 +103,11 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
104103
}
105104
(&ty::TyInfer(TyVar(a_id)), _) => {
106105
self.fields
107-
.instantiate(b, SupertypeOf, a_id, !self.a_is_expected)?;
106+
.instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
108107
Ok(a)
109108
}
110109
(_, &ty::TyInfer(TyVar(b_id))) => {
111-
self.fields.instantiate(a, SubtypeOf, b_id, self.a_is_expected)?;
110+
self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
112111
Ok(a)
113112
}
114113

src/librustc/infer/type_variable.rs

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

11-
pub use self::RelationDir::*;
1211
use self::TypeVariableValue::*;
13-
use self::UndoEntry::*;
1412
use hir::def_id::{DefId};
15-
use syntax::util::small_vector::SmallVector;
1613
use syntax::ast;
1714
use syntax_pos::Span;
1815
use ty::{self, Ty};
@@ -55,7 +52,6 @@ struct TypeVariableData<'tcx> {
5552
enum TypeVariableValue<'tcx> {
5653
Known(Ty<'tcx>),
5754
Bounded {
58-
relations: Vec<Relation>,
5955
default: Option<Default<'tcx>>
6056
}
6157
}
@@ -76,33 +72,13 @@ pub struct Snapshot {
7672
eq_snapshot: ut::Snapshot<ty::TyVid>,
7773
}
7874

79-
enum UndoEntry<'tcx> {
80-
// The type of the var was specified.
81-
SpecifyVar(ty::TyVid, Vec<Relation>, Option<Default<'tcx>>),
82-
Relate(ty::TyVid, ty::TyVid),
83-
RelateRange(ty::TyVid, usize),
75+
struct Instantiate<'tcx> {
76+
vid: ty::TyVid,
77+
default: Option<Default<'tcx>>,
8478
}
8579

8680
struct Delegate<'tcx>(PhantomData<&'tcx ()>);
8781

88-
type Relation = (RelationDir, ty::TyVid);
89-
90-
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
91-
pub enum RelationDir {
92-
SubtypeOf, SupertypeOf, EqTo, BiTo
93-
}
94-
95-
impl RelationDir {
96-
fn opposite(self) -> RelationDir {
97-
match self {
98-
SubtypeOf => SupertypeOf,
99-
SupertypeOf => SubtypeOf,
100-
EqTo => EqTo,
101-
BiTo => BiTo,
102-
}
103-
}
104-
}
105-
10682
impl<'tcx> TypeVariableTable<'tcx> {
10783
pub fn new() -> TypeVariableTable<'tcx> {
10884
TypeVariableTable {
@@ -111,10 +87,6 @@ impl<'tcx> TypeVariableTable<'tcx> {
11187
}
11288
}
11389

114-
fn relations<'a>(&'a mut self, a: ty::TyVid) -> &'a mut Vec<Relation> {
115-
relations(self.values.get_mut(a.index as usize))
116-
}
117-
11890
pub fn default(&self, vid: ty::TyVid) -> Option<Default<'tcx>> {
11991
match &self.values.get(vid.index as usize).value {
12092
&Known(_) => None,
@@ -130,68 +102,37 @@ impl<'tcx> TypeVariableTable<'tcx> {
130102
&self.values.get(vid.index as usize).origin
131103
}
132104

133-
/// Records that `a <: b`, `a :> b`, or `a == b`, depending on `dir`.
105+
/// Records that `a == b`, depending on `dir`.
134106
///
135107
/// Precondition: neither `a` nor `b` are known.
136-
pub fn relate_vars(&mut self, a: ty::TyVid, dir: RelationDir, b: ty::TyVid) {
137-
let a = self.root_var(a);
138-
let b = self.root_var(b);
139-
if a != b {
140-
if dir == EqTo {
141-
// a and b must be equal which we mark in the unification table
142-
let root = self.eq_relations.union(a, b);
143-
// In addition to being equal, all relations from the variable which is no longer
144-
// the root must be added to the root so they are not forgotten as the other
145-
// variable should no longer be referenced (other than to get the root)
146-
let other = if a == root { b } else { a };
147-
let count = {
148-
let (relations, root_relations) = if other.index < root.index {
149-
let (pre, post) = self.values.split_at_mut(root.index as usize);
150-
(relations(&mut pre[other.index as usize]), relations(&mut post[0]))
151-
} else {
152-
let (pre, post) = self.values.split_at_mut(other.index as usize);
153-
(relations(&mut post[0]), relations(&mut pre[root.index as usize]))
154-
};
155-
root_relations.extend_from_slice(relations);
156-
relations.len()
157-
};
158-
self.values.record(RelateRange(root, count));
159-
} else {
160-
self.relations(a).push((dir, b));
161-
self.relations(b).push((dir.opposite(), a));
162-
self.values.record(Relate(a, b));
163-
}
164-
}
108+
pub fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
109+
debug_assert!(self.probe(a).is_none());
110+
debug_assert!(self.probe(b).is_none());
111+
self.eq_relations.union(a, b);
165112
}
166113

167-
/// Instantiates `vid` with the type `ty` and then pushes an entry onto `stack` for each of the
168-
/// relations of `vid` to other variables. The relations will have the form `(ty, dir, vid1)`
169-
/// where `vid1` is some other variable id.
114+
/// Instantiates `vid` with the type `ty`.
170115
///
171116
/// Precondition: `vid` must be a root in the unification table
172-
pub fn instantiate_and_push(
173-
&mut self,
174-
vid: ty::TyVid,
175-
ty: Ty<'tcx>,
176-
stack: &mut SmallVector<(Ty<'tcx>, RelationDir, ty::TyVid)>)
177-
{
117+
/// and has not previously been instantiated.
118+
pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
178119
debug_assert!(self.root_var(vid) == vid);
179-
let old_value = {
180-
let value_ptr = &mut self.values.get_mut(vid.index as usize).value;
181-
mem::replace(value_ptr, Known(ty))
182-
};
120+
debug_assert!(self.probe(vid).is_none());
183121

184-
let (relations, default) = match old_value {
185-
Bounded { relations, default } => (relations, default),
186-
Known(_) => bug!("Asked to instantiate variable that is \
187-
already instantiated")
122+
let old_value = {
123+
let vid_data = &mut self.values[vid.index as usize];
124+
mem::replace(&mut vid_data.value, TypeVariableValue::Known(ty))
188125
};
189126

190-
for &(dir, vid) in &relations {
191-
stack.push((ty, dir, vid));
127+
match old_value {
128+
TypeVariableValue::Bounded { default } => {
129+
self.values.record(Instantiate { vid: vid, default: default });
130+
}
131+
TypeVariableValue::Known(old_ty) => {
132+
bug!("instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}",
133+
vid, ty, old_ty)
134+
}
192135
}
193-
194-
self.values.record(SpecifyVar(vid, relations, default));
195136
}
196137

197138
pub fn new_var(&mut self,
@@ -201,7 +142,7 @@ impl<'tcx> TypeVariableTable<'tcx> {
201142
debug!("new_var(diverging={:?}, origin={:?})", diverging, origin);
202143
self.eq_relations.new_key(());
203144
let index = self.values.push(TypeVariableData {
204-
value: Bounded { relations: vec![], default: default },
145+
value: Bounded { default: default },
205146
origin: origin,
206147
diverging: diverging
207148
});
@@ -298,7 +239,7 @@ impl<'tcx> TypeVariableTable<'tcx> {
298239
debug!("NewElem({}) new_elem_threshold={}", index, new_elem_threshold);
299240
}
300241

301-
sv::UndoLog::Other(SpecifyVar(vid, ..)) => {
242+
sv::UndoLog::Other(Instantiate { vid, .. }) => {
302243
if vid.index < new_elem_threshold {
303244
// quick check to see if this variable was
304245
// created since the snapshot started or not.
@@ -334,35 +275,12 @@ impl<'tcx> TypeVariableTable<'tcx> {
334275

335276
impl<'tcx> sv::SnapshotVecDelegate for Delegate<'tcx> {
336277
type Value = TypeVariableData<'tcx>;
337-
type Undo = UndoEntry<'tcx>;
338-
339-
fn reverse(values: &mut Vec<TypeVariableData<'tcx>>, action: UndoEntry<'tcx>) {
340-
match action {
341-
SpecifyVar(vid, relations, default) => {
342-
values[vid.index as usize].value = Bounded {
343-
relations: relations,
344-
default: default
345-
};
346-
}
278+
type Undo = Instantiate<'tcx>;
347279

348-
Relate(a, b) => {
349-
relations(&mut (*values)[a.index as usize]).pop();
350-
relations(&mut (*values)[b.index as usize]).pop();
351-
}
352-
353-
RelateRange(i, n) => {
354-
let relations = relations(&mut (*values)[i.index as usize]);
355-
for _ in 0..n {
356-
relations.pop();
357-
}
358-
}
359-
}
360-
}
361-
}
362-
363-
fn relations<'a>(v: &'a mut TypeVariableData) -> &'a mut Vec<Relation> {
364-
match v.value {
365-
Known(_) => bug!("var_sub_var: variable is known"),
366-
Bounded { ref mut relations, .. } => relations
280+
fn reverse(values: &mut Vec<TypeVariableData<'tcx>>, action: Instantiate<'tcx>) {
281+
let Instantiate { vid, default } = action;
282+
values[vid.index as usize].value = Bounded {
283+
default: default
284+
};
367285
}
368286
}

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![feature(i128_type)]
3333
#![feature(libc)]
3434
#![feature(loop_break_value)]
35+
#![feature(never_type)]
3536
#![feature(nonzero)]
3637
#![cfg_attr(stage0, feature(pub_restricted))]
3738
#![feature(quote)]

src/test/compile-fail/issue-7813.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
let v = &[];
13-
//~^ NOTE consider giving `it` a type
14-
let it = v.iter(); //~ ERROR cannot infer type for `_`
12+
let v = &[]; //~ NOTE consider giving `v` a type
13+
let it = v.iter(); //~ ERROR type annotations needed
14+
//~^ NOTE cannot infer type for `_`
1515
}

0 commit comments

Comments
 (0)