22//! representation of the various objects Chalk deals with (types, goals etc.).
33
44use super :: tls;
5+ use crate :: GenericArg ;
56use base_db:: salsa:: InternId ;
6- use chalk_ir:: { GenericArg , Goal , GoalData } ;
7- use hir_def:: TypeAliasId ;
7+ use chalk_ir:: { Goal , GoalData } ;
8+ use hir_def:: {
9+ intern:: { impl_internable, InternStorage , Internable , Interned } ,
10+ TypeAliasId ,
11+ } ;
812use smallvec:: SmallVec ;
913use std:: { fmt, sync:: Arc } ;
1014
@@ -26,22 +30,47 @@ pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
2630pub ( crate ) type OpaqueTyDatum = chalk_solve:: rust_ir:: OpaqueTyDatum < Interner > ;
2731pub ( crate ) type Variances = chalk_ir:: Variances < Interner > ;
2832
33+ #[ derive( PartialEq , Eq , Hash , Debug ) ]
34+ pub struct InternedWrapper < T > ( T ) ;
35+
36+ impl < T > std:: ops:: Deref for InternedWrapper < T > {
37+ type Target = T ;
38+
39+ fn deref ( & self ) -> & Self :: Target {
40+ & self . 0
41+ }
42+ }
43+
44+ impl_internable ! (
45+ InternedWrapper <Vec <chalk_ir:: VariableKind <Interner >>>,
46+ InternedWrapper <SmallVec <[ GenericArg ; 2 ] >>,
47+ InternedWrapper <chalk_ir:: TyData <Interner >>,
48+ InternedWrapper <chalk_ir:: LifetimeData <Interner >>,
49+ InternedWrapper <chalk_ir:: ConstData <Interner >>,
50+ InternedWrapper <Vec <chalk_ir:: CanonicalVarKind <Interner >>>,
51+ InternedWrapper <Vec <chalk_ir:: ProgramClause <Interner >>>,
52+ InternedWrapper <Vec <chalk_ir:: QuantifiedWhereClause <Interner >>>,
53+ InternedWrapper <Vec <chalk_ir:: Variance >>,
54+ ) ;
55+
2956impl chalk_ir:: interner:: Interner for Interner {
30- type InternedType = Arc < chalk_ir:: TyData < Self > > ;
31- type InternedLifetime = chalk_ir:: LifetimeData < Self > ;
32- type InternedConst = Arc < chalk_ir:: ConstData < Self > > ;
57+ type InternedType = Interned < InternedWrapper < chalk_ir:: TyData < Interner > > > ;
58+ type InternedLifetime = Interned < InternedWrapper < chalk_ir:: LifetimeData < Self > > > ;
59+ type InternedConst = Interned < InternedWrapper < chalk_ir:: ConstData < Self > > > ;
3360 type InternedConcreteConst = ( ) ;
3461 type InternedGenericArg = chalk_ir:: GenericArgData < Self > ;
3562 type InternedGoal = Arc < GoalData < Self > > ;
3663 type InternedGoals = Vec < Goal < Self > > ;
37- type InternedSubstitution = SmallVec < [ GenericArg < Self > ; 2 ] > ;
38- type InternedProgramClause = Arc < chalk_ir:: ProgramClauseData < Self > > ;
39- type InternedProgramClauses = Arc < [ chalk_ir:: ProgramClause < Self > ] > ;
40- type InternedQuantifiedWhereClauses = Vec < chalk_ir:: QuantifiedWhereClause < Self > > ;
41- type InternedVariableKinds = Vec < chalk_ir:: VariableKind < Self > > ;
42- type InternedCanonicalVarKinds = Vec < chalk_ir:: CanonicalVarKind < Self > > ;
64+ type InternedSubstitution = Interned < InternedWrapper < SmallVec < [ GenericArg ; 2 ] > > > ;
65+ type InternedProgramClause = chalk_ir:: ProgramClauseData < Self > ;
66+ type InternedProgramClauses = Interned < InternedWrapper < Vec < chalk_ir:: ProgramClause < Self > > > > ;
67+ type InternedQuantifiedWhereClauses =
68+ Interned < InternedWrapper < Vec < chalk_ir:: QuantifiedWhereClause < Self > > > > ;
69+ type InternedVariableKinds = Interned < InternedWrapper < Vec < chalk_ir:: VariableKind < Interner > > > > ;
70+ type InternedCanonicalVarKinds =
71+ Interned < InternedWrapper < Vec < chalk_ir:: CanonicalVarKind < Self > > > > ;
4372 type InternedConstraints = Vec < chalk_ir:: InEnvironment < chalk_ir:: Constraint < Self > > > ;
44- type InternedVariances = Arc < [ chalk_ir:: Variance ] > ;
73+ type InternedVariances = Interned < InternedWrapper < Vec < chalk_ir:: Variance > > > ;
4574 type DefId = InternId ;
4675 type InternedAdtId = hir_def:: AdtId ;
4776 type Identifier = TypeAliasId ;
@@ -99,7 +128,7 @@ impl chalk_ir::interner::Interner for Interner {
99128 }
100129
101130 fn debug_generic_arg (
102- parameter : & GenericArg < Interner > ,
131+ parameter : & GenericArg ,
103132 fmt : & mut fmt:: Formatter < ' _ > ,
104133 ) -> Option < fmt:: Result > {
105134 tls:: with_current_program ( |prog| Some ( prog?. debug_generic_arg ( parameter, fmt) ) )
@@ -194,30 +223,30 @@ impl chalk_ir::interner::Interner for Interner {
194223
195224 fn intern_ty ( & self , kind : chalk_ir:: TyKind < Self > ) -> Self :: InternedType {
196225 let flags = kind. compute_flags ( self ) ;
197- Arc :: new ( chalk_ir:: TyData { kind, flags } )
226+ Interned :: new ( InternedWrapper ( chalk_ir:: TyData { kind, flags } ) )
198227 }
199228
200229 fn ty_data < ' a > ( & self , ty : & ' a Self :: InternedType ) -> & ' a chalk_ir:: TyData < Self > {
201- ty
230+ & ty . 0
202231 }
203232
204233 fn intern_lifetime ( & self , lifetime : chalk_ir:: LifetimeData < Self > ) -> Self :: InternedLifetime {
205- lifetime
234+ Interned :: new ( InternedWrapper ( lifetime) )
206235 }
207236
208237 fn lifetime_data < ' a > (
209238 & self ,
210239 lifetime : & ' a Self :: InternedLifetime ,
211240 ) -> & ' a chalk_ir:: LifetimeData < Self > {
212- lifetime
241+ & lifetime. 0
213242 }
214243
215244 fn intern_const ( & self , constant : chalk_ir:: ConstData < Self > ) -> Self :: InternedConst {
216- Arc :: new ( constant)
245+ Interned :: new ( InternedWrapper ( constant) )
217246 }
218247
219248 fn const_data < ' a > ( & self , constant : & ' a Self :: InternedConst ) -> & ' a chalk_ir:: ConstData < Self > {
220- constant
249+ & constant. 0
221250 }
222251
223252 fn const_eq (
@@ -264,23 +293,23 @@ impl chalk_ir::interner::Interner for Interner {
264293
265294 fn intern_substitution < E > (
266295 & self ,
267- data : impl IntoIterator < Item = Result < GenericArg < Self > , E > > ,
296+ data : impl IntoIterator < Item = Result < GenericArg , E > > ,
268297 ) -> Result < Self :: InternedSubstitution , E > {
269- data. into_iter ( ) . collect ( )
298+ Ok ( Interned :: new ( InternedWrapper ( data. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ? ) ) )
270299 }
271300
272301 fn substitution_data < ' a > (
273302 & self ,
274303 substitution : & ' a Self :: InternedSubstitution ,
275- ) -> & ' a [ GenericArg < Self > ] {
276- substitution
304+ ) -> & ' a [ GenericArg ] {
305+ & substitution. as_ref ( ) . 0
277306 }
278307
279308 fn intern_program_clause (
280309 & self ,
281310 data : chalk_ir:: ProgramClauseData < Self > ,
282311 ) -> Self :: InternedProgramClause {
283- Arc :: new ( data)
312+ data
284313 }
285314
286315 fn program_clause_data < ' a > (
@@ -294,7 +323,7 @@ impl chalk_ir::interner::Interner for Interner {
294323 & self ,
295324 data : impl IntoIterator < Item = Result < chalk_ir:: ProgramClause < Self > , E > > ,
296325 ) -> Result < Self :: InternedProgramClauses , E > {
297- data. into_iter ( ) . collect ( )
326+ Ok ( Interned :: new ( InternedWrapper ( data. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ? ) ) )
298327 }
299328
300329 fn program_clauses_data < ' a > (
@@ -308,7 +337,7 @@ impl chalk_ir::interner::Interner for Interner {
308337 & self ,
309338 data : impl IntoIterator < Item = Result < chalk_ir:: QuantifiedWhereClause < Self > , E > > ,
310339 ) -> Result < Self :: InternedQuantifiedWhereClauses , E > {
311- data. into_iter ( ) . collect ( )
340+ Ok ( Interned :: new ( InternedWrapper ( data. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ? ) ) )
312341 }
313342
314343 fn quantified_where_clauses_data < ' a > (
@@ -322,21 +351,21 @@ impl chalk_ir::interner::Interner for Interner {
322351 & self ,
323352 data : impl IntoIterator < Item = Result < chalk_ir:: VariableKind < Self > , E > > ,
324353 ) -> Result < Self :: InternedVariableKinds , E > {
325- data. into_iter ( ) . collect ( )
354+ Ok ( Interned :: new ( InternedWrapper ( data. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ? ) ) )
326355 }
327356
328357 fn variable_kinds_data < ' a > (
329358 & self ,
330359 parameter_kinds : & ' a Self :: InternedVariableKinds ,
331360 ) -> & ' a [ chalk_ir:: VariableKind < Self > ] {
332- & parameter_kinds
361+ & parameter_kinds. as_ref ( ) . 0
333362 }
334363
335364 fn intern_canonical_var_kinds < E > (
336365 & self ,
337366 data : impl IntoIterator < Item = Result < chalk_ir:: CanonicalVarKind < Self > , E > > ,
338367 ) -> Result < Self :: InternedCanonicalVarKinds , E > {
339- data. into_iter ( ) . collect ( )
368+ Ok ( Interned :: new ( InternedWrapper ( data. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ? ) ) )
340369 }
341370
342371 fn canonical_var_kinds_data < ' a > (
@@ -376,7 +405,7 @@ impl chalk_ir::interner::Interner for Interner {
376405 & self ,
377406 data : impl IntoIterator < Item = Result < chalk_ir:: Variance , E > > ,
378407 ) -> Result < Self :: InternedVariances , E > {
379- data. into_iter ( ) . collect ( )
408+ Ok ( Interned :: new ( InternedWrapper ( data. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ? ) ) )
380409 }
381410
382411 fn variances_data < ' a > (
0 commit comments