From 2f02977e56a44a5f78576a25547672cd8f910f13 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 9 Apr 2021 14:28:04 +0200 Subject: More moving stuff around --- crates/hir_ty/src/chalk_db.rs | 15 +++++---- crates/hir_ty/src/lib.rs | 61 ++++--------------------------------- crates/hir_ty/src/mapping.rs | 71 ++++++++++++++++++++++++++++++++++--------- crates/hir_ty/src/tls.rs | 5 +-- 4 files changed, 74 insertions(+), 78 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/chalk_db.rs b/crates/hir_ty/src/chalk_db.rs index 9d07ad597..8f054d06b 100644 --- a/crates/hir_ty/src/chalk_db.rs +++ b/crates/hir_ty/src/chalk_db.rs @@ -17,7 +17,7 @@ use hir_expand::name::name; use crate::{ db::HirDatabase, display::HirDisplay, - from_assoc_type_id, make_only_type_binders, + from_assoc_type_id, from_chalk_trait_id, make_only_type_binders, mapping::{from_chalk, ToChalk, TypeAliasAsValue}, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, to_assoc_type_id, to_chalk_trait_id, @@ -79,7 +79,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { binders: &CanonicalVarKinds, ) -> Vec { debug!("impls_for_trait {:?}", trait_id); - let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); + let trait_: hir_def::TraitId = from_chalk_trait_id(trait_id); let ty: Ty = parameters[0].assert_ty_ref(&Interner).clone(); @@ -161,7 +161,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { Some(LangItemTarget::TraitId(trait_)) => trait_, _ => return None, }; - Some(trait_.to_chalk(self.db)) + Some(to_chalk_trait_id(trait_)) } fn program_clauses_for_env( @@ -308,7 +308,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { } fn trait_name(&self, trait_id: chalk_ir::TraitId) -> String { - let id = from_chalk(self.db, trait_id); + let id = from_chalk_trait_id(trait_id); self.db.trait_data(id).name.to_string() } fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String { @@ -413,7 +413,7 @@ pub(crate) fn trait_datum_query( trait_id: TraitId, ) -> Arc { debug!("trait_datum {:?}", trait_id); - let trait_: hir_def::TraitId = from_chalk(db, trait_id); + let trait_ = from_chalk_trait_id(trait_id); let trait_data = db.trait_data(trait_); debug!("trait {:?} = {:?}", trait_id, trait_data.name); let generic_params = generics(db.upcast(), trait_.into()); @@ -723,7 +723,10 @@ pub(super) fn generic_predicate_to_inline_bound( .collect(); let alias_eq_bound = rust_ir::AliasEqBound { value: ty.clone(), - trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, + trait_bound: rust_ir::TraitBound { + trait_id: to_chalk_trait_id(trait_), + args_no_self, + }, associated_ty_id: projection_ty.associated_ty_id, parameters: Vec::new(), // FIXME we don't support generic associated types yet }; diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 434e2e533..6b5006f5d 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -34,16 +34,12 @@ mod test_db; use std::sync::Arc; -use base_db::salsa; use chalk_ir::{ fold::{Fold, Shift}, interner::HasInterner, UintTy, }; -use hir_def::{ - expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId, - TypeParamId, -}; +use hir_def::{expr::ExprId, type_ref::Rawness, TypeParamId}; use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; @@ -56,6 +52,11 @@ pub use lower::{ associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, TyDefId, TyLoweringContext, ValueTyDefId, }; +pub use mapping::{ + const_from_placeholder_idx, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, + from_placeholder_idx, lt_from_placeholder_idx, to_assoc_type_id, to_chalk_trait_id, + to_foreign_def_id, to_placeholder_idx, +}; pub use traits::TraitEnvironment; pub use walk::TypeWalk; @@ -242,56 +243,6 @@ pub(crate) struct ReturnTypeImplTrait { pub(crate) bounds: Binders>, } -pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { - chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) -} - -pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId { - salsa::InternKey::from_intern_id(id.0) -} - -pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId { - chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id)) -} - -pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { - salsa::InternKey::from_intern_id(id.0) -} - -pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId { - assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); - let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); - db.lookup_intern_type_param_id(interned_id) -} - -pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex { - let interned_id = db.intern_type_param_id(id); - PlaceholderIndex { - ui: chalk_ir::UniverseIndex::ROOT, - idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(), - } -} - -pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId { - assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); - let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); - db.lookup_intern_lifetime_param_id(interned_id) -} - -pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId { - assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); - let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); - db.lookup_intern_const_param_id(interned_id) -} - -pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId { - chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id)) -} - -pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId { - salsa::InternKey::from_intern_id(id.0) -} - pub fn static_lifetime() -> Lifetime { LifetimeData::Static.intern(&Interner) } diff --git a/crates/hir_ty/src/mapping.rs b/crates/hir_ty/src/mapping.rs index 37c935194..5e86fafe5 100644 --- a/crates/hir_ty/src/mapping.rs +++ b/crates/hir_ty/src/mapping.rs @@ -5,10 +5,13 @@ use chalk_solve::rust_ir; -use base_db::salsa::InternKey; -use hir_def::TypeAliasId; +use base_db::salsa::{self, InternKey}; +use hir_def::{ConstParamId, LifetimeParamId, TraitId, TypeAliasId, TypeParamId}; -use crate::{chalk_db, db::HirDatabase, CallableDefId, FnDefId, Interner, OpaqueTyId}; +use crate::{ + chalk_db, db::HirDatabase, AssocTypeId, CallableDefId, ChalkTraitId, FnDefId, ForeignDefId, + Interner, OpaqueTyId, PlaceholderIndex, +}; pub(crate) trait ToChalk { type Chalk; @@ -23,18 +26,6 @@ where T::from_chalk(db, chalk) } -impl ToChalk for hir_def::TraitId { - type Chalk = chalk_db::TraitId; - - fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::TraitId { - chalk_ir::TraitId(self.as_intern_id()) - } - - fn from_chalk(_db: &dyn HirDatabase, trait_id: chalk_db::TraitId) -> hir_def::TraitId { - InternKey::from_intern_id(trait_id.0) - } -} - impl ToChalk for hir_def::ImplId { type Chalk = chalk_db::ImplId; @@ -111,3 +102,53 @@ impl From for chalk_ir::ClosureId { chalk_ir::ClosureId(id.as_intern_id()) } } + +pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { + chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) +} + +pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId { + salsa::InternKey::from_intern_id(id.0) +} + +pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId { + chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id)) +} + +pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { + salsa::InternKey::from_intern_id(id.0) +} + +pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeParamId { + assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); + let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); + db.lookup_intern_type_param_id(interned_id) +} + +pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeParamId) -> PlaceholderIndex { + let interned_id = db.intern_type_param_id(id); + PlaceholderIndex { + ui: chalk_ir::UniverseIndex::ROOT, + idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(), + } +} + +pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId { + assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); + let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); + db.lookup_intern_lifetime_param_id(interned_id) +} + +pub fn const_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> ConstParamId { + assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT); + let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx)); + db.lookup_intern_const_param_id(interned_id) +} + +pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId { + chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id)) +} + +pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId { + salsa::InternKey::from_intern_id(id.0) +} diff --git a/crates/hir_ty/src/tls.rs b/crates/hir_ty/src/tls.rs index 92989f11f..87c671a42 100644 --- a/crates/hir_ty/src/tls.rs +++ b/crates/hir_ty/src/tls.rs @@ -5,7 +5,8 @@ use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplicat use itertools::Itertools; use crate::{ - chalk_db, db::HirDatabase, from_assoc_type_id, mapping::from_chalk, CallableDefId, Interner, + chalk_db, db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, mapping::from_chalk, + CallableDefId, Interner, }; use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; @@ -32,7 +33,7 @@ impl DebugContext<'_> { id: chalk_db::TraitId, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let trait_: hir_def::TraitId = from_chalk(self.0, id); + let trait_: hir_def::TraitId = from_chalk_trait_id(id); let trait_data = self.0.trait_data(trait_); write!(fmt, "{}", trait_data.name) } -- cgit v1.2.3