aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-25 13:26:52 +0000
committerAleksey Kladov <[email protected]>2019-11-25 13:26:52 +0000
commit78791d6fac1ff426264e636545a07664d83d2039 (patch)
treebd0825246d6f26f7831f50dad8e39a131848a513 /crates
parent9047a4ad4620d3b37d17a11e6dee0ea4ffbd7af1 (diff)
Use ids for Callable
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/ty.rs19
-rw-r--r--crates/ra_hir/src/ty/infer/expr.rs14
-rw-r--r--crates/ra_hir/src/ty/lower.rs38
-rw-r--r--crates/ra_ide_api/src/call_info.rs13
4 files changed, 50 insertions, 34 deletions
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 {
171 | TypeCtor::Tuple { .. } => None, 171 | TypeCtor::Tuple { .. } => None,
172 TypeCtor::Closure { def, .. } => def.krate(db), 172 TypeCtor::Closure { def, .. } => def.krate(db),
173 TypeCtor::Adt(adt) => adt.krate(db), 173 TypeCtor::Adt(adt) => adt.krate(db),
174 TypeCtor::FnDef(callable) => callable.krate(db), 174 TypeCtor::FnDef(callable) => Some(callable.krate(db).into()),
175 TypeCtor::AssociatedType(type_alias) => type_alias.krate(db), 175 TypeCtor::AssociatedType(type_alias) => type_alias.krate(db),
176 } 176 }
177 } 177 }
@@ -856,13 +856,20 @@ impl HirDisplay for ApplicationTy {
856 TypeCtor::FnDef(def) => { 856 TypeCtor::FnDef(def) => {
857 let sig = f.db.callable_item_signature(def); 857 let sig = f.db.callable_item_signature(def);
858 let name = match def { 858 let name = match def {
859 CallableDef::Function(ff) => ff.name(f.db), 859 CallableDef::FunctionId(ff) => f.db.function_data(ff).name.clone(),
860 CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing), 860 CallableDef::StructId(s) => {
861 CallableDef::EnumVariant(e) => e.name(f.db).unwrap_or_else(Name::missing), 861 f.db.struct_data(s.0).name.clone().unwrap_or_else(Name::missing)
862 }
863 CallableDef::EnumVariantId(e) => {
864 let enum_data = f.db.enum_data(e.parent);
865 enum_data.variants[e.local_id].name.clone().unwrap_or_else(Name::missing)
866 }
862 }; 867 };
863 match def { 868 match def {
864 CallableDef::Function(_) => write!(f, "fn {}", name)?, 869 CallableDef::FunctionId(_) => write!(f, "fn {}", name)?,
865 CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?, 870 CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => {
871 write!(f, "{}", name)?
872 }
866 } 873 }
867 if self.parameters.len() > 0 { 874 if self.parameters.len() > 0 {
868 write!(f, "<")?; 875 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::{
8 generics::GenericParams, 8 generics::GenericParams,
9 path::{GenericArg, GenericArgs}, 9 path::{GenericArg, GenericArgs},
10 resolver::resolver_for_expr, 10 resolver::resolver_for_expr,
11 ContainerId, Lookup,
11}; 12};
12use hir_expand::name; 13use hir_expand::name;
13 14
@@ -660,18 +661,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
660 } 661 }
661 // add obligation for trait implementation, if this is a trait method 662 // add obligation for trait implementation, if this is a trait method
662 match def { 663 match def {
663 CallableDef::Function(f) => { 664 CallableDef::FunctionId(f) => {
664 if let Some(trait_) = f.parent_trait(self.db) { 665 if let ContainerId::TraitId(trait_) = f.lookup(self.db).container {
665 // construct a TraitDef 666 // construct a TraitDef
666 let substs = a_ty.parameters.prefix( 667 let substs = a_ty.parameters.prefix(
667 self.db 668 self.db
668 .generic_params(trait_.id.into()) 669 .generic_params(trait_.into())
669 .count_params_including_parent(), 670 .count_params_including_parent(),
670 ); 671 );
671 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); 672 self.obligations.push(Obligation::Trait(TraitRef {
673 trait_: trait_.into(),
674 substs,
675 }));
672 } 676 }
673 } 677 }
674 CallableDef::Struct(_) | CallableDef::EnumVariant(_) => {} 678 CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => {}
675 } 679 }
676 } 680 }
677 } 681 }
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::{
14 path::{GenericArg, PathSegment}, 14 path::{GenericArg, PathSegment},
15 resolver::{HasResolver, Resolver, TypeNs}, 15 resolver::{HasResolver, Resolver, TypeNs},
16 type_ref::{TypeBound, TypeRef}, 16 type_ref::{TypeBound, TypeRef},
17 AdtId, EnumVariantId, FunctionId, GenericDefId, LocalStructFieldId, StructId, VariantId, 17 AdtId, AstItemDef, EnumVariantId, FunctionId, GenericDefId, HasModule, LocalStructFieldId,
18 Lookup, StructId, VariantId,
18}; 19};
19use ra_arena::map::ArenaMap; 20use ra_arena::map::ArenaMap;
21use ra_db::CrateId;
20 22
21use super::{ 23use super::{
22 FnSig, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, 24 FnSig, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef,
@@ -546,9 +548,9 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
546/// Build the signature of a callable item (function, struct or enum variant). 548/// Build the signature of a callable item (function, struct or enum variant).
547pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSig { 549pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSig {
548 match def { 550 match def {
549 CallableDef::Function(f) => fn_sig_for_fn(db, f.id), 551 CallableDef::FunctionId(f) => fn_sig_for_fn(db, f),
550 CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s.id), 552 CallableDef::StructId(s) => fn_sig_for_struct_constructor(db, s),
551 CallableDef::EnumVariant(e) => fn_sig_for_enum_variant_constructor(db, e.into()), 553 CallableDef::EnumVariantId(e) => fn_sig_for_enum_variant_constructor(db, e),
552 } 554 }
553} 555}
554 556
@@ -643,7 +645,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> FnSig {
643fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { 645fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty {
644 let generics = db.generic_params(def.id.into()); 646 let generics = db.generic_params(def.id.into());
645 let substs = Substs::identity(&generics); 647 let substs = Substs::identity(&generics);
646 Ty::apply(TypeCtor::FnDef(def.into()), substs) 648 Ty::apply(TypeCtor::FnDef(def.id.into()), substs)
647} 649}
648 650
649/// Build the declared type of a const. 651/// Build the declared type of a const.
@@ -723,7 +725,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty {
723 } 725 }
724 let generics = db.generic_params(def.id.into()); 726 let generics = db.generic_params(def.id.into());
725 let substs = Substs::identity(&generics); 727 let substs = Substs::identity(&generics);
726 Ty::apply(TypeCtor::FnDef(def.into()), substs) 728 Ty::apply(TypeCtor::FnDef(def.id.into()), substs)
727} 729}
728 730
729fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) -> FnSig { 731fn 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) ->
749 } 751 }
750 let generics = db.generic_params(def.parent_enum(db).id.into()); 752 let generics = db.generic_params(def.parent_enum(db).id.into());
751 let substs = Substs::identity(&generics); 753 let substs = Substs::identity(&generics);
752 Ty::apply(TypeCtor::FnDef(def.into()), substs) 754 Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs)
753} 755}
754 756
755fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty { 757fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty {
@@ -806,18 +808,18 @@ impl From<ModuleDef> for Option<TypableDef> {
806 808
807#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 809#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
808pub enum CallableDef { 810pub enum CallableDef {
809 Function(Function), 811 FunctionId(FunctionId),
810 Struct(Struct), 812 StructId(StructId),
811 EnumVariant(EnumVariant), 813 EnumVariantId(EnumVariantId),
812} 814}
813impl_froms!(CallableDef: Function, Struct, EnumVariant); 815impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId);
814 816
815impl CallableDef { 817impl CallableDef {
816 pub fn krate(self, db: &impl HirDatabase) -> Option<crate::Crate> { 818 pub fn krate(self, db: &impl HirDatabase) -> CrateId {
817 match self { 819 match self {
818 CallableDef::Function(f) => f.krate(db), 820 CallableDef::FunctionId(f) => f.lookup(db).module(db).krate,
819 CallableDef::Struct(s) => s.krate(db), 821 CallableDef::StructId(s) => s.0.module(db).krate,
820 CallableDef::EnumVariant(e) => e.parent_enum(db).krate(db), 822 CallableDef::EnumVariantId(e) => e.parent.module(db).krate,
821 } 823 }
822 } 824 }
823} 825}
@@ -825,9 +827,9 @@ impl CallableDef {
825impl From<CallableDef> for GenericDefId { 827impl From<CallableDef> for GenericDefId {
826 fn from(def: CallableDef) -> GenericDefId { 828 fn from(def: CallableDef) -> GenericDefId {
827 match def { 829 match def {
828 CallableDef::Function(f) => f.id.into(), 830 CallableDef::FunctionId(f) => f.into(),
829 CallableDef::Struct(s) => s.id.into(), 831 CallableDef::StructId(s) => s.into(),
830 CallableDef::EnumVariant(e) => EnumVariantId::from(e).into(), 832 CallableDef::EnumVariantId(e) => e.into(),
831 } 833 }
832 } 834 }
833} 835}
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs
index 7c367230e..9beceb29c 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<Cal
26 ); 26 );
27 let (mut call_info, has_self) = match &calling_node { 27 let (mut call_info, has_self) = match &calling_node {
28 FnCallNode::CallExpr(expr) => { 28 FnCallNode::CallExpr(expr) => {
29 //FIXME: apply subst 29 //FIXME: don't poke into Ty
30 let (callable_def, _subst) = analyzer.type_of(db, &expr.expr()?)?.as_callable()?; 30 let (callable_def, _subst) = analyzer.type_of(db, &expr.expr()?)?.as_callable()?;
31 match callable_def { 31 match callable_def {
32 hir::CallableDef::Function(it) => { 32 hir::CallableDef::FunctionId(it) => {
33 (CallInfo::with_fn(db, it), it.has_self_param(db)) 33 let fn_def = it.into();
34 (CallInfo::with_fn(db, fn_def), fn_def.has_self_param(db))
35 }
36 hir::CallableDef::StructId(it) => (CallInfo::with_struct(db, it.into())?, false),
37 hir::CallableDef::EnumVariantId(it) => {
38 (CallInfo::with_enum_variant(db, it.into())?, false)
34 } 39 }
35 hir::CallableDef::Struct(it) => (CallInfo::with_struct(db, it)?, false),
36 hir::CallableDef::EnumVariant(it) => (CallInfo::with_enum_variant(db, it)?, false),
37 } 40 }
38 } 41 }
39 FnCallNode::MethodCallExpr(expr) => { 42 FnCallNode::MethodCallExpr(expr) => {