aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/traits
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/traits')
-rw-r--r--crates/hir_ty/src/traits/chalk.rs75
-rw-r--r--crates/hir_ty/src/traits/chalk/interner.rs43
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs94
3 files changed, 112 insertions, 100 deletions
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 011bef6f6..dff87ef70 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -3,7 +3,7 @@ use std::sync::Arc;
3 3
4use log::debug; 4use log::debug;
5 5
6use chalk_ir::{fold::shift::Shift, CanonicalVarKinds, GenericArg}; 6use chalk_ir::{fold::shift::Shift, CanonicalVarKinds};
7use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; 7use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait};
8 8
9use base_db::{salsa::InternKey, CrateId}; 9use base_db::{salsa::InternKey, CrateId};
@@ -22,7 +22,7 @@ use crate::{
22 to_assoc_type_id, to_chalk_trait_id, 22 to_assoc_type_id, to_chalk_trait_id,
23 utils::generics, 23 utils::generics,
24 AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, 24 AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution,
25 TraitRef, Ty, TyKind, WhereClause, 25 TraitRef, Ty, TyBuilder, TyKind, WhereClause,
26}; 26};
27use mapping::{ 27use mapping::{
28 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, 28 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue,
@@ -80,7 +80,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
80 fn impls_for_trait( 80 fn impls_for_trait(
81 &self, 81 &self,
82 trait_id: TraitId, 82 trait_id: TraitId,
83 parameters: &[GenericArg<Interner>], 83 parameters: &[chalk_ir::GenericArg<Interner>],
84 binders: &CanonicalVarKinds<Interner>, 84 binders: &CanonicalVarKinds<Interner>,
85 ) -> Vec<ImplId> { 85 ) -> Vec<ImplId> {
86 debug!("impls_for_trait {:?}", trait_id); 86 debug!("impls_for_trait {:?}", trait_id);
@@ -92,7 +92,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
92 ty: &Ty, 92 ty: &Ty,
93 binders: &CanonicalVarKinds<Interner>, 93 binders: &CanonicalVarKinds<Interner>,
94 ) -> Option<chalk_ir::TyVariableKind> { 94 ) -> Option<chalk_ir::TyVariableKind> {
95 if let TyKind::BoundVar(bv) = ty.interned(&Interner) { 95 if let TyKind::BoundVar(bv) = ty.kind(&Interner) {
96 let binders = binders.as_slice(&Interner); 96 let binders = binders.as_slice(&Interner);
97 if bv.debruijn == DebruijnIndex::INNERMOST { 97 if bv.debruijn == DebruijnIndex::INNERMOST {
98 if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { 98 if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind {
@@ -184,16 +184,21 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
184 .db 184 .db
185 .return_type_impl_traits(func) 185 .return_type_impl_traits(func)
186 .expect("impl trait id without impl traits"); 186 .expect("impl trait id without impl traits");
187 let data = &datas.value.impl_traits[idx as usize]; 187 let (datas, binders) = (*datas).as_ref().into_value_and_skipped_binders();
188 let data = &datas.impl_traits[idx as usize];
188 let bound = OpaqueTyDatumBound { 189 let bound = OpaqueTyDatumBound {
189 bounds: make_binders( 190 bounds: make_binders(
190 data.bounds.value.iter().cloned().map(|b| b.to_chalk(self.db)).collect(), 191 data.bounds
192 .skip_binders()
193 .iter()
194 .cloned()
195 .map(|b| b.to_chalk(self.db))
196 .collect(),
191 1, 197 1,
192 ), 198 ),
193 where_clauses: make_binders(vec![], 0), 199 where_clauses: make_binders(vec![], 0),
194 }; 200 };
195 let num_vars = datas.num_binders; 201 chalk_ir::Binders::new(binders, bound)
196 make_binders(bound, num_vars)
197 } 202 }
198 crate::ImplTraitId::AsyncBlockTypeImplTrait(..) => { 203 crate::ImplTraitId::AsyncBlockTypeImplTrait(..) => {
199 if let Some((future_trait, future_output)) = self 204 if let Some((future_trait, future_output)) = self
@@ -265,7 +270,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
265 270
266 fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> { 271 fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> {
267 // FIXME: actually provide the hidden type; it is relevant for auto traits 272 // FIXME: actually provide the hidden type; it is relevant for auto traits
268 TyKind::Unknown.intern(&Interner).to_chalk(self.db) 273 TyKind::Error.intern(&Interner).to_chalk(self.db)
269 } 274 }
270 275
271 fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool { 276 fn is_object_safe(&self, _trait_id: chalk_ir::TraitId<Interner>) -> bool {
@@ -300,7 +305,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
300 _closure_id: chalk_ir::ClosureId<Interner>, 305 _closure_id: chalk_ir::ClosureId<Interner>,
301 _substs: &chalk_ir::Substitution<Interner>, 306 _substs: &chalk_ir::Substitution<Interner>,
302 ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { 307 ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> {
303 let ty = Ty::unit().to_chalk(self.db); 308 let ty = TyBuilder::unit().to_chalk(self.db);
304 make_binders(ty, 0) 309 make_binders(ty, 0)
305 } 310 }
306 fn closure_fn_substitution( 311 fn closure_fn_substitution(
@@ -308,7 +313,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
308 _closure_id: chalk_ir::ClosureId<Interner>, 313 _closure_id: chalk_ir::ClosureId<Interner>,
309 _substs: &chalk_ir::Substitution<Interner>, 314 _substs: &chalk_ir::Substitution<Interner>,
310 ) -> chalk_ir::Substitution<Interner> { 315 ) -> chalk_ir::Substitution<Interner> {
311 Substitution::empty().to_chalk(self.db) 316 Substitution::empty(&Interner).to_chalk(self.db)
312 } 317 }
313 318
314 fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { 319 fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String {
@@ -387,7 +392,7 @@ pub(crate) fn associated_ty_data_query(
387 // Lower bounds -- we could/should maybe move this to a separate query in `lower` 392 // Lower bounds -- we could/should maybe move this to a separate query in `lower`
388 let type_alias_data = db.type_alias_data(type_alias); 393 let type_alias_data = db.type_alias_data(type_alias);
389 let generic_params = generics(db.upcast(), type_alias.into()); 394 let generic_params = generics(db.upcast(), type_alias.into());
390 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 395 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
391 let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); 396 let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast());
392 let ctx = crate::TyLoweringContext::new(db, &resolver) 397 let ctx = crate::TyLoweringContext::new(db, &resolver)
393 .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); 398 .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable);
@@ -421,7 +426,7 @@ pub(crate) fn trait_datum_query(
421 let trait_data = db.trait_data(trait_); 426 let trait_data = db.trait_data(trait_);
422 debug!("trait {:?} = {:?}", trait_id, trait_data.name); 427 debug!("trait {:?} = {:?}", trait_id, trait_data.name);
423 let generic_params = generics(db.upcast(), trait_.into()); 428 let generic_params = generics(db.upcast(), trait_.into());
424 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 429 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
425 let flags = rust_ir::TraitFlags { 430 let flags = rust_ir::TraitFlags {
426 auto: trait_data.is_auto, 431 auto: trait_data.is_auto,
427 upstream: trait_.lookup(db.upcast()).container.krate() != krate, 432 upstream: trait_.lookup(db.upcast()).container.krate() != krate,
@@ -439,7 +444,7 @@ pub(crate) fn trait_datum_query(
439 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); 444 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name));
440 let trait_datum = TraitDatum { 445 let trait_datum = TraitDatum {
441 id: trait_id, 446 id: trait_id,
442 binders: make_binders(trait_datum_bound, bound_vars.len()), 447 binders: make_binders(trait_datum_bound, bound_vars.len(&Interner)),
443 flags, 448 flags,
444 associated_ty_ids, 449 associated_ty_ids,
445 well_known, 450 well_known,
@@ -490,7 +495,7 @@ pub(crate) fn struct_datum_query(
490 let upstream = adt_id.module(db.upcast()).krate() != krate; 495 let upstream = adt_id.module(db.upcast()).krate() != krate;
491 let where_clauses = { 496 let where_clauses = {
492 let generic_params = generics(db.upcast(), adt_id.into()); 497 let generic_params = generics(db.upcast(), adt_id.into());
493 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 498 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
494 convert_where_clauses(db, adt_id.into(), &bound_vars) 499 convert_where_clauses(db, adt_id.into(), &bound_vars)
495 }; 500 };
496 let flags = rust_ir::AdtFlags { 501 let flags = rust_ir::AdtFlags {
@@ -535,11 +540,12 @@ fn impl_def_datum(
535 .impl_trait(impl_id) 540 .impl_trait(impl_id)
536 // ImplIds for impls where the trait ref can't be resolved should never reach Chalk 541 // ImplIds for impls where the trait ref can't be resolved should never reach Chalk
537 .expect("invalid impl passed to Chalk") 542 .expect("invalid impl passed to Chalk")
538 .value; 543 .into_value_and_skipped_binders()
544 .0;
539 let impl_data = db.impl_data(impl_id); 545 let impl_data = db.impl_data(impl_id);
540 546
541 let generic_params = generics(db.upcast(), impl_id.into()); 547 let generic_params = generics(db.upcast(), impl_id.into());
542 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 548 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
543 let trait_ = trait_ref.hir_trait_id(); 549 let trait_ = trait_ref.hir_trait_id();
544 let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate { 550 let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate {
545 rust_ir::ImplType::Local 551 rust_ir::ImplType::Local
@@ -577,7 +583,7 @@ fn impl_def_datum(
577 .collect(); 583 .collect();
578 debug!("impl_datum: {:?}", impl_datum_bound); 584 debug!("impl_datum: {:?}", impl_datum_bound);
579 let impl_datum = ImplDatum { 585 let impl_datum = ImplDatum {
580 binders: make_binders(impl_datum_bound, bound_vars.len()), 586 binders: make_binders(impl_datum_bound, bound_vars.len(&Interner)),
581 impl_type, 587 impl_type,
582 polarity, 588 polarity,
583 associated_ty_value_ids, 589 associated_ty_value_ids,
@@ -605,18 +611,22 @@ fn type_alias_associated_ty_value(
605 _ => panic!("assoc ty value should be in impl"), 611 _ => panic!("assoc ty value should be in impl"),
606 }; 612 };
607 613
608 let trait_ref = db.impl_trait(impl_id).expect("assoc ty value should not exist").value; // we don't return any assoc ty values if the impl'd trait can't be resolved 614 let trait_ref = db
615 .impl_trait(impl_id)
616 .expect("assoc ty value should not exist")
617 .into_value_and_skipped_binders()
618 .0; // we don't return any assoc ty values if the impl'd trait can't be resolved
609 619
610 let assoc_ty = db 620 let assoc_ty = db
611 .trait_data(trait_ref.hir_trait_id()) 621 .trait_data(trait_ref.hir_trait_id())
612 .associated_type_by_name(&type_alias_data.name) 622 .associated_type_by_name(&type_alias_data.name)
613 .expect("assoc ty value should not exist"); // validated when building the impl data as well 623 .expect("assoc ty value should not exist"); // validated when building the impl data as well
614 let ty = db.ty(type_alias.into()); 624 let (ty, binders) = db.ty(type_alias.into()).into_value_and_skipped_binders();
615 let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; 625 let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) };
616 let value = rust_ir::AssociatedTyValue { 626 let value = rust_ir::AssociatedTyValue {
617 impl_id: impl_id.to_chalk(db), 627 impl_id: impl_id.to_chalk(db),
618 associated_ty_id: to_assoc_type_id(assoc_ty), 628 associated_ty_id: to_assoc_type_id(assoc_ty),
619 value: make_binders(value_bound, ty.num_binders), 629 value: chalk_ir::Binders::new(binders, value_bound),
620 }; 630 };
621 Arc::new(value) 631 Arc::new(value)
622} 632}
@@ -628,20 +638,15 @@ pub(crate) fn fn_def_datum_query(
628) -> Arc<FnDefDatum> { 638) -> Arc<FnDefDatum> {
629 let callable_def: CallableDefId = from_chalk(db, fn_def_id); 639 let callable_def: CallableDefId = from_chalk(db, fn_def_id);
630 let generic_params = generics(db.upcast(), callable_def.into()); 640 let generic_params = generics(db.upcast(), callable_def.into());
631 let sig = db.callable_item_signature(callable_def); 641 let (sig, binders) = db.callable_item_signature(callable_def).into_value_and_skipped_binders();
632 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 642 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
633 let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); 643 let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars);
634 let bound = rust_ir::FnDefDatumBound { 644 let bound = rust_ir::FnDefDatumBound {
635 // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway 645 // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway
636 inputs_and_output: make_binders( 646 inputs_and_output: make_binders(
637 rust_ir::FnDefInputsAndOutputDatum { 647 rust_ir::FnDefInputsAndOutputDatum {
638 argument_types: sig 648 argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(),
639 .value 649 return_type: sig.ret().clone().to_chalk(db),
640 .params()
641 .iter()
642 .map(|ty| ty.clone().to_chalk(db))
643 .collect(),
644 return_type: sig.value.ret().clone().to_chalk(db),
645 } 650 }
646 .shifted_in(&Interner), 651 .shifted_in(&Interner),
647 0, 652 0,
@@ -650,12 +655,8 @@ pub(crate) fn fn_def_datum_query(
650 }; 655 };
651 let datum = FnDefDatum { 656 let datum = FnDefDatum {
652 id: fn_def_id, 657 id: fn_def_id,
653 sig: chalk_ir::FnSig { 658 sig: chalk_ir::FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: sig.is_varargs },
654 abi: (), 659 binders: chalk_ir::Binders::new(binders, bound),
655 safety: chalk_ir::Safety::Safe,
656 variadic: sig.value.is_varargs,
657 },
658 binders: make_binders(bound, sig.num_binders),
659 }; 660 };
660 Arc::new(datum) 661 Arc::new(datum)
661} 662}
diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs
index 94e94a26d..bd9395b7e 100644
--- a/crates/hir_ty/src/traits/chalk/interner.rs
+++ b/crates/hir_ty/src/traits/chalk/interner.rs
@@ -192,59 +192,58 @@ impl chalk_ir::interner::Interner for Interner {
192 tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt))) 192 tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt)))
193 } 193 }
194 194
195 fn intern_ty(&self, kind: chalk_ir::TyKind<Self>) -> Arc<chalk_ir::TyData<Self>> { 195 fn intern_ty(&self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType {
196 let flags = kind.compute_flags(self); 196 let flags = kind.compute_flags(self);
197 Arc::new(chalk_ir::TyData { kind, flags }) 197 Arc::new(chalk_ir::TyData { kind, flags })
198 } 198 }
199 199
200 fn ty_data<'a>(&self, ty: &'a Arc<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> { 200 fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData<Self> {
201 ty 201 ty
202 } 202 }
203 203
204 fn intern_lifetime( 204 fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime {
205 &self,
206 lifetime: chalk_ir::LifetimeData<Self>,
207 ) -> chalk_ir::LifetimeData<Self> {
208 lifetime 205 lifetime
209 } 206 }
210 207
211 fn lifetime_data<'a>( 208 fn lifetime_data<'a>(
212 &self, 209 &self,
213 lifetime: &'a chalk_ir::LifetimeData<Self>, 210 lifetime: &'a Self::InternedLifetime,
214 ) -> &'a chalk_ir::LifetimeData<Self> { 211 ) -> &'a chalk_ir::LifetimeData<Self> {
215 lifetime 212 lifetime
216 } 213 }
217 214
218 fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Arc<chalk_ir::ConstData<Self>> { 215 fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst {
219 Arc::new(constant) 216 Arc::new(constant)
220 } 217 }
221 218
222 fn const_data<'a>( 219 fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData<Self> {
223 &self,
224 constant: &'a Arc<chalk_ir::ConstData<Self>>,
225 ) -> &'a chalk_ir::ConstData<Self> {
226 constant 220 constant
227 } 221 }
228 222
229 fn const_eq(&self, _ty: &Arc<chalk_ir::TyData<Self>>, _c1: &(), _c2: &()) -> bool { 223 fn const_eq(
224 &self,
225 _ty: &Self::InternedType,
226 _c1: &Self::InternedConcreteConst,
227 _c2: &Self::InternedConcreteConst,
228 ) -> bool {
230 true 229 true
231 } 230 }
232 231
233 fn intern_generic_arg( 232 fn intern_generic_arg(
234 &self, 233 &self,
235 parameter: chalk_ir::GenericArgData<Self>, 234 parameter: chalk_ir::GenericArgData<Self>,
236 ) -> chalk_ir::GenericArgData<Self> { 235 ) -> Self::InternedGenericArg {
237 parameter 236 parameter
238 } 237 }
239 238
240 fn generic_arg_data<'a>( 239 fn generic_arg_data<'a>(
241 &self, 240 &self,
242 parameter: &'a chalk_ir::GenericArgData<Self>, 241 parameter: &'a Self::InternedGenericArg,
243 ) -> &'a chalk_ir::GenericArgData<Self> { 242 ) -> &'a chalk_ir::GenericArgData<Self> {
244 parameter 243 parameter
245 } 244 }
246 245
247 fn intern_goal(&self, goal: GoalData<Self>) -> Arc<GoalData<Self>> { 246 fn intern_goal(&self, goal: GoalData<Self>) -> Self::InternedGoal {
248 Arc::new(goal) 247 Arc::new(goal)
249 } 248 }
250 249
@@ -255,11 +254,11 @@ impl chalk_ir::interner::Interner for Interner {
255 data.into_iter().collect() 254 data.into_iter().collect()
256 } 255 }
257 256
258 fn goal_data<'a>(&self, goal: &'a Arc<GoalData<Self>>) -> &'a GoalData<Self> { 257 fn goal_data<'a>(&self, goal: &'a Self::InternedGoal) -> &'a GoalData<Self> {
259 goal 258 goal
260 } 259 }
261 260
262 fn goals_data<'a>(&self, goals: &'a Vec<Goal<Interner>>) -> &'a [Goal<Interner>] { 261 fn goals_data<'a>(&self, goals: &'a Self::InternedGoals) -> &'a [Goal<Interner>] {
263 goals 262 goals
264 } 263 }
265 264
@@ -280,13 +279,13 @@ impl chalk_ir::interner::Interner for Interner {
280 fn intern_program_clause( 279 fn intern_program_clause(
281 &self, 280 &self,
282 data: chalk_ir::ProgramClauseData<Self>, 281 data: chalk_ir::ProgramClauseData<Self>,
283 ) -> Arc<chalk_ir::ProgramClauseData<Self>> { 282 ) -> Self::InternedProgramClause {
284 Arc::new(data) 283 Arc::new(data)
285 } 284 }
286 285
287 fn program_clause_data<'a>( 286 fn program_clause_data<'a>(
288 &self, 287 &self,
289 clause: &'a Arc<chalk_ir::ProgramClauseData<Self>>, 288 clause: &'a Self::InternedProgramClause,
290 ) -> &'a chalk_ir::ProgramClauseData<Self> { 289 ) -> &'a chalk_ir::ProgramClauseData<Self> {
291 clause 290 clause
292 } 291 }
@@ -294,13 +293,13 @@ impl chalk_ir::interner::Interner for Interner {
294 fn intern_program_clauses<E>( 293 fn intern_program_clauses<E>(
295 &self, 294 &self,
296 data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, 295 data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>,
297 ) -> Result<Arc<[chalk_ir::ProgramClause<Self>]>, E> { 296 ) -> Result<Self::InternedProgramClauses, E> {
298 data.into_iter().collect() 297 data.into_iter().collect()
299 } 298 }
300 299
301 fn program_clauses_data<'a>( 300 fn program_clauses_data<'a>(
302 &self, 301 &self,
303 clauses: &'a Arc<[chalk_ir::ProgramClause<Self>]>, 302 clauses: &'a Self::InternedProgramClauses,
304 ) -> &'a [chalk_ir::ProgramClause<Self>] { 303 ) -> &'a [chalk_ir::ProgramClause<Self>] {
305 &clauses 304 &clauses
306 } 305 }
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index aef6b8a15..c3b148cab 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -10,11 +10,9 @@ use base_db::salsa::InternKey;
10use hir_def::{GenericDefId, TypeAliasId}; 10use hir_def::{GenericDefId, TypeAliasId};
11 11
12use crate::{ 12use crate::{
13 db::HirDatabase, 13 db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId, Canonical, DomainGoal, FnPointer,
14 primitive::UintTy, 14 GenericArg, InEnvironment, OpaqueTy, ProjectionTy, QuantifiedWhereClause, Scalar, Substitution,
15 traits::{Canonical, DomainGoal}, 15 TraitRef, Ty, TypeWalk, WhereClause,
16 AliasTy, CallableDefId, FnPointer, InEnvironment, OpaqueTy, ProjectionTy,
17 QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
18}; 16};
19 17
20use super::interner::*; 18use super::interner::*;
@@ -45,7 +43,7 @@ impl ToChalk for Ty {
45 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) 43 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
46 } 44 }
47 45
48 TyKind::ForeignType(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner), 46 TyKind::Foreign(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner),
49 47
50 TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), 48 TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
51 49
@@ -95,22 +93,23 @@ impl ToChalk for Ty {
95 TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), 93 TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
96 TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"), 94 TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"),
97 TyKind::Dyn(dyn_ty) => { 95 TyKind::Dyn(dyn_ty) => {
96 let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders();
98 let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( 97 let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter(
99 &Interner, 98 &Interner,
100 dyn_ty.bounds.value.interned().iter().cloned().map(|p| p.to_chalk(db)), 99 bounds.interned().iter().cloned().map(|p| p.to_chalk(db)),
101 ); 100 );
102 let bounded_ty = chalk_ir::DynTy { 101 let bounded_ty = chalk_ir::DynTy {
103 bounds: make_binders(where_clauses, 1), 102 bounds: chalk_ir::Binders::new(binders, where_clauses),
104 lifetime: LifetimeData::Static.intern(&Interner), 103 lifetime: LifetimeData::Static.intern(&Interner),
105 }; 104 };
106 chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) 105 chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
107 } 106 }
108 TyKind::Unknown => chalk_ir::TyKind::Error.intern(&Interner), 107 TyKind::Error => chalk_ir::TyKind::Error.intern(&Interner),
109 } 108 }
110 } 109 }
111 fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { 110 fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
112 match chalk.data(&Interner).kind.clone() { 111 match chalk.data(&Interner).kind.clone() {
113 chalk_ir::TyKind::Error => TyKind::Unknown, 112 chalk_ir::TyKind::Error => TyKind::Error,
114 chalk_ir::TyKind::Array(ty, _size) => TyKind::Array(from_chalk(db, ty)), 113 chalk_ir::TyKind::Array(ty, _size) => TyKind::Array(from_chalk(db, ty)),
115 chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx), 114 chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx),
116 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { 115 chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
@@ -137,10 +136,10 @@ impl ToChalk for Ty {
137 db, 136 db,
138 substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), 137 substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"),
139 ); 138 );
140 TyKind::Function(FnPointer { num_args: (substs.len() - 1), sig, substs }) 139 TyKind::Function(FnPointer { num_args: (substs.len(&Interner) - 1), sig, substs })
141 } 140 }
142 chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), 141 chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
143 chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown, 142 chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error,
144 chalk_ir::TyKind::Dyn(where_clauses) => { 143 chalk_ir::TyKind::Dyn(where_clauses) => {
145 assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); 144 assert_eq!(where_clauses.bounds.binders.len(&Interner), 1);
146 let bounds = where_clauses 145 let bounds = where_clauses
@@ -150,7 +149,7 @@ impl ToChalk for Ty {
150 .map(|c| from_chalk(db, c.clone())); 149 .map(|c| from_chalk(db, c.clone()));
151 TyKind::Dyn(crate::DynTy { 150 TyKind::Dyn(crate::DynTy {
152 bounds: crate::Binders::new( 151 bounds: crate::Binders::new(
153 1, 152 where_clauses.bounds.binders.clone(),
154 crate::QuantifiedWhereClauses::from_iter(&Interner, bounds), 153 crate::QuantifiedWhereClauses::from_iter(&Interner, bounds),
155 ), 154 ),
156 }) 155 })
@@ -183,7 +182,7 @@ impl ToChalk for Ty {
183 182
184 chalk_ir::TyKind::Closure(id, subst) => TyKind::Closure(id, from_chalk(db, subst)), 183 chalk_ir::TyKind::Closure(id, subst) => TyKind::Closure(id, from_chalk(db, subst)),
185 184
186 chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::ForeignType(foreign_def_id), 185 chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::Foreign(foreign_def_id),
187 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME 186 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
188 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME 187 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
189 } 188 }
@@ -216,25 +215,40 @@ fn array_to_chalk(db: &dyn HirDatabase, ty: Ty) -> chalk_ir::Ty<Interner> {
216 chalk_ir::TyKind::Array(arg, const_).intern(&Interner) 215 chalk_ir::TyKind::Array(arg, const_).intern(&Interner)
217} 216}
218 217
218impl ToChalk for GenericArg {
219 type Chalk = chalk_ir::GenericArg<Interner>;
220
221 fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk {
222 match self.interned() {
223 crate::GenericArgData::Ty(ty) => ty.clone().to_chalk(db).cast(&Interner),
224 }
225 }
226
227 fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
228 match chalk.interned() {
229 chalk_ir::GenericArgData::Ty(ty) => Ty::from_chalk(db, ty.clone()).cast(&Interner),
230 chalk_ir::GenericArgData::Lifetime(_) => unimplemented!(),
231 chalk_ir::GenericArgData::Const(_) => unimplemented!(),
232 }
233 }
234}
235
219impl ToChalk for Substitution { 236impl ToChalk for Substitution {
220 type Chalk = chalk_ir::Substitution<Interner>; 237 type Chalk = chalk_ir::Substitution<Interner>;
221 238
222 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { 239 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> {
223 chalk_ir::Substitution::from_iter(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) 240 chalk_ir::Substitution::from_iter(
241 &Interner,
242 self.iter(&Interner).map(|ty| ty.clone().to_chalk(db)),
243 )
224 } 244 }
225 245
226 fn from_chalk( 246 fn from_chalk(
227 db: &dyn HirDatabase, 247 db: &dyn HirDatabase,
228 parameters: chalk_ir::Substitution<Interner>, 248 parameters: chalk_ir::Substitution<Interner>,
229 ) -> Substitution { 249 ) -> Substitution {
230 let tys = parameters 250 let tys = parameters.iter(&Interner).map(|p| from_chalk(db, p.clone())).collect();
231 .iter(&Interner) 251 Substitution::intern(tys)
232 .map(|p| match p.ty(&Interner) {
233 Some(ty) => from_chalk(db, ty.clone()),
234 None => unimplemented!(),
235 })
236 .collect();
237 Substitution(tys)
238 } 252 }
239} 253}
240 254
@@ -473,19 +487,13 @@ where
473 type Chalk = chalk_ir::Binders<T::Chalk>; 487 type Chalk = chalk_ir::Binders<T::Chalk>;
474 488
475 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Binders<T::Chalk> { 489 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Binders<T::Chalk> {
476 chalk_ir::Binders::new( 490 let (value, binders) = self.into_value_and_skipped_binders();
477 chalk_ir::VariableKinds::from_iter( 491 chalk_ir::Binders::new(binders, value.to_chalk(db))
478 &Interner,
479 std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
480 .take(self.num_binders),
481 ),
482 self.value.to_chalk(db),
483 )
484 } 492 }
485 493
486 fn from_chalk(db: &dyn HirDatabase, binders: chalk_ir::Binders<T::Chalk>) -> crate::Binders<T> { 494 fn from_chalk(db: &dyn HirDatabase, binders: chalk_ir::Binders<T::Chalk>) -> crate::Binders<T> {
487 let (v, b) = binders.into_value_and_skipped_binders(); 495 let (v, b) = binders.into_value_and_skipped_binders();
488 crate::Binders::new(b.len(&Interner), from_chalk(db, v)) 496 crate::Binders::new(b, from_chalk(db, v))
489 } 497 }
490} 498}
491 499
@@ -511,7 +519,7 @@ pub(super) fn convert_where_clauses(
511 let generic_predicates = db.generic_predicates(def); 519 let generic_predicates = db.generic_predicates(def);
512 let mut result = Vec::with_capacity(generic_predicates.len()); 520 let mut result = Vec::with_capacity(generic_predicates.len());
513 for pred in generic_predicates.iter() { 521 for pred in generic_predicates.iter() {
514 result.push(pred.clone().subst(substs).to_chalk(db)); 522 result.push(pred.clone().substitute(&Interner, substs).to_chalk(db));
515 } 523 }
516 result 524 result
517} 525}
@@ -523,27 +531,28 @@ pub(super) fn generic_predicate_to_inline_bound(
523) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> { 531) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> {
524 // An InlineBound is like a GenericPredicate, except the self type is left out. 532 // An InlineBound is like a GenericPredicate, except the self type is left out.
525 // We don't have a special type for this, but Chalk does. 533 // We don't have a special type for this, but Chalk does.
526 let self_ty_shifted_in = self_ty.clone().shift_bound_vars(DebruijnIndex::ONE); 534 let self_ty_shifted_in = self_ty.clone().shifted_in_from(DebruijnIndex::ONE);
527 match &pred.value { 535 let (pred, binders) = pred.as_ref().into_value_and_skipped_binders();
536 match pred {
528 WhereClause::Implemented(trait_ref) => { 537 WhereClause::Implemented(trait_ref) => {
529 if trait_ref.self_type_parameter() != &self_ty_shifted_in { 538 if trait_ref.self_type_parameter(&Interner) != &self_ty_shifted_in {
530 // we can only convert predicates back to type bounds if they 539 // we can only convert predicates back to type bounds if they
531 // have the expected self type 540 // have the expected self type
532 return None; 541 return None;
533 } 542 }
534 let args_no_self = trait_ref.substitution[1..] 543 let args_no_self = trait_ref.substitution.interned()[1..]
535 .iter() 544 .iter()
536 .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) 545 .map(|ty| ty.clone().to_chalk(db).cast(&Interner))
537 .collect(); 546 .collect();
538 let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self }; 547 let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self };
539 Some(make_binders(rust_ir::InlineBound::TraitBound(trait_bound), pred.num_binders)) 548 Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound)))
540 } 549 }
541 WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => { 550 WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
542 if projection_ty.self_type_parameter() != &self_ty_shifted_in { 551 if projection_ty.self_type_parameter(&Interner) != &self_ty_shifted_in {
543 return None; 552 return None;
544 } 553 }
545 let trait_ = projection_ty.trait_(db); 554 let trait_ = projection_ty.trait_(db);
546 let args_no_self = projection_ty.substitution[1..] 555 let args_no_self = projection_ty.substitution.interned()[1..]
547 .iter() 556 .iter()
548 .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) 557 .map(|ty| ty.clone().to_chalk(db).cast(&Interner))
549 .collect(); 558 .collect();
@@ -553,7 +562,10 @@ pub(super) fn generic_predicate_to_inline_bound(
553 associated_ty_id: projection_ty.associated_ty_id, 562 associated_ty_id: projection_ty.associated_ty_id,
554 parameters: Vec::new(), // FIXME we don't support generic associated types yet 563 parameters: Vec::new(), // FIXME we don't support generic associated types yet
555 }; 564 };
556 Some(make_binders(rust_ir::InlineBound::AliasEqBound(alias_eq_bound), pred.num_binders)) 565 Some(chalk_ir::Binders::new(
566 binders,
567 rust_ir::InlineBound::AliasEqBound(alias_eq_bound),
568 ))
557 } 569 }
558 _ => None, 570 _ => None,
559 } 571 }