diff options
Diffstat (limited to 'crates/hir_ty/src/traits')
-rw-r--r-- | crates/hir_ty/src/traits/chalk.rs | 26 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 47 |
2 files changed, 43 insertions, 30 deletions
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 011bef6f6..541e6082f 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 | ||
4 | use log::debug; | 4 | use log::debug; |
5 | 5 | ||
6 | use chalk_ir::{fold::shift::Shift, CanonicalVarKinds, GenericArg}; | 6 | use chalk_ir::{fold::shift::Shift, CanonicalVarKinds}; |
7 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; | 7 | use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; |
8 | 8 | ||
9 | use base_db::{salsa::InternKey, CrateId}; | 9 | use 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 | }; |
27 | use mapping::{ | 27 | use 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 { |
@@ -300,7 +300,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
300 | _closure_id: chalk_ir::ClosureId<Interner>, | 300 | _closure_id: chalk_ir::ClosureId<Interner>, |
301 | _substs: &chalk_ir::Substitution<Interner>, | 301 | _substs: &chalk_ir::Substitution<Interner>, |
302 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { | 302 | ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { |
303 | let ty = Ty::unit().to_chalk(self.db); | 303 | let ty = TyBuilder::unit().to_chalk(self.db); |
304 | make_binders(ty, 0) | 304 | make_binders(ty, 0) |
305 | } | 305 | } |
306 | fn closure_fn_substitution( | 306 | fn closure_fn_substitution( |
@@ -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 { |
@@ -387,7 +387,7 @@ pub(crate) fn associated_ty_data_query( | |||
387 | // Lower bounds -- we could/should maybe move this to a separate query in `lower` | 387 | // 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); | 388 | let type_alias_data = db.type_alias_data(type_alias); |
389 | let generic_params = generics(db.upcast(), type_alias.into()); | 389 | let generic_params = generics(db.upcast(), type_alias.into()); |
390 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 390 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
391 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); | 391 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); |
392 | let ctx = crate::TyLoweringContext::new(db, &resolver) | 392 | let ctx = crate::TyLoweringContext::new(db, &resolver) |
393 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); | 393 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); |
@@ -421,7 +421,7 @@ pub(crate) fn trait_datum_query( | |||
421 | let trait_data = db.trait_data(trait_); | 421 | let trait_data = db.trait_data(trait_); |
422 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); | 422 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); |
423 | let generic_params = generics(db.upcast(), trait_.into()); | 423 | let generic_params = generics(db.upcast(), trait_.into()); |
424 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 424 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
425 | let flags = rust_ir::TraitFlags { | 425 | let flags = rust_ir::TraitFlags { |
426 | auto: trait_data.is_auto, | 426 | auto: trait_data.is_auto, |
427 | upstream: trait_.lookup(db.upcast()).container.krate() != krate, | 427 | upstream: trait_.lookup(db.upcast()).container.krate() != krate, |
@@ -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, |
@@ -490,7 +490,7 @@ pub(crate) fn struct_datum_query( | |||
490 | let upstream = adt_id.module(db.upcast()).krate() != krate; | 490 | let upstream = adt_id.module(db.upcast()).krate() != krate; |
491 | let where_clauses = { | 491 | let where_clauses = { |
492 | let generic_params = generics(db.upcast(), adt_id.into()); | 492 | let generic_params = generics(db.upcast(), adt_id.into()); |
493 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 493 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
494 | convert_where_clauses(db, adt_id.into(), &bound_vars) | 494 | convert_where_clauses(db, adt_id.into(), &bound_vars) |
495 | }; | 495 | }; |
496 | let flags = rust_ir::AdtFlags { | 496 | let flags = rust_ir::AdtFlags { |
@@ -539,7 +539,7 @@ fn impl_def_datum( | |||
539 | let impl_data = db.impl_data(impl_id); | 539 | let impl_data = db.impl_data(impl_id); |
540 | 540 | ||
541 | let generic_params = generics(db.upcast(), impl_id.into()); | 541 | let generic_params = generics(db.upcast(), impl_id.into()); |
542 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 542 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
543 | let trait_ = trait_ref.hir_trait_id(); | 543 | let trait_ = trait_ref.hir_trait_id(); |
544 | let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate { | 544 | let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate { |
545 | rust_ir::ImplType::Local | 545 | rust_ir::ImplType::Local |
@@ -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, |
@@ -629,7 +629,7 @@ pub(crate) fn fn_def_datum_query( | |||
629 | let callable_def: CallableDefId = from_chalk(db, fn_def_id); | 629 | let callable_def: CallableDefId = from_chalk(db, fn_def_id); |
630 | let generic_params = generics(db.upcast(), callable_def.into()); | 630 | let generic_params = generics(db.upcast(), callable_def.into()); |
631 | let sig = db.callable_item_signature(callable_def); | 631 | let sig = db.callable_item_signature(callable_def); |
632 | let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 632 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
633 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); | 633 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); |
634 | let bound = rust_ir::FnDefDatumBound { | 634 | 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 | 635 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway |
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index aef6b8a15..5e4f97a46 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; | |||
10 | use hir_def::{GenericDefId, TypeAliasId}; | 10 | use hir_def::{GenericDefId, TypeAliasId}; |
11 | 11 | ||
12 | use crate::{ | 12 | use 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 | ||
20 | use super::interner::*; | 18 | use super::interner::*; |
@@ -137,7 +135,7 @@ impl ToChalk for Ty { | |||
137 | db, | 135 | db, |
138 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), | 136 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), |
139 | ); | 137 | ); |
140 | TyKind::Function(FnPointer { num_args: (substs.len() - 1), sig, substs }) | 138 | TyKind::Function(FnPointer { num_args: (substs.len(&Interner) - 1), sig, substs }) |
141 | } | 139 | } |
142 | chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), | 140 | chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), |
143 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown, | 141 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown, |
@@ -216,25 +214,40 @@ fn array_to_chalk(db: &dyn HirDatabase, ty: Ty) -> chalk_ir::Ty<Interner> { | |||
216 | chalk_ir::TyKind::Array(arg, const_).intern(&Interner) | 214 | chalk_ir::TyKind::Array(arg, const_).intern(&Interner) |
217 | } | 215 | } |
218 | 216 | ||
217 | impl ToChalk for GenericArg { | ||
218 | type Chalk = chalk_ir::GenericArg<Interner>; | ||
219 | |||
220 | fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk { | ||
221 | match self.interned() { | ||
222 | crate::GenericArgData::Ty(ty) => ty.clone().to_chalk(db).cast(&Interner), | ||
223 | } | ||
224 | } | ||
225 | |||
226 | fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { | ||
227 | match chalk.interned() { | ||
228 | chalk_ir::GenericArgData::Ty(ty) => Ty::from_chalk(db, ty.clone()).cast(&Interner), | ||
229 | chalk_ir::GenericArgData::Lifetime(_) => unimplemented!(), | ||
230 | chalk_ir::GenericArgData::Const(_) => unimplemented!(), | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | |||
219 | impl ToChalk for Substitution { | 235 | impl ToChalk for Substitution { |
220 | type Chalk = chalk_ir::Substitution<Interner>; | 236 | type Chalk = chalk_ir::Substitution<Interner>; |
221 | 237 | ||
222 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { | 238 | 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))) | 239 | chalk_ir::Substitution::from_iter( |
240 | &Interner, | ||
241 | self.iter(&Interner).map(|ty| ty.clone().to_chalk(db)), | ||
242 | ) | ||
224 | } | 243 | } |
225 | 244 | ||
226 | fn from_chalk( | 245 | fn from_chalk( |
227 | db: &dyn HirDatabase, | 246 | db: &dyn HirDatabase, |
228 | parameters: chalk_ir::Substitution<Interner>, | 247 | parameters: chalk_ir::Substitution<Interner>, |
229 | ) -> Substitution { | 248 | ) -> Substitution { |
230 | let tys = parameters | 249 | let tys = parameters.iter(&Interner).map(|p| from_chalk(db, p.clone())).collect(); |
231 | .iter(&Interner) | 250 | 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 | } | 251 | } |
239 | } | 252 | } |
240 | 253 | ||
@@ -531,7 +544,7 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
531 | // have the expected self type | 544 | // have the expected self type |
532 | return None; | 545 | return None; |
533 | } | 546 | } |
534 | let args_no_self = trait_ref.substitution[1..] | 547 | let args_no_self = trait_ref.substitution.interned()[1..] |
535 | .iter() | 548 | .iter() |
536 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | 549 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) |
537 | .collect(); | 550 | .collect(); |
@@ -543,7 +556,7 @@ pub(super) fn generic_predicate_to_inline_bound( | |||
543 | return None; | 556 | return None; |
544 | } | 557 | } |
545 | let trait_ = projection_ty.trait_(db); | 558 | let trait_ = projection_ty.trait_(db); |
546 | let args_no_self = projection_ty.substitution[1..] | 559 | let args_no_self = projection_ty.substitution.interned()[1..] |
547 | .iter() | 560 | .iter() |
548 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | 561 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) |
549 | .collect(); | 562 | .collect(); |