Use ids for Callable

This commit is contained in:
Aleksey Kladov 2019-11-25 16:26:52 +03:00
parent 9047a4ad46
commit 78791d6fac
4 changed files with 50 additions and 34 deletions

View file

@ -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, "<")?;

View file

@ -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(_) => {}
}
}
}

View file

@ -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<Adt>) -> Ty {
@ -806,18 +808,18 @@ impl From<ModuleDef> for Option<TypableDef> {
#[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<crate::Crate> {
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<CallableDef> 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(),
}
}
}

View file

@ -26,14 +26,17 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
);
let (mut call_info, has_self) = match &calling_node {
FnCallNode::CallExpr(expr) => {
//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) => {