From dfafcd926a4cc9b09aba0eb3cc5275a4abe633b9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 13 Mar 2021 17:23:19 +0100 Subject: Use chalk_ir::ForeignDefId --- crates/hir_ty/src/display.rs | 8 ++++---- crates/hir_ty/src/lib.rs | 20 ++++++++++++++++---- crates/hir_ty/src/lower.rs | 2 +- crates/hir_ty/src/method_resolution.rs | 15 +++++++++------ crates/hir_ty/src/traits/chalk/interner.rs | 1 - crates/hir_ty/src/traits/chalk/mapping.rs | 24 ++---------------------- 6 files changed, 32 insertions(+), 38 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index ee15f4f52..9b1e2ad05 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -11,9 +11,9 @@ use hir_def::{ use hir_expand::name::Name; use crate::{ - db::HirDatabase, primitive, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, - GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, - Substs, TraitRef, Ty, TyKind, + db::HirDatabase, from_foreign_def_id, primitive, utils::generics, AdtId, AliasTy, + CallableDefId, CallableSig, GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, + OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, }; pub struct HirFormatter<'a> { @@ -491,7 +491,7 @@ impl HirDisplay for Ty { } } TyKind::ForeignType(type_alias) => { - let type_alias = f.db.type_alias_data(*type_alias); + let type_alias = f.db.type_alias_data(from_foreign_def_id(*type_alias)); write!(f, "{}", type_alias.name)?; } TyKind::OpaqueType(opaque_ty_id, parameters) => { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 2309db492..94cbb21ca 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -51,6 +51,8 @@ pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariabl pub use crate::traits::chalk::Interner; +pub type ForeignDefId = chalk_ir::ForeignDefId; + #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Lifetime { Parameter(LifetimeParamId), @@ -194,7 +196,7 @@ pub enum TyKind { Closure(DefWithBodyId, ExprId, Substs), /// Represents a foreign type declared in external blocks. - ForeignType(TypeAliasId), + ForeignType(ForeignDefId), /// A pointer to a function. Written as `fn() -> i32`. /// @@ -705,7 +707,7 @@ impl Ty { TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), TyKind::FnDef(callable, ..) => Some(callable.into()), TyKind::AssociatedType(type_alias, ..) => Some(type_alias.into()), - TyKind::ForeignType(type_alias, ..) => Some(type_alias.into()), + TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), _ => None, } } @@ -724,8 +726,10 @@ impl Ty { (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_), TyKind::Array(_)) => true, (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2, (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, - (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) - | (TyKind::ForeignType(ty_id, ..), TyKind::ForeignType(ty_id2, ..)) => ty_id == ty_id2, + (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => { + ty_id == ty_id2 + } + (TyKind::ForeignType(ty_id, ..), TyKind::ForeignType(ty_id2, ..)) => ty_id == ty_id2, (TyKind::Closure(def, expr, _), TyKind::Closure(def2, expr2, _)) => { expr == expr2 && def == def2 } @@ -1116,3 +1120,11 @@ pub struct ReturnTypeImplTraits { pub(crate) struct ReturnTypeImplTrait { pub(crate) bounds: Binders>, } + +pub(crate) fn to_foreign_def_id(id: TypeAliasId) -> chalk_ir::ForeignDefId { + chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) +} + +pub(crate) fn from_foreign_def_id(id: chalk_ir::ForeignDefId) -> TypeAliasId { + salsa::InternKey::from_intern_id(id.0) +} diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 9fe7e3dce..a35d7266d 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -1143,7 +1143,7 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders { let ctx = TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); if db.type_alias_data(t).is_extern { - Binders::new(0, TyKind::ForeignType(t).intern(&Interner)) + Binders::new(0, TyKind::ForeignType(crate::to_foreign_def_id(t)).intern(&Interner)) } else { let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); let type_ref = &db.type_alias_data(t).type_ref; diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index f9877e760..c7055bee5 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -9,7 +9,7 @@ use base_db::CrateId; use chalk_ir::Mutability; use hir_def::{ lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, - ImplId, Lookup, ModuleId, TraitId, TypeAliasId, + ImplId, Lookup, ModuleId, TraitId, }; use hir_expand::name::Name; use rustc_hash::{FxHashMap, FxHashSet}; @@ -17,10 +17,11 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ autoderef, db::HirDatabase, + from_foreign_def_id, primitive::{self, FloatTy, IntTy, UintTy}, utils::all_super_traits, - AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Interner, Scalar, Substs, - TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk, + AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, ForeignDefId, InEnvironment, Interner, + Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk, }; /// This is used as a key for indexing impls. @@ -35,7 +36,7 @@ pub enum TyFingerprint { Adt(hir_def::AdtId), Dyn(TraitId), Tuple(usize), - ForeignType(TypeAliasId), + ForeignType(ForeignDefId), FnPtr(usize, FnSig), } @@ -236,8 +237,10 @@ impl Ty { TyKind::Adt(AdtId(def_id), _) => { return mod_to_crate_ids(def_id.module(db.upcast())); } - TyKind::ForeignType(type_alias_id) => { - return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast())); + TyKind::ForeignType(id) => { + return mod_to_crate_ids( + from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()), + ); } TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"), TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"), diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index 54bd1c724..e891efb7b 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -12,7 +12,6 @@ pub struct Interner; pub(crate) type AssocTypeId = chalk_ir::AssocTypeId; pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum; -pub(crate) type ForeignDefId = chalk_ir::ForeignDefId; pub(crate) type TraitId = chalk_ir::TraitId; pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum; pub(crate) type AdtId = chalk_ir::AdtId; diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 44cfb9359..cf490f9c5 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -51,11 +51,7 @@ impl ToChalk for Ty { chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) } - TyKind::ForeignType(type_alias) => { - let foreign_type = TypeAliasAsForeignType(type_alias); - let foreign_type_id = foreign_type.to_chalk(db); - chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) - } + TyKind::ForeignType(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner), TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), @@ -217,9 +213,7 @@ impl ToChalk for Ty { TyKind::Closure(def, expr, from_chalk(db, subst)) } - chalk_ir::TyKind::Foreign(foreign_def_id) => { - TyKind::ForeignType(from_chalk::(db, foreign_def_id).0) - } + chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::ForeignType(foreign_def_id), chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME } @@ -352,20 +346,6 @@ impl ToChalk for TypeAliasAsAssocType { } } -pub(crate) struct TypeAliasAsForeignType(pub(crate) TypeAliasId); - -impl ToChalk for TypeAliasAsForeignType { - type Chalk = ForeignDefId; - - fn to_chalk(self, _db: &dyn HirDatabase) -> ForeignDefId { - chalk_ir::ForeignDefId(self.0.as_intern_id()) - } - - fn from_chalk(_db: &dyn HirDatabase, foreign_def_id: ForeignDefId) -> TypeAliasAsForeignType { - TypeAliasAsForeignType(InternKey::from_intern_id(foreign_def_id.0)) - } -} - pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); impl ToChalk for TypeAliasAsValue { -- cgit v1.2.3