From 9047a4ad4620d3b37d17a11e6dee0ea4ffbd7af1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 25 Nov 2019 16:16:41 +0300 Subject: Use more IDs --- crates/ra_hir/src/ty/lower.rs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 1ceafd9b1..348c5f67d 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -14,7 +14,7 @@ use hir_def::{ path::{GenericArg, PathSegment}, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, - AdtId, EnumVariantId, GenericDefId, LocalStructFieldId, VariantId, + AdtId, EnumVariantId, FunctionId, GenericDefId, LocalStructFieldId, StructId, VariantId, }; use ra_arena::map::ArenaMap; @@ -546,9 +546,9 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace /// Build the signature of a callable item (function, struct or enum variant). pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSig { match def { - CallableDef::Function(f) => fn_sig_for_fn(db, f), - CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s), - CallableDef::EnumVariant(e) => fn_sig_for_enum_variant_constructor(db, e), + CallableDef::Function(f) => fn_sig_for_fn(db, f.id), + CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s.id), + CallableDef::EnumVariant(e) => fn_sig_for_enum_variant_constructor(db, e.into()), } } @@ -630,9 +630,9 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) - Substs(defaults) } -fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { - let data = db.function_data(def.id); - let resolver = def.id.resolver(db); +fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> FnSig { + let data = db.function_data(def); + let resolver = def.resolver(db); let params = data.params.iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::>(); let ret = Ty::from_hir(db, &resolver, &data.ret_type); FnSig::from_params_and_return(params, ret) @@ -703,15 +703,15 @@ impl From> for Uncertain { } } -fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { - let struct_data = db.struct_data(def.id.into()); +fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> FnSig { + let struct_data = db.struct_data(def.into()); let fields = struct_data.variant_data.fields(); - let resolver = def.id.resolver(db); + let resolver = def.resolver(db); let params = fields .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) .collect::>(); - let ret = type_for_adt(db, def); + let ret = type_for_adt(db, Struct::from(def)); FnSig::from_params_and_return(params, ret) } @@ -726,17 +726,18 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { Ty::apply(TypeCtor::FnDef(def.into()), substs) } -fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { - let var_data = def.variant_data(db); - let fields = var_data.fields(); - let resolver = def.parent.id.resolver(db); +fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) -> FnSig { + let enum_data = db.enum_data(def.parent); + let var_data = &enum_data.variants[def.local_id]; + let fields = var_data.variant_data.fields(); + let resolver = def.parent.resolver(db); let params = fields .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) .collect::>(); - let generics = db.generic_params(def.parent_enum(db).id.into()); + let generics = db.generic_params(def.parent.into()); let substs = Substs::identity(&generics); - let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs); + let ret = type_for_adt(db, Enum::from(def.parent)).subst(&substs); FnSig::from_params_and_return(params, ret) } -- cgit v1.2.3 From 78791d6fac1ff426264e636545a07664d83d2039 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 25 Nov 2019 16:26:52 +0300 Subject: Use ids for Callable --- crates/ra_hir/src/ty.rs | 19 +++++++++++++------ crates/ra_hir/src/ty/infer/expr.rs | 14 +++++++++----- crates/ra_hir/src/ty/lower.rs | 38 ++++++++++++++++++++------------------ 3 files changed, 42 insertions(+), 29 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 1e05ac3f2..8c045aaef 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -171,7 +171,7 @@ impl TypeCtor { | TypeCtor::Tuple { .. } => None, TypeCtor::Closure { def, .. } => def.krate(db), TypeCtor::Adt(adt) => adt.krate(db), - TypeCtor::FnDef(callable) => callable.krate(db), + TypeCtor::FnDef(callable) => Some(callable.krate(db).into()), TypeCtor::AssociatedType(type_alias) => type_alias.krate(db), } } @@ -856,13 +856,20 @@ impl HirDisplay for ApplicationTy { TypeCtor::FnDef(def) => { let sig = f.db.callable_item_signature(def); let name = match def { - CallableDef::Function(ff) => ff.name(f.db), - CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing), - CallableDef::EnumVariant(e) => e.name(f.db).unwrap_or_else(Name::missing), + CallableDef::FunctionId(ff) => f.db.function_data(ff).name.clone(), + CallableDef::StructId(s) => { + f.db.struct_data(s.0).name.clone().unwrap_or_else(Name::missing) + } + CallableDef::EnumVariantId(e) => { + let enum_data = f.db.enum_data(e.parent); + enum_data.variants[e.local_id].name.clone().unwrap_or_else(Name::missing) + } }; match def { - CallableDef::Function(_) => write!(f, "fn {}", name)?, - CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?, + CallableDef::FunctionId(_) => write!(f, "fn {}", name)?, + CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { + write!(f, "{}", name)? + } } if self.parameters.len() > 0 { write!(f, "<")?; diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index 194e55819..1d6df2b7a 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -8,6 +8,7 @@ use hir_def::{ generics::GenericParams, path::{GenericArg, GenericArgs}, resolver::resolver_for_expr, + ContainerId, Lookup, }; use hir_expand::name; @@ -660,18 +661,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } // add obligation for trait implementation, if this is a trait method match def { - CallableDef::Function(f) => { - if let Some(trait_) = f.parent_trait(self.db) { + CallableDef::FunctionId(f) => { + if let ContainerId::TraitId(trait_) = f.lookup(self.db).container { // construct a TraitDef let substs = a_ty.parameters.prefix( self.db - .generic_params(trait_.id.into()) + .generic_params(trait_.into()) .count_params_including_parent(), ); - self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); + self.obligations.push(Obligation::Trait(TraitRef { + trait_: trait_.into(), + substs, + })); } } - CallableDef::Struct(_) | CallableDef::EnumVariant(_) => {} + CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => {} } } } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 348c5f67d..27cfe00c1 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -14,9 +14,11 @@ use hir_def::{ path::{GenericArg, PathSegment}, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, - AdtId, EnumVariantId, FunctionId, GenericDefId, LocalStructFieldId, StructId, VariantId, + AdtId, AstItemDef, EnumVariantId, FunctionId, GenericDefId, HasModule, LocalStructFieldId, + Lookup, StructId, VariantId, }; use ra_arena::map::ArenaMap; +use ra_db::CrateId; use super::{ FnSig, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, @@ -546,9 +548,9 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace /// Build the signature of a callable item (function, struct or enum variant). pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSig { match def { - CallableDef::Function(f) => fn_sig_for_fn(db, f.id), - CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s.id), - CallableDef::EnumVariant(e) => fn_sig_for_enum_variant_constructor(db, e.into()), + CallableDef::FunctionId(f) => fn_sig_for_fn(db, f), + CallableDef::StructId(s) => fn_sig_for_struct_constructor(db, s), + CallableDef::EnumVariantId(e) => fn_sig_for_enum_variant_constructor(db, e), } } @@ -643,7 +645,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> FnSig { fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { let generics = db.generic_params(def.id.into()); let substs = Substs::identity(&generics); - Ty::apply(TypeCtor::FnDef(def.into()), substs) + Ty::apply(TypeCtor::FnDef(def.id.into()), substs) } /// Build the declared type of a const. @@ -723,7 +725,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { } let generics = db.generic_params(def.id.into()); let substs = Substs::identity(&generics); - Ty::apply(TypeCtor::FnDef(def.into()), substs) + Ty::apply(TypeCtor::FnDef(def.id.into()), substs) } fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) -> FnSig { @@ -749,7 +751,7 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> } let generics = db.generic_params(def.parent_enum(db).id.into()); let substs = Substs::identity(&generics); - Ty::apply(TypeCtor::FnDef(def.into()), substs) + Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs) } fn type_for_adt(db: &impl HirDatabase, adt: impl Into) -> Ty { @@ -806,18 +808,18 @@ impl From for Option { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum CallableDef { - Function(Function), - Struct(Struct), - EnumVariant(EnumVariant), + FunctionId(FunctionId), + StructId(StructId), + EnumVariantId(EnumVariantId), } -impl_froms!(CallableDef: Function, Struct, EnumVariant); +impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId); impl CallableDef { - pub fn krate(self, db: &impl HirDatabase) -> Option { + pub fn krate(self, db: &impl HirDatabase) -> CrateId { match self { - CallableDef::Function(f) => f.krate(db), - CallableDef::Struct(s) => s.krate(db), - CallableDef::EnumVariant(e) => e.parent_enum(db).krate(db), + CallableDef::FunctionId(f) => f.lookup(db).module(db).krate, + CallableDef::StructId(s) => s.0.module(db).krate, + CallableDef::EnumVariantId(e) => e.parent.module(db).krate, } } } @@ -825,9 +827,9 @@ impl CallableDef { impl From for GenericDefId { fn from(def: CallableDef) -> GenericDefId { match def { - CallableDef::Function(f) => f.id.into(), - CallableDef::Struct(s) => s.id.into(), - CallableDef::EnumVariant(e) => EnumVariantId::from(e).into(), + CallableDef::FunctionId(f) => f.into(), + CallableDef::StructId(s) => s.into(), + CallableDef::EnumVariantId(e) => e.into(), } } } -- cgit v1.2.3