aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/traits
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-04-01 20:04:02 +0100
committerFlorian Diebold <[email protected]>2021-04-03 10:17:13 +0100
commite480d81988fc0c0e4f80f1c54058b95b9aaf1ebf (patch)
tree079dabc4e0de72e30d2bd04bd50427ff6950b348 /crates/hir_ty/src/traits
parent327f3a0a3017e047be58b8312f8bf3ac690db3fd (diff)
Introduce `GenericArg` like in Chalk
Plus some more adaptations to Substitution. Lots of `assert_ty_ref` that we should revisit when introducing lifetime/const parameters.
Diffstat (limited to 'crates/hir_ty/src/traits')
-rw-r--r--crates/hir_ty/src/traits/chalk.rs10
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs39
2 files changed, 32 insertions, 17 deletions
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 011bef6f6..cf7ed1e11 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};
@@ -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);
@@ -308,7 +308,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
308 _closure_id: chalk_ir::ClosureId<Interner>, 308 _closure_id: chalk_ir::ClosureId<Interner>,
309 _substs: &chalk_ir::Substitution<Interner>, 309 _substs: &chalk_ir::Substitution<Interner>,
310 ) -> chalk_ir::Substitution<Interner> { 310 ) -> chalk_ir::Substitution<Interner> {
311 Substitution::empty().to_chalk(self.db) 311 Substitution::empty(&Interner).to_chalk(self.db)
312 } 312 }
313 313
314 fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String { 314 fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String {
@@ -439,7 +439,7 @@ pub(crate) fn trait_datum_query(
439 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); 439 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name));
440 let trait_datum = TraitDatum { 440 let trait_datum = TraitDatum {
441 id: trait_id, 441 id: trait_id,
442 binders: make_binders(trait_datum_bound, bound_vars.len()), 442 binders: make_binders(trait_datum_bound, bound_vars.len(&Interner)),
443 flags, 443 flags,
444 associated_ty_ids, 444 associated_ty_ids,
445 well_known, 445 well_known,
@@ -577,7 +577,7 @@ fn impl_def_datum(
577 .collect(); 577 .collect();
578 debug!("impl_datum: {:?}", impl_datum_bound); 578 debug!("impl_datum: {:?}", impl_datum_bound);
579 let impl_datum = ImplDatum { 579 let impl_datum = ImplDatum {
580 binders: make_binders(impl_datum_bound, bound_vars.len()), 580 binders: make_binders(impl_datum_bound, bound_vars.len(&Interner)),
581 impl_type, 581 impl_type,
582 polarity, 582 polarity,
583 associated_ty_value_ids, 583 associated_ty_value_ids,
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index aef6b8a15..452b357e8 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -13,7 +13,7 @@ use crate::{
13 db::HirDatabase, 13 db::HirDatabase,
14 primitive::UintTy, 14 primitive::UintTy,
15 traits::{Canonical, DomainGoal}, 15 traits::{Canonical, DomainGoal},
16 AliasTy, CallableDefId, FnPointer, InEnvironment, OpaqueTy, ProjectionTy, 16 AliasTy, CallableDefId, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy,
17 QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause, 17 QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
18}; 18};
19 19
@@ -137,7 +137,7 @@ impl ToChalk for Ty {
137 db, 137 db,
138 substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), 138 substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"),
139 ); 139 );
140 TyKind::Function(FnPointer { num_args: (substs.len() - 1), sig, substs }) 140 TyKind::Function(FnPointer { num_args: (substs.len(&Interner) - 1), sig, substs })
141 } 141 }
142 chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), 142 chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
143 chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown, 143 chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown,
@@ -216,24 +216,39 @@ fn array_to_chalk(db: &dyn HirDatabase, ty: Ty) -> chalk_ir::Ty<Interner> {
216 chalk_ir::TyKind::Array(arg, const_).intern(&Interner) 216 chalk_ir::TyKind::Array(arg, const_).intern(&Interner)
217} 217}
218 218
219impl ToChalk for GenericArg {
220 type Chalk = chalk_ir::GenericArg<Interner>;
221
222 fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk {
223 match self.interned {
224 crate::GenericArgData::Ty(ty) => ty.to_chalk(db).cast(&Interner),
225 }
226 }
227
228 fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
229 match chalk.interned() {
230 chalk_ir::GenericArgData::Ty(ty) => Ty::from_chalk(db, ty.clone()).cast(&Interner),
231 chalk_ir::GenericArgData::Lifetime(_) => unimplemented!(),
232 chalk_ir::GenericArgData::Const(_) => unimplemented!(),
233 }
234 }
235}
236
219impl ToChalk for Substitution { 237impl ToChalk for Substitution {
220 type Chalk = chalk_ir::Substitution<Interner>; 238 type Chalk = chalk_ir::Substitution<Interner>;
221 239
222 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { 240 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))) 241 chalk_ir::Substitution::from_iter(
242 &Interner,
243 self.iter(&Interner).map(|ty| ty.clone().to_chalk(db)),
244 )
224 } 245 }
225 246
226 fn from_chalk( 247 fn from_chalk(
227 db: &dyn HirDatabase, 248 db: &dyn HirDatabase,
228 parameters: chalk_ir::Substitution<Interner>, 249 parameters: chalk_ir::Substitution<Interner>,
229 ) -> Substitution { 250 ) -> Substitution {
230 let tys = parameters 251 let tys = parameters.iter(&Interner).map(|p| from_chalk(db, p.clone())).collect();
231 .iter(&Interner)
232 .map(|p| match p.ty(&Interner) {
233 Some(ty) => from_chalk(db, ty.clone()),
234 None => unimplemented!(),
235 })
236 .collect();
237 Substitution(tys) 252 Substitution(tys)
238 } 253 }
239} 254}
@@ -531,7 +546,7 @@ pub(super) fn generic_predicate_to_inline_bound(
531 // have the expected self type 546 // have the expected self type
532 return None; 547 return None;
533 } 548 }
534 let args_no_self = trait_ref.substitution[1..] 549 let args_no_self = trait_ref.substitution.interned(&Interner)[1..]
535 .iter() 550 .iter()
536 .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) 551 .map(|ty| ty.clone().to_chalk(db).cast(&Interner))
537 .collect(); 552 .collect();
@@ -543,7 +558,7 @@ pub(super) fn generic_predicate_to_inline_bound(
543 return None; 558 return None;
544 } 559 }
545 let trait_ = projection_ty.trait_(db); 560 let trait_ = projection_ty.trait_(db);
546 let args_no_self = projection_ty.substitution[1..] 561 let args_no_self = projection_ty.substitution.interned(&Interner)[1..]
547 .iter() 562 .iter()
548 .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) 563 .map(|ty| ty.clone().to_chalk(db).cast(&Interner))
549 .collect(); 564 .collect();