diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 1e05ac3f23..8c045aaef2 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 194e558191..1d6df2b7ad 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 1ceafd9b15..27cfe00c11 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, GenericDefId, LocalStructFieldId, 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), - CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s), - CallableDef::EnumVariant(e) => fn_sig_for_enum_variant_constructor(db, e), + 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), } } @@ -630,9 +632,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) @@ -643,7 +645,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> 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. @@ -703,15 +705,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) } @@ -723,20 +725,21 @@ 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: 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) } @@ -748,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 { @@ -805,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, } } } @@ -824,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(), } } } diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 7c367230e7..9beceb29cf 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -26,14 +26,17 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { - //FIXME: apply subst + //FIXME: don't poke into Ty let (callable_def, _subst) = analyzer.type_of(db, &expr.expr()?)?.as_callable()?; match callable_def { - hir::CallableDef::Function(it) => { - (CallInfo::with_fn(db, it), it.has_self_param(db)) + hir::CallableDef::FunctionId(it) => { + let fn_def = it.into(); + (CallInfo::with_fn(db, fn_def), fn_def.has_self_param(db)) + } + hir::CallableDef::StructId(it) => (CallInfo::with_struct(db, it.into())?, false), + hir::CallableDef::EnumVariantId(it) => { + (CallInfo::with_enum_variant(db, it.into())?, false) } - hir::CallableDef::Struct(it) => (CallInfo::with_struct(db, it)?, false), - hir::CallableDef::EnumVariant(it) => (CallInfo::with_enum_variant(db, it)?, false), } } FnCallNode::MethodCallExpr(expr) => {