1
- use rustc_ast_ir:: try_visit;
2
- use rustc_ast_ir:: visit:: VisitorResult ;
3
1
#[ cfg( feature = "nightly" ) ]
4
2
use rustc_macros:: { HashStable_NoContext , TyDecodable , TyEncodable } ;
3
+ use rustc_type_ir_macros:: { TypeFoldable_Generic , TypeVisitable_Generic } ;
5
4
use std:: fmt;
6
5
use std:: hash:: Hash ;
7
6
8
- use crate :: fold:: { FallibleTypeFolder , TypeFoldable } ;
9
7
use crate :: inherent:: * ;
10
- use crate :: visit:: { TypeVisitable , TypeVisitor } ;
11
8
use crate :: { Interner , UniverseIndex } ;
12
9
13
10
/// A "canonicalized" type `V` is one where all free inference
14
11
/// variables have been rewritten to "canonical vars". These are
15
12
/// numbered starting from 0 in order of first appearance.
16
13
#[ derive( derivative:: Derivative ) ]
17
- #[ derivative( Clone ( bound = "V: Clone" ) , Hash ( bound = "V: Hash" ) ) ]
14
+ #[ derivative(
15
+ Clone ( bound = "V: Clone" ) ,
16
+ Hash ( bound = "V: Hash" ) ,
17
+ PartialEq ( bound = "V: PartialEq" ) ,
18
+ Eq ( bound = "V: Eq" ) ,
19
+ Debug ( bound = "V: fmt::Debug" ) ,
20
+ Copy ( bound = "V: Copy" )
21
+ ) ]
22
+ #[ derive( TypeVisitable_Generic , TypeFoldable_Generic ) ]
18
23
#[ cfg_attr( feature = "nightly" , derive( TyEncodable , TyDecodable , HashStable_NoContext ) ) ]
19
24
pub struct Canonical < I : Interner , V > {
20
25
pub value : V ,
@@ -64,18 +69,6 @@ impl<I: Interner, V> Canonical<I, V> {
64
69
}
65
70
}
66
71
67
- impl < I : Interner , V : Eq > Eq for Canonical < I , V > { }
68
-
69
- impl < I : Interner , V : PartialEq > PartialEq for Canonical < I , V > {
70
- fn eq ( & self , other : & Self ) -> bool {
71
- let Self { value, max_universe, variables, defining_opaque_types } = self ;
72
- * value == other. value
73
- && * max_universe == other. max_universe
74
- && * variables == other. variables
75
- && * defining_opaque_types == other. defining_opaque_types
76
- }
77
- }
78
-
79
72
impl < I : Interner , V : fmt:: Display > fmt:: Display for Canonical < I , V > {
80
73
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
81
74
let Self { value, max_universe, variables, defining_opaque_types } = self ;
@@ -86,84 +79,25 @@ impl<I: Interner, V: fmt::Display> fmt::Display for Canonical<I, V> {
86
79
}
87
80
}
88
81
89
- impl < I : Interner , V : fmt:: Debug > fmt:: Debug for Canonical < I , V > {
90
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
91
- let Self { value, max_universe, variables, defining_opaque_types } = self ;
92
- f. debug_struct ( "Canonical" )
93
- . field ( "value" , & value)
94
- . field ( "max_universe" , & max_universe)
95
- . field ( "variables" , & variables)
96
- . field ( "defining_opaque_types" , & defining_opaque_types)
97
- . finish ( )
98
- }
99
- }
100
-
101
- impl < I : Interner , V : Copy > Copy for Canonical < I , V > where I :: CanonicalVars : Copy { }
102
-
103
- impl < I : Interner , V : TypeFoldable < I > > TypeFoldable < I > for Canonical < I , V >
104
- where
105
- I :: CanonicalVars : TypeFoldable < I > ,
106
- {
107
- fn try_fold_with < F : FallibleTypeFolder < I > > ( self , folder : & mut F ) -> Result < Self , F :: Error > {
108
- Ok ( Canonical {
109
- value : self . value . try_fold_with ( folder) ?,
110
- max_universe : self . max_universe . try_fold_with ( folder) ?,
111
- variables : self . variables . try_fold_with ( folder) ?,
112
- defining_opaque_types : self . defining_opaque_types ,
113
- } )
114
- }
115
- }
116
-
117
- impl < I : Interner , V : TypeVisitable < I > > TypeVisitable < I > for Canonical < I , V >
118
- where
119
- I :: CanonicalVars : TypeVisitable < I > ,
120
- {
121
- fn visit_with < F : TypeVisitor < I > > ( & self , folder : & mut F ) -> F :: Result {
122
- let Self { value, max_universe, variables, defining_opaque_types } = self ;
123
- try_visit ! ( value. visit_with( folder) ) ;
124
- try_visit ! ( max_universe. visit_with( folder) ) ;
125
- try_visit ! ( defining_opaque_types. visit_with( folder) ) ;
126
- variables. visit_with ( folder)
127
- }
128
- }
129
-
130
82
/// Information about a canonical variable that is included with the
131
83
/// canonical value. This is sufficient information for code to create
132
84
/// a copy of the canonical value in some other inference context,
133
85
/// with fresh inference variables replacing the canonical values.
134
86
#[ derive( derivative:: Derivative ) ]
135
- #[ derivative( Clone ( bound = "" ) , Copy ( bound = "" ) , Hash ( bound = "" ) , Debug ( bound = "" ) ) ]
87
+ #[ derivative(
88
+ Clone ( bound = "" ) ,
89
+ Copy ( bound = "" ) ,
90
+ Hash ( bound = "" ) ,
91
+ Debug ( bound = "" ) ,
92
+ Eq ( bound = "" ) ,
93
+ PartialEq ( bound = "" )
94
+ ) ]
95
+ #[ derive( TypeVisitable_Generic , TypeFoldable_Generic ) ]
136
96
#[ cfg_attr( feature = "nightly" , derive( TyDecodable , TyEncodable , HashStable_NoContext ) ) ]
137
97
pub struct CanonicalVarInfo < I : Interner > {
138
98
pub kind : CanonicalVarKind < I > ,
139
99
}
140
100
141
- impl < I : Interner > PartialEq for CanonicalVarInfo < I > {
142
- fn eq ( & self , other : & Self ) -> bool {
143
- self . kind == other. kind
144
- }
145
- }
146
-
147
- impl < I : Interner > Eq for CanonicalVarInfo < I > { }
148
-
149
- impl < I : Interner > TypeVisitable < I > for CanonicalVarInfo < I >
150
- where
151
- I :: Ty : TypeVisitable < I > ,
152
- {
153
- fn visit_with < V : TypeVisitor < I > > ( & self , visitor : & mut V ) -> V :: Result {
154
- self . kind . visit_with ( visitor)
155
- }
156
- }
157
-
158
- impl < I : Interner > TypeFoldable < I > for CanonicalVarInfo < I >
159
- where
160
- I :: Ty : TypeFoldable < I > ,
161
- {
162
- fn try_fold_with < F : FallibleTypeFolder < I > > ( self , folder : & mut F ) -> Result < Self , F :: Error > {
163
- Ok ( CanonicalVarInfo { kind : self . kind . try_fold_with ( folder) ? } )
164
- }
165
- }
166
-
167
101
impl < I : Interner > CanonicalVarInfo < I > {
168
102
pub fn universe ( self ) -> UniverseIndex {
169
103
self . kind . universe ( )
@@ -216,6 +150,7 @@ impl<I: Interner> CanonicalVarInfo<I> {
216
150
/// that analyzes type-like values.
217
151
#[ derive( derivative:: Derivative ) ]
218
152
#[ derivative( Clone ( bound = "" ) , Copy ( bound = "" ) , Hash ( bound = "" ) , Debug ( bound = "" ) ) ]
153
+ #[ derive( TypeVisitable_Generic , TypeFoldable_Generic ) ]
219
154
#[ cfg_attr( feature = "nightly" , derive( TyDecodable , TyEncodable , HashStable_NoContext ) ) ]
220
155
pub enum CanonicalVarKind < I : Interner > {
221
156
/// Some kind of type inference variable.
@@ -258,51 +193,6 @@ impl<I: Interner> PartialEq for CanonicalVarKind<I> {
258
193
}
259
194
}
260
195
261
- impl < I : Interner > Eq for CanonicalVarKind < I > { }
262
-
263
- impl < I : Interner > TypeVisitable < I > for CanonicalVarKind < I >
264
- where
265
- I :: Ty : TypeVisitable < I > ,
266
- {
267
- fn visit_with < V : TypeVisitor < I > > ( & self , visitor : & mut V ) -> V :: Result {
268
- match self {
269
- CanonicalVarKind :: Ty ( _)
270
- | CanonicalVarKind :: PlaceholderTy ( _)
271
- | CanonicalVarKind :: Region ( _)
272
- | CanonicalVarKind :: PlaceholderRegion ( _)
273
- | CanonicalVarKind :: Effect => V :: Result :: output ( ) ,
274
- CanonicalVarKind :: Const ( _, ty) | CanonicalVarKind :: PlaceholderConst ( _, ty) => {
275
- ty. visit_with ( visitor)
276
- }
277
- }
278
- }
279
- }
280
-
281
- impl < I : Interner > TypeFoldable < I > for CanonicalVarKind < I >
282
- where
283
- I :: Ty : TypeFoldable < I > ,
284
- {
285
- fn try_fold_with < F : FallibleTypeFolder < I > > ( self , folder : & mut F ) -> Result < Self , F :: Error > {
286
- Ok ( match self {
287
- CanonicalVarKind :: Ty ( kind) => CanonicalVarKind :: Ty ( kind) ,
288
- CanonicalVarKind :: Region ( kind) => CanonicalVarKind :: Region ( kind) ,
289
- CanonicalVarKind :: Const ( kind, ty) => {
290
- CanonicalVarKind :: Const ( kind, ty. try_fold_with ( folder) ?)
291
- }
292
- CanonicalVarKind :: PlaceholderTy ( placeholder) => {
293
- CanonicalVarKind :: PlaceholderTy ( placeholder)
294
- }
295
- CanonicalVarKind :: PlaceholderRegion ( placeholder) => {
296
- CanonicalVarKind :: PlaceholderRegion ( placeholder)
297
- }
298
- CanonicalVarKind :: PlaceholderConst ( placeholder, ty) => {
299
- CanonicalVarKind :: PlaceholderConst ( placeholder, ty. try_fold_with ( folder) ?)
300
- }
301
- CanonicalVarKind :: Effect => CanonicalVarKind :: Effect ,
302
- } )
303
- }
304
- }
305
-
306
196
impl < I : Interner > CanonicalVarKind < I > {
307
197
pub fn universe ( self ) -> UniverseIndex {
308
198
match self {
@@ -355,6 +245,7 @@ impl<I: Interner> CanonicalVarKind<I> {
355
245
/// usize or f32). In order to faithfully reproduce a type, we need to
356
246
/// know what set of types a given type variable can be unified with.
357
247
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
248
+ #[ derive( TypeVisitable_Generic , TypeFoldable_Generic ) ]
358
249
#[ cfg_attr( feature = "nightly" , derive( TyDecodable , TyEncodable , HashStable_NoContext ) ) ]
359
250
pub enum CanonicalTyVarKind {
360
251
/// General type variable `?T` that can be unified with arbitrary types.
0 commit comments