8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- pub use self :: RelationDir :: * ;
12
11
use self :: TypeVariableValue :: * ;
13
- use self :: UndoEntry :: * ;
14
12
use hir:: def_id:: { DefId } ;
15
- use syntax:: util:: small_vector:: SmallVector ;
16
13
use syntax:: ast;
17
14
use syntax_pos:: Span ;
18
15
use ty:: { self , Ty } ;
@@ -55,7 +52,6 @@ struct TypeVariableData<'tcx> {
55
52
enum TypeVariableValue < ' tcx > {
56
53
Known ( Ty < ' tcx > ) ,
57
54
Bounded {
58
- relations : Vec < Relation > ,
59
55
default : Option < Default < ' tcx > >
60
56
}
61
57
}
@@ -76,33 +72,13 @@ pub struct Snapshot {
76
72
eq_snapshot : ut:: Snapshot < ty:: TyVid > ,
77
73
}
78
74
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 > > ,
84
78
}
85
79
86
80
struct Delegate < ' tcx > ( PhantomData < & ' tcx ( ) > ) ;
87
81
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
-
106
82
impl < ' tcx > TypeVariableTable < ' tcx > {
107
83
pub fn new ( ) -> TypeVariableTable < ' tcx > {
108
84
TypeVariableTable {
@@ -111,10 +87,6 @@ impl<'tcx> TypeVariableTable<'tcx> {
111
87
}
112
88
}
113
89
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
-
118
90
pub fn default ( & self , vid : ty:: TyVid ) -> Option < Default < ' tcx > > {
119
91
match & self . values . get ( vid. index as usize ) . value {
120
92
& Known ( _) => None ,
@@ -130,68 +102,37 @@ impl<'tcx> TypeVariableTable<'tcx> {
130
102
& self . values . get ( vid. index as usize ) . origin
131
103
}
132
104
133
- /// Records that `a <: b`, `a :> b`, or `a == b`, depending on `dir`.
105
+ /// Records that `a == b`, depending on `dir`.
134
106
///
135
107
/// 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) ;
165
112
}
166
113
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`.
170
115
///
171
116
/// 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 > ) {
178
119
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( ) ) ;
183
121
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) )
188
125
} ;
189
126
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
+ }
192
135
}
193
-
194
- self . values . record ( SpecifyVar ( vid, relations, default) ) ;
195
136
}
196
137
197
138
pub fn new_var ( & mut self ,
@@ -201,7 +142,7 @@ impl<'tcx> TypeVariableTable<'tcx> {
201
142
debug ! ( "new_var(diverging={:?}, origin={:?})" , diverging, origin) ;
202
143
self . eq_relations . new_key ( ( ) ) ;
203
144
let index = self . values . push ( TypeVariableData {
204
- value : Bounded { relations : vec ! [ ] , default : default } ,
145
+ value : Bounded { default : default } ,
205
146
origin : origin,
206
147
diverging : diverging
207
148
} ) ;
@@ -298,7 +239,7 @@ impl<'tcx> TypeVariableTable<'tcx> {
298
239
debug ! ( "NewElem({}) new_elem_threshold={}" , index, new_elem_threshold) ;
299
240
}
300
241
301
- sv:: UndoLog :: Other ( SpecifyVar ( vid, ..) ) => {
242
+ sv:: UndoLog :: Other ( Instantiate { vid, .. } ) => {
302
243
if vid. index < new_elem_threshold {
303
244
// quick check to see if this variable was
304
245
// created since the snapshot started or not.
@@ -334,35 +275,12 @@ impl<'tcx> TypeVariableTable<'tcx> {
334
275
335
276
impl < ' tcx > sv:: SnapshotVecDelegate for Delegate < ' tcx > {
336
277
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 > ;
347
279
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
+ } ;
367
285
}
368
286
}
0 commit comments