aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/traits/chalk/interner.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/traits/chalk/interner.rs')
-rw-r--r--crates/hir_ty/src/traits/chalk/interner.rs89
1 files changed, 59 insertions, 30 deletions
diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs
index bd9395b7e..b6a3cec6d 100644
--- a/crates/hir_ty/src/traits/chalk/interner.rs
+++ b/crates/hir_ty/src/traits/chalk/interner.rs
@@ -2,9 +2,13 @@
2//! representation of the various objects Chalk deals with (types, goals etc.). 2//! representation of the various objects Chalk deals with (types, goals etc.).
3 3
4use super::tls; 4use super::tls;
5use crate::GenericArg;
5use base_db::salsa::InternId; 6use base_db::salsa::InternId;
6use chalk_ir::{GenericArg, Goal, GoalData}; 7use chalk_ir::{Goal, GoalData};
7use hir_def::TypeAliasId; 8use hir_def::{
9 intern::{impl_internable, InternStorage, Internable, Interned},
10 TypeAliasId,
11};
8use smallvec::SmallVec; 12use smallvec::SmallVec;
9use std::{fmt, sync::Arc}; 13use std::{fmt, sync::Arc};
10 14
@@ -26,22 +30,47 @@ pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
26pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; 30pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
27pub(crate) type Variances = chalk_ir::Variances<Interner>; 31pub(crate) type Variances = chalk_ir::Variances<Interner>;
28 32
33#[derive(PartialEq, Eq, Hash, Debug)]
34pub struct InternedWrapper<T>(T);
35
36impl<T> std::ops::Deref for InternedWrapper<T> {
37 type Target = T;
38
39 fn deref(&self) -> &Self::Target {
40 &self.0
41 }
42}
43
44impl_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
29impl chalk_ir::interner::Interner for Interner { 56impl chalk_ir::interner::Interner for Interner {
30 type InternedType = Arc<chalk_ir::TyData<Self>>; 57 type InternedType = Interned<InternedWrapper<chalk_ir::TyData<Interner>>>;
31 type InternedLifetime = chalk_ir::LifetimeData<Self>; 58 type InternedLifetime = Interned<InternedWrapper<chalk_ir::LifetimeData<Self>>>;
32 type InternedConst = Arc<chalk_ir::ConstData<Self>>; 59 type InternedConst = Interned<InternedWrapper<chalk_ir::ConstData<Self>>>;
33 type InternedConcreteConst = (); 60 type InternedConcreteConst = ();
34 type InternedGenericArg = chalk_ir::GenericArgData<Self>; 61 type InternedGenericArg = chalk_ir::GenericArgData<Self>;
35 type InternedGoal = Arc<GoalData<Self>>; 62 type InternedGoal = Arc<GoalData<Self>>;
36 type InternedGoals = Vec<Goal<Self>>; 63 type InternedGoals = Vec<Goal<Self>>;
37 type InternedSubstitution = SmallVec<[GenericArg<Self>; 2]>; 64 type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
38 type InternedProgramClause = Arc<chalk_ir::ProgramClauseData<Self>>; 65 type InternedProgramClause = chalk_ir::ProgramClauseData<Self>;
39 type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>; 66 type InternedProgramClauses = Interned<InternedWrapper<Vec<chalk_ir::ProgramClause<Self>>>>;
40 type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; 67 type InternedQuantifiedWhereClauses =
41 type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; 68 Interned<InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Self>>>>;
42 type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; 69 type InternedVariableKinds = Interned<InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>>;
70 type InternedCanonicalVarKinds =
71 Interned<InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Self>>>>;
43 type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; 72 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>>>;
45 type DefId = InternId; 74 type DefId = InternId;
46 type InternedAdtId = hir_def::AdtId; 75 type InternedAdtId = hir_def::AdtId;
47 type Identifier = TypeAliasId; 76 type Identifier = TypeAliasId;
@@ -99,7 +128,7 @@ impl chalk_ir::interner::Interner for Interner {
99 } 128 }
100 129
101 fn debug_generic_arg( 130 fn debug_generic_arg(
102 parameter: &GenericArg<Interner>, 131 parameter: &GenericArg,
103 fmt: &mut fmt::Formatter<'_>, 132 fmt: &mut fmt::Formatter<'_>,
104 ) -> Option<fmt::Result> { 133 ) -> Option<fmt::Result> {
105 tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) 134 tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt)))
@@ -194,30 +223,30 @@ impl chalk_ir::interner::Interner for Interner {
194 223
195 fn intern_ty(&self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType { 224 fn intern_ty(&self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType {
196 let flags = kind.compute_flags(self); 225 let flags = kind.compute_flags(self);
197 Arc::new(chalk_ir::TyData { kind, flags }) 226 Interned::new(InternedWrapper(chalk_ir::TyData { kind, flags }))
198 } 227 }
199 228
200 fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData<Self> { 229 fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData<Self> {
201 ty 230 &ty.0
202 } 231 }
203 232
204 fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime { 233 fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime {
205 lifetime 234 Interned::new(InternedWrapper(lifetime))
206 } 235 }
207 236
208 fn lifetime_data<'a>( 237 fn lifetime_data<'a>(
209 &self, 238 &self,
210 lifetime: &'a Self::InternedLifetime, 239 lifetime: &'a Self::InternedLifetime,
211 ) -> &'a chalk_ir::LifetimeData<Self> { 240 ) -> &'a chalk_ir::LifetimeData<Self> {
212 lifetime 241 &lifetime.0
213 } 242 }
214 243
215 fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst { 244 fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst {
216 Arc::new(constant) 245 Interned::new(InternedWrapper(constant))
217 } 246 }
218 247
219 fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData<Self> { 248 fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData<Self> {
220 constant 249 &constant.0
221 } 250 }
222 251
223 fn const_eq( 252 fn const_eq(
@@ -264,23 +293,23 @@ impl chalk_ir::interner::Interner for Interner {
264 293
265 fn intern_substitution<E>( 294 fn intern_substitution<E>(
266 &self, 295 &self,
267 data: impl IntoIterator<Item = Result<GenericArg<Self>, E>>, 296 data: impl IntoIterator<Item = Result<GenericArg, E>>,
268 ) -> Result<Self::InternedSubstitution, E> { 297 ) -> Result<Self::InternedSubstitution, E> {
269 data.into_iter().collect() 298 Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
270 } 299 }
271 300
272 fn substitution_data<'a>( 301 fn substitution_data<'a>(
273 &self, 302 &self,
274 substitution: &'a Self::InternedSubstitution, 303 substitution: &'a Self::InternedSubstitution,
275 ) -> &'a [GenericArg<Self>] { 304 ) -> &'a [GenericArg] {
276 substitution 305 &substitution.as_ref().0
277 } 306 }
278 307
279 fn intern_program_clause( 308 fn intern_program_clause(
280 &self, 309 &self,
281 data: chalk_ir::ProgramClauseData<Self>, 310 data: chalk_ir::ProgramClauseData<Self>,
282 ) -> Self::InternedProgramClause { 311 ) -> Self::InternedProgramClause {
283 Arc::new(data) 312 data
284 } 313 }
285 314
286 fn program_clause_data<'a>( 315 fn program_clause_data<'a>(
@@ -294,7 +323,7 @@ impl chalk_ir::interner::Interner for Interner {
294 &self, 323 &self,
295 data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, 324 data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>,
296 ) -> Result<Self::InternedProgramClauses, E> { 325 ) -> Result<Self::InternedProgramClauses, E> {
297 data.into_iter().collect() 326 Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
298 } 327 }
299 328
300 fn program_clauses_data<'a>( 329 fn program_clauses_data<'a>(
@@ -308,7 +337,7 @@ impl chalk_ir::interner::Interner for Interner {
308 &self, 337 &self,
309 data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, 338 data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>,
310 ) -> Result<Self::InternedQuantifiedWhereClauses, E> { 339 ) -> Result<Self::InternedQuantifiedWhereClauses, E> {
311 data.into_iter().collect() 340 Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
312 } 341 }
313 342
314 fn quantified_where_clauses_data<'a>( 343 fn quantified_where_clauses_data<'a>(
@@ -322,21 +351,21 @@ impl chalk_ir::interner::Interner for Interner {
322 &self, 351 &self,
323 data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, 352 data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>,
324 ) -> Result<Self::InternedVariableKinds, E> { 353 ) -> Result<Self::InternedVariableKinds, E> {
325 data.into_iter().collect() 354 Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
326 } 355 }
327 356
328 fn variable_kinds_data<'a>( 357 fn variable_kinds_data<'a>(
329 &self, 358 &self,
330 parameter_kinds: &'a Self::InternedVariableKinds, 359 parameter_kinds: &'a Self::InternedVariableKinds,
331 ) -> &'a [chalk_ir::VariableKind<Self>] { 360 ) -> &'a [chalk_ir::VariableKind<Self>] {
332 &parameter_kinds 361 &parameter_kinds.as_ref().0
333 } 362 }
334 363
335 fn intern_canonical_var_kinds<E>( 364 fn intern_canonical_var_kinds<E>(
336 &self, 365 &self,
337 data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, 366 data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>,
338 ) -> Result<Self::InternedCanonicalVarKinds, E> { 367 ) -> Result<Self::InternedCanonicalVarKinds, E> {
339 data.into_iter().collect() 368 Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
340 } 369 }
341 370
342 fn canonical_var_kinds_data<'a>( 371 fn canonical_var_kinds_data<'a>(
@@ -376,7 +405,7 @@ impl chalk_ir::interner::Interner for Interner {
376 &self, 405 &self,
377 data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>, 406 data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>,
378 ) -> Result<Self::InternedVariances, E> { 407 ) -> Result<Self::InternedVariances, E> {
379 data.into_iter().collect() 408 Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
380 } 409 }
381 410
382 fn variances_data<'a>( 411 fn variances_data<'a>(