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 From 19664e276aba21a42cad5351a2c91995d1ce5d52 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 13 Mar 2021 17:36:07 +0100 Subject: Use chalk_ir::AssocTypeId --- crates/hir_ty/src/autoderef.rs | 3 +- crates/hir_ty/src/display.rs | 28 +++++++++++------- crates/hir_ty/src/infer.rs | 7 +++-- crates/hir_ty/src/infer/expr.rs | 7 +++-- crates/hir_ty/src/lib.rs | 28 ++++++++++++------ crates/hir_ty/src/lower.rs | 10 +++++-- crates/hir_ty/src/traits/chalk.rs | 17 ++++++----- crates/hir_ty/src/traits/chalk/mapping.rs | 47 +++++++++---------------------- crates/hir_ty/src/traits/chalk/tls.rs | 9 +++--- 9 files changed, 82 insertions(+), 74 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 09009a3d8..d739d5d60 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs @@ -12,6 +12,7 @@ use log::{info, warn}; use crate::{ db::HirDatabase, + to_assoc_type_id, traits::{InEnvironment, Solution}, utils::generics, BoundVar, Canonical, DebruijnIndex, Interner, Obligation, Substs, TraitRef, Ty, TyKind, @@ -83,7 +84,7 @@ fn deref_by_trait( let projection = super::traits::ProjectionPredicate { ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())) .intern(&Interner), - projection_ty: super::ProjectionTy { associated_ty: target, parameters }, + projection_ty: super::ProjectionTy { associated_ty: to_assoc_type_id(target), parameters }, }; let obligation = super::Obligation::Projection(projection); diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 9b1e2ad05..2022069d8 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, from_foreign_def_id, primitive, utils::generics, AdtId, AliasTy, - CallableDefId, CallableSig, GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, - OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, + db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id, + utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, GenericPredicate, Interner, + Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, }; pub struct HirFormatter<'a> { @@ -256,7 +256,7 @@ impl HirDisplay for ProjectionTy { f.write_joined(&self.parameters[1..], ", ")?; write!(f, ">")?; } - write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?; + write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty)).name)?; Ok(()) } } @@ -467,13 +467,14 @@ impl HirDisplay for Ty { } } } - TyKind::AssociatedType(type_alias, parameters) => { + TyKind::AssociatedType(assoc_type_id, parameters) => { + let type_alias = from_assoc_type_id(*assoc_type_id); let trait_ = match type_alias.lookup(f.db.upcast()).container { AssocContainerId::TraitId(it) => it, _ => panic!("not an associated type"), }; let trait_ = f.db.trait_data(trait_); - let type_alias_data = f.db.type_alias_data(*type_alias); + let type_alias_data = f.db.type_alias_data(type_alias); // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) if f.display_target.is_test() { @@ -484,8 +485,10 @@ impl HirDisplay for Ty { write!(f, ">")?; } } else { - let projection_ty = - ProjectionTy { associated_ty: *type_alias, parameters: parameters.clone() }; + let projection_ty = ProjectionTy { + associated_ty: to_assoc_type_id(type_alias), + parameters: parameters.clone(), + }; projection_ty.hir_fmt(f)?; } @@ -697,7 +700,9 @@ fn write_bounds_like_dyn_trait( write!(f, "<")?; angle_open = true; } - let type_alias = f.db.type_alias_data(projection_pred.projection_ty.associated_ty); + let type_alias = f.db.type_alias_data(from_assoc_type_id( + projection_pred.projection_ty.associated_ty, + )); write!(f, "{} = ", type_alias.name)?; projection_pred.ty.hir_fmt(f)?; } @@ -768,7 +773,10 @@ impl HirDisplay for GenericPredicate { write!( f, ">::{} = ", - f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name, + f.db.type_alias_data(from_assoc_type_id( + projection_pred.projection_ty.associated_ty + )) + .name, )?; projection_pred.ty.hir_fmt(f)?; } diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index acde99b04..9d9bf549c 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -42,7 +42,7 @@ use super::{ }; use crate::{ db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, - AliasTy, Interner, TyKind, + to_assoc_type_id, AliasTy, Interner, TyKind, }; pub(crate) use unify::unify; @@ -382,7 +382,10 @@ impl<'a> InferenceContext<'a> { let trait_ref = TraitRef { trait_, substs: substs.clone() }; let projection = ProjectionPredicate { ty: ty.clone(), - projection_ty: ProjectionTy { associated_ty: res_assoc_ty, parameters: substs }, + projection_ty: ProjectionTy { + associated_ty: to_assoc_type_id(res_assoc_ty), + parameters: substs, + }, }; self.obligations.push(Obligation::Trait(trait_ref)); self.obligations.push(Obligation::Projection(projection)); diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 4e77f22fd..a72424dc7 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -18,6 +18,7 @@ use crate::{ lower::lower_to_chalk_mutability, method_resolution, op, primitive::{self, UintTy}, + to_assoc_type_id, traits::{FnTrait, InEnvironment}, utils::{generics, variant_data, Generics}, AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, OpaqueTyId, Rawness, @@ -97,8 +98,10 @@ impl<'a> InferenceContext<'a> { }); if self.db.trait_solve(krate, goal.value).is_some() { self.obligations.push(implements_fn_trait); - let output_proj_ty = - crate::ProjectionTy { associated_ty: output_assoc_type, parameters: substs }; + let output_proj_ty = crate::ProjectionTy { + associated_ty: to_assoc_type_id(output_assoc_type), + parameters: substs, + }; let return_ty = self.normalize_projection_ty(output_proj_ty); Some((arg_tys, return_ty)) } else { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 94cbb21ca..5752ddc4e 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -52,6 +52,7 @@ pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariabl pub use crate::traits::chalk::Interner; pub type ForeignDefId = chalk_ir::ForeignDefId; +pub type AssocTypeId = chalk_ir::AssocTypeId; #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Lifetime { @@ -70,7 +71,7 @@ pub struct OpaqueTy { /// trait and all its parameters are fully known. #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct ProjectionTy { - pub associated_ty: TypeAliasId, + pub associated_ty: AssocTypeId, pub parameters: Substs, } @@ -80,7 +81,7 @@ impl ProjectionTy { } fn trait_(&self, db: &dyn HirDatabase) -> TraitId { - match self.associated_ty.lookup(db.upcast()).container { + match from_assoc_type_id(self.associated_ty).lookup(db.upcast()).container { AssocContainerId::TraitId(it) => it, _ => panic!("projection ty without parent trait"), } @@ -141,7 +142,7 @@ pub enum TyKind { /// when we have tried to normalize a projection like `T::Item` but /// couldn't find a better representation. In that case, we generate /// an **application type** like `(Iterator::Item)`. - AssociatedType(TypeAliasId, Substs), + AssociatedType(AssocTypeId, Substs), /// a scalar type like `bool` or `u32` Scalar(Scalar), @@ -706,7 +707,7 @@ impl Ty { match *self.interned(&Interner) { TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), TyKind::FnDef(callable, ..) => Some(callable.into()), - TyKind::AssociatedType(type_alias, ..) => Some(type_alias.into()), + TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()), TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), _ => None, } @@ -920,14 +921,15 @@ impl Ty { pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option { match self.interned(&Interner) { - TyKind::AssociatedType(type_alias_id, ..) => { - match type_alias_id.lookup(db.upcast()).container { + TyKind::AssociatedType(id, ..) => { + match from_assoc_type_id(*id).lookup(db.upcast()).container { AssocContainerId::TraitId(trait_id) => Some(trait_id), _ => None, } } TyKind::Alias(AliasTy::Projection(projection_ty)) => { - match projection_ty.associated_ty.lookup(db.upcast()).container { + match from_assoc_type_id(projection_ty.associated_ty).lookup(db.upcast()).container + { AssocContainerId::TraitId(trait_id) => Some(trait_id), _ => None, } @@ -1121,10 +1123,18 @@ pub(crate) struct ReturnTypeImplTrait { pub(crate) bounds: Binders>, } -pub(crate) fn to_foreign_def_id(id: TypeAliasId) -> chalk_ir::ForeignDefId { +pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) } -pub(crate) fn from_foreign_def_id(id: chalk_ir::ForeignDefId) -> TypeAliasId { +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) } diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index a35d7266d..a5ab1ff70 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -27,6 +27,7 @@ use stdx::impl_from; use crate::{ db::HirDatabase, + to_assoc_type_id, traits::chalk::{Interner, ToChalk}, utils::{ all_super_trait_refs, associated_type_by_name_including_super_traits, generics, @@ -358,7 +359,7 @@ impl Ty { Some((super_trait_ref, associated_ty)) => { // FIXME handle type parameters on the segment TyKind::Alias(AliasTy::Projection(ProjectionTy { - associated_ty, + associated_ty: to_assoc_type_id(associated_ty), parameters: super_trait_ref.substs, })) .intern(&Interner) @@ -487,7 +488,7 @@ impl Ty { // FIXME handle type parameters on the segment return Some( TyKind::Alias(AliasTy::Projection(ProjectionTy { - associated_ty, + associated_ty: to_assoc_type_id(associated_ty), parameters: substs, })) .intern(&Interner), @@ -753,7 +754,10 @@ fn assoc_type_bindings_from_type_bound<'a>( None => return SmallVec::<[GenericPredicate; 1]>::new(), Some(t) => t, }; - let projection_ty = ProjectionTy { associated_ty, parameters: super_trait_ref.substs }; + let projection_ty = ProjectionTy { + associated_ty: to_assoc_type_id(associated_ty), + parameters: super_trait_ref.substs, + }; let mut preds = SmallVec::with_capacity( binding.type_ref.as_ref().map_or(0, |_| 1) + binding.bounds.len(), ); diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 55181cc49..2d3cd6719 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -17,14 +17,15 @@ use super::ChalkContext; use crate::{ db::HirDatabase, display::HirDisplay, + from_assoc_type_id, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, + to_assoc_type_id, utils::generics, BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TyKind, }; use mapping::{ - convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, - TypeAliasAsValue, + convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, }; pub use self::interner::Interner; @@ -234,7 +235,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { ty: TyKind::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }) .intern(&Interner), projection_ty: ProjectionTy { - associated_ty: future_output, + associated_ty: to_assoc_type_id(future_output), // Self type as the first parameter. parameters: Substs::single( TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) @@ -383,7 +384,7 @@ pub(crate) fn associated_ty_data_query( id: AssocTypeId, ) -> Arc { debug!("associated_ty_data {:?}", id); - let type_alias: TypeAliasId = from_chalk::(db, id).0; + let type_alias: TypeAliasId = from_assoc_type_id(id); let trait_ = match type_alias.lookup(db.upcast()).container { AssocContainerId::TraitId(t) => t, _ => panic!("associated type not in trait"), @@ -438,10 +439,8 @@ pub(crate) fn trait_datum_query( fundamental: false, }; let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); - let associated_ty_ids = trait_data - .associated_types() - .map(|type_alias| TypeAliasAsAssocType(type_alias).to_chalk(db)) - .collect(); + let associated_ty_ids = + trait_data.associated_types().map(|type_alias| to_assoc_type_id(type_alias)).collect(); let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; let well_known = lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); @@ -623,7 +622,7 @@ fn type_alias_associated_ty_value( let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; let value = rust_ir::AssociatedTyValue { impl_id: impl_id.to_chalk(db), - associated_ty_id: TypeAliasAsAssocType(assoc_ty).to_chalk(db), + associated_ty_id: to_assoc_type_id(assoc_ty), value: make_binders(value_bound, ty.num_binders), }; Arc::new(value) diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index cf490f9c5..67ac95a0e 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -14,6 +14,7 @@ use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId}; use crate::{ db::HirDatabase, + from_assoc_type_id, primitive::UintTy, traits::{Canonical, Obligation}, AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, @@ -38,9 +39,7 @@ impl ToChalk for Ty { }) .intern(&Interner) } - TyKind::AssociatedType(type_alias, substs) => { - let assoc_type = TypeAliasAsAssocType(type_alias); - let assoc_type_id = assoc_type.to_chalk(db); + TyKind::AssociatedType(assoc_type_id, substs) => { let substitution = substs.to_chalk(db); chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) } @@ -85,7 +84,7 @@ impl ToChalk for Ty { chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) } TyKind::Alias(AliasTy::Projection(proj_ty)) => { - let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); + let associated_ty_id = proj_ty.associated_ty; let substitution = proj_ty.parameters.to_chalk(db); chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { associated_ty_id, @@ -139,8 +138,7 @@ impl ToChalk for Ty { TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id)) } chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { - let associated_ty = - from_chalk::(db, proj.associated_ty_id).0; + let associated_ty = proj.associated_ty_id; let parameters = from_chalk(db, proj.substitution); TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) } @@ -180,10 +178,9 @@ impl ToChalk for Ty { } chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), - chalk_ir::TyKind::AssociatedType(type_id, subst) => TyKind::AssociatedType( - from_chalk::(db, type_id).0, - from_chalk(db, subst), - ), + chalk_ir::TyKind::AssociatedType(type_id, subst) => { + TyKind::AssociatedType(type_id, from_chalk(db, subst)) + } chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) @@ -332,20 +329,6 @@ impl ToChalk for CallableDefId { } } -pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId); - -impl ToChalk for TypeAliasAsAssocType { - type Chalk = AssocTypeId; - - fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { - chalk_ir::AssocTypeId(self.0.as_intern_id()) - } - - fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType { - TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0)) - } -} - pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); impl ToChalk for TypeAliasAsValue { @@ -427,7 +410,7 @@ impl ToChalk for ProjectionTy { fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy { chalk_ir::ProjectionTy { - associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), + associated_ty_id: self.associated_ty, substitution: self.parameters.to_chalk(db), } } @@ -437,11 +420,7 @@ impl ToChalk for ProjectionTy { projection_ty: chalk_ir::ProjectionTy, ) -> ProjectionTy { ProjectionTy { - associated_ty: from_chalk::( - db, - projection_ty.associated_ty_id, - ) - .0, + associated_ty: projection_ty.associated_ty_id, parameters: from_chalk(db, projection_ty.substitution), } } @@ -595,7 +574,10 @@ pub(super) fn generic_predicate_to_inline_bound( if &proj.projection_ty.parameters[0] != self_ty { return None; } - let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { + let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty) + .lookup(db.upcast()) + .container + { AssocContainerId::TraitId(t) => t, _ => panic!("associated type not in trait"), }; @@ -606,8 +588,7 @@ pub(super) fn generic_predicate_to_inline_bound( let alias_eq_bound = rust_ir::AliasEqBound { value: proj.ty.clone().to_chalk(db), trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, - associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) - .to_chalk(db), + associated_ty_id: proj.projection_ty.associated_ty, parameters: Vec::new(), // FIXME we don't support generic associated types yet }; Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs index 75b16172e..8892a63a9 100644 --- a/crates/hir_ty/src/traits/chalk/tls.rs +++ b/crates/hir_ty/src/traits/chalk/tls.rs @@ -4,8 +4,8 @@ use std::fmt; use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; use itertools::Itertools; -use super::{from_chalk, Interner, TypeAliasAsAssocType}; -use crate::{db::HirDatabase, CallableDefId}; +use super::{from_chalk, Interner}; +use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId}; use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; pub(crate) use unsafe_tls::{set_current_program, with_current_program}; @@ -41,7 +41,7 @@ impl DebugContext<'_> { id: super::AssocTypeId, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let type_alias: TypeAliasId = from_chalk::(self.0, id).0; + let type_alias: TypeAliasId = from_assoc_type_id(id); let type_alias_data = self.0.type_alias_data(type_alias); let trait_ = match type_alias.lookup(self.0.upcast()).container { AssocContainerId::TraitId(t) => t, @@ -75,8 +75,7 @@ impl DebugContext<'_> { projection_ty: &chalk_ir::ProjectionTy, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let type_alias: TypeAliasId = - from_chalk::(self.0, projection_ty.associated_ty_id).0; + let type_alias = from_assoc_type_id(projection_ty.associated_ty_id); let type_alias_data = self.0.type_alias_data(type_alias); let trait_ = match type_alias.lookup(self.0.upcast()).container { AssocContainerId::TraitId(t) => t, -- cgit v1.2.3 From 9719ce9fc731a400c9744ba1a6569e978c1a97e7 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 13 Mar 2021 17:55:50 +0100 Subject: Use chalk_ir::FnDefId --- crates/hir_ty/src/db.rs | 6 +++--- crates/hir_ty/src/diagnostics/unsafe_check.rs | 2 +- crates/hir_ty/src/display.rs | 9 +++++---- crates/hir_ty/src/infer/expr.rs | 7 ++++--- crates/hir_ty/src/lib.rs | 24 ++++++++++++++++++------ crates/hir_ty/src/lower.rs | 15 ++++++++++++--- crates/hir_ty/src/traits/chalk.rs | 4 ++-- crates/hir_ty/src/traits/chalk/interner.rs | 1 - crates/hir_ty/src/traits/chalk/mapping.rs | 5 ++--- 9 files changed, 47 insertions(+), 26 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index 06714409f..a038674cf 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs @@ -12,7 +12,7 @@ use la_arena::ArenaMap; use crate::{ method_resolution::{InherentImpls, TraitImpls}, traits::chalk, - Binders, CallableDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig, + Binders, CallableDefId, FnDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig, ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId, }; use hir_expand::name::Name; @@ -100,10 +100,10 @@ pub trait HirDatabase: DefDatabase + Upcast { fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc; #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)] - fn fn_def_datum(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> Arc; + fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc; #[salsa::invoke(crate::traits::chalk::fn_def_variance_query)] - fn fn_def_variance(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> chalk::Variances; + fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk::Variances; #[salsa::invoke(crate::traits::chalk::adt_variance_query)] fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances; diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs index e095bee28..20bb64827 100644 --- a/crates/hir_ty/src/diagnostics/unsafe_check.rs +++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs @@ -85,7 +85,7 @@ fn walk_unsafe( let expr = &body.exprs[current]; match expr { &Expr::Call { callee, .. } => { - if let Some(func) = infer[callee].as_fn_def() { + if let Some(func) = infer[callee].as_fn_def(db) { if db.function_data(func).is_unsafe { unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 2022069d8..b7e85e024 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -12,8 +12,9 @@ use hir_expand::name::Name; use crate::{ db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id, - utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, GenericPredicate, Interner, - Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, + traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, + GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, + Substs, TraitRef, Ty, TyKind, }; pub struct HirFormatter<'a> { @@ -363,7 +364,7 @@ impl HirDisplay for Ty { sig.hir_fmt(f)?; } TyKind::FnDef(def, parameters) => { - let def = *def; + let def = from_chalk(f.db, *def); let sig = f.db.callable_item_signature(def).subst(parameters); match def { CallableDefId::FunctionId(ff) => { @@ -431,7 +432,7 @@ impl HirDisplay for Ty { || f.omit_verbose_types() { match self - .as_generic_def() + .as_generic_def(f.db) .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) .filter(|defaults| !defaults.is_empty()) { diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index a72424dc7..153f22f25 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -19,7 +19,7 @@ use crate::{ method_resolution, op, primitive::{self, UintTy}, to_assoc_type_id, - traits::{FnTrait, InEnvironment}, + traits::{chalk::from_chalk, FnTrait, InEnvironment}, utils::{generics, variant_data, Generics}, AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, OpaqueTyId, Rawness, Scalar, Substs, TraitRef, Ty, TyKind, @@ -932,8 +932,9 @@ impl<'a> InferenceContext<'a> { } fn register_obligations_for_call(&mut self, callable_ty: &Ty) { - if let TyKind::FnDef(def, parameters) = callable_ty.interned(&Interner) { - let generic_predicates = self.db.generic_predicates((*def).into()); + if let TyKind::FnDef(fn_def, parameters) = callable_ty.interned(&Interner) { + let def: CallableDefId = from_chalk(self.db, *fn_def); + let generic_predicates = self.db.generic_predicates(def.into()); for predicate in generic_predicates.iter() { let predicate = predicate.clone().subst(parameters); if let Some(obligation) = Obligation::from_predicate(predicate) { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 5752ddc4e..6b3485264 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -53,6 +53,7 @@ pub use crate::traits::chalk::Interner; pub type ForeignDefId = chalk_ir::ForeignDefId; pub type AssocTypeId = chalk_ir::AssocTypeId; +pub(crate) type FnDefId = chalk_ir::FnDefId; #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Lifetime { @@ -182,7 +183,7 @@ pub enum TyKind { /// fn foo() -> i32 { 1 } /// let bar = foo; // bar: fn() -> i32 {foo} /// ``` - FnDef(CallableDefId, Substs), + FnDef(FnDefId, Substs), /// The pointee of a string slice. Written as `str`. Str, @@ -703,10 +704,12 @@ impl Ty { } } - pub fn as_generic_def(&self) -> Option { + pub fn as_generic_def(&self, db: &dyn HirDatabase) -> Option { match *self.interned(&Interner) { TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), - TyKind::FnDef(callable, ..) => Some(callable.into()), + TyKind::FnDef(callable, ..) => { + Some(db.lookup_intern_callable_def(callable.into()).into()) + } TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()), TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), _ => None, @@ -775,18 +778,27 @@ impl Ty { } } - pub fn as_fn_def(&self) -> Option { + pub fn callable_def(&self, db: &dyn HirDatabase) -> Option { match self.interned(&Interner) { - &TyKind::FnDef(CallableDefId::FunctionId(func), ..) => Some(func), + &TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())), _ => None, } } + pub fn as_fn_def(&self, db: &dyn HirDatabase) -> Option { + if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) { + Some(func) + } else { + None + } + } + pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option { match self.interned(&Interner) { TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), TyKind::FnDef(def, parameters) => { - let sig = db.callable_item_signature(*def); + let callable_def = db.lookup_intern_callable_def((*def).into()); + let sig = db.callable_item_signature(callable_def); Some(sig.subst(¶meters)) } TyKind::Closure(.., substs) => { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index a5ab1ff70..b8b1400eb 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -1064,7 +1064,10 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders { let generics = generics(db.upcast(), def.into()); let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); - Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner)) + Binders::new( + substs.len(), + TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), + ) } /// Build the declared type of a const. @@ -1107,7 +1110,10 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders PolyFnSig { @@ -1132,7 +1138,10 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) - } let generics = generics(db.upcast(), def.parent.into()); let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); - Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner)) + Binders::new( + substs.len(), + TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner), + ) } fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders { diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 2d3cd6719..bb92d8e2a 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -21,8 +21,8 @@ use crate::{ method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, to_assoc_type_id, utils::generics, - BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate, - ProjectionTy, Substs, TraitRef, Ty, TyKind, + BoundVar, CallableDefId, CallableSig, DebruijnIndex, FnDefId, GenericPredicate, + ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TyKind, }; use mapping::{ convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index e891efb7b..1dc3f497d 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -20,7 +20,6 @@ pub(crate) type ImplId = chalk_ir::ImplId; pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum; pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId; pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue; -pub(crate) type FnDefId = chalk_ir::FnDefId; pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum; pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId; pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum; diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 67ac95a0e..23ef07d77 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -66,8 +66,7 @@ impl ToChalk for Ty { chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner) } TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner), - TyKind::FnDef(callable_def, substs) => { - let id = callable_def.to_chalk(db); + TyKind::FnDef(id, substs) => { let substitution = substs.to_chalk(db); chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) } @@ -201,7 +200,7 @@ impl ToChalk for Ty { chalk_ir::TyKind::Never => TyKind::Never, chalk_ir::TyKind::FnDef(fn_def_id, subst) => { - TyKind::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst)) + TyKind::FnDef(fn_def_id, from_chalk(db, subst)) } chalk_ir::TyKind::Closure(id, subst) => { -- cgit v1.2.3