1- use rustc_ast_ir:: try_visit;
2- use rustc_ast_ir:: visit:: VisitorResult ;
31#[ cfg( feature = "nightly" ) ]
42use rustc_macros:: { HashStable_NoContext , TyDecodable , TyEncodable } ;
3+ use rustc_type_ir_macros:: { TypeFoldable_Generic , TypeVisitable_Generic } ;
54use std:: fmt;
65use std:: hash:: Hash ;
76
8- use crate :: fold:: { FallibleTypeFolder , TypeFoldable } ;
97use crate :: inherent:: * ;
10- use crate :: visit:: { TypeVisitable , TypeVisitor } ;
118use crate :: { Interner , UniverseIndex } ;
129
1310/// A "canonicalized" type `V` is one where all free inference
1411/// variables have been rewritten to "canonical vars". These are
1512/// numbered starting from 0 in order of first appearance.
1613#[ 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 ) ]
1823#[ cfg_attr( feature = "nightly" , derive( TyEncodable , TyDecodable , HashStable_NoContext ) ) ]
1924pub struct Canonical < I : Interner , V > {
2025 pub value : V ,
@@ -64,18 +69,6 @@ impl<I: Interner, V> Canonical<I, V> {
6469 }
6570}
6671
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-
7972impl < I : Interner , V : fmt:: Display > fmt:: Display for Canonical < I , V > {
8073 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
8174 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> {
8679 }
8780}
8881
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-
13082/// Information about a canonical variable that is included with the
13183/// canonical value. This is sufficient information for code to create
13284/// a copy of the canonical value in some other inference context,
13385/// with fresh inference variables replacing the canonical values.
13486#[ 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 ) ]
13696#[ cfg_attr( feature = "nightly" , derive( TyDecodable , TyEncodable , HashStable_NoContext ) ) ]
13797pub struct CanonicalVarInfo < I : Interner > {
13898 pub kind : CanonicalVarKind < I > ,
13999}
140100
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-
167101impl < I : Interner > CanonicalVarInfo < I > {
168102 pub fn universe ( self ) -> UniverseIndex {
169103 self . kind . universe ( )
@@ -216,6 +150,7 @@ impl<I: Interner> CanonicalVarInfo<I> {
216150/// that analyzes type-like values.
217151#[ derive( derivative:: Derivative ) ]
218152#[ derivative( Clone ( bound = "" ) , Copy ( bound = "" ) , Hash ( bound = "" ) , Debug ( bound = "" ) ) ]
153+ #[ derive( TypeVisitable_Generic , TypeFoldable_Generic ) ]
219154#[ cfg_attr( feature = "nightly" , derive( TyDecodable , TyEncodable , HashStable_NoContext ) ) ]
220155pub enum CanonicalVarKind < I : Interner > {
221156 /// Some kind of type inference variable.
@@ -258,51 +193,6 @@ impl<I: Interner> PartialEq for CanonicalVarKind<I> {
258193 }
259194}
260195
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-
306196impl < I : Interner > CanonicalVarKind < I > {
307197 pub fn universe ( self ) -> UniverseIndex {
308198 match self {
@@ -355,6 +245,7 @@ impl<I: Interner> CanonicalVarKind<I> {
355245/// usize or f32). In order to faithfully reproduce a type, we need to
356246/// know what set of types a given type variable can be unified with.
357247#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
248+ #[ derive( TypeVisitable_Generic , TypeFoldable_Generic ) ]
358249#[ cfg_attr( feature = "nightly" , derive( TyDecodable , TyEncodable , HashStable_NoContext ) ) ]
359250pub enum CanonicalTyVarKind {
360251 /// General type variable `?T` that can be unified with arbitrary types.
0 commit comments