aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
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.rs65
-rw-r--r--crates/ra_ide_api/src/call_info.rs13
4 files changed, 64 insertions, 47 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 1ceafd9b1..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, GenericDefId, LocalStructFieldId, 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), 551 CallableDef::FunctionId(f) => fn_sig_for_fn(db, f),
550 CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s), 552 CallableDef::StructId(s) => fn_sig_for_struct_constructor(db, s),
551 CallableDef::EnumVariant(e) => fn_sig_for_enum_variant_constructor(db, e), 553 CallableDef::EnumVariantId(e) => fn_sig_for_enum_variant_constructor(db, e),
552 } 554 }
553} 555}
554 556
@@ -630,9 +632,9 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -
630 Substs(defaults) 632 Substs(defaults)
631} 633}
632 634
633fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { 635fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> FnSig {
634 let data = db.function_data(def.id); 636 let data = db.function_data(def);
635 let resolver = def.id.resolver(db); 637 let resolver = def.resolver(db);
636 let params = data.params.iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>(); 638 let params = data.params.iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>();
637 let ret = Ty::from_hir(db, &resolver, &data.ret_type); 639 let ret = Ty::from_hir(db, &resolver, &data.ret_type);
638 FnSig::from_params_and_return(params, ret) 640 FnSig::from_params_and_return(params, ret)
@@ -643,7 +645,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> 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.
@@ -703,15 +705,15 @@ impl From<Option<BuiltinFloat>> for Uncertain<FloatTy> {
703 } 705 }
704} 706}
705 707
706fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { 708fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> FnSig {
707 let struct_data = db.struct_data(def.id.into()); 709 let struct_data = db.struct_data(def.into());
708 let fields = struct_data.variant_data.fields(); 710 let fields = struct_data.variant_data.fields();
709 let resolver = def.id.resolver(db); 711 let resolver = def.resolver(db);
710 let params = fields 712 let params = fields
711 .iter() 713 .iter()
712 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) 714 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
713 .collect::<Vec<_>>(); 715 .collect::<Vec<_>>();
714 let ret = type_for_adt(db, def); 716 let ret = type_for_adt(db, Struct::from(def));
715 FnSig::from_params_and_return(params, ret) 717 FnSig::from_params_and_return(params, ret)
716} 718}
717 719
@@ -723,20 +725,21 @@ 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: EnumVariant) -> FnSig { 731fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) -> FnSig {
730 let var_data = def.variant_data(db); 732 let enum_data = db.enum_data(def.parent);
731 let fields = var_data.fields(); 733 let var_data = &enum_data.variants[def.local_id];
732 let resolver = def.parent.id.resolver(db); 734 let fields = var_data.variant_data.fields();
735 let resolver = def.parent.resolver(db);
733 let params = fields 736 let params = fields
734 .iter() 737 .iter()
735 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) 738 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
736 .collect::<Vec<_>>(); 739 .collect::<Vec<_>>();
737 let generics = db.generic_params(def.parent_enum(db).id.into()); 740 let generics = db.generic_params(def.parent.into());
738 let substs = Substs::identity(&generics); 741 let substs = Substs::identity(&generics);
739 let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs); 742 let ret = type_for_adt(db, Enum::from(def.parent)).subst(&substs);
740 FnSig::from_params_and_return(params, ret) 743 FnSig::from_params_and_return(params, ret)
741} 744}
742 745
@@ -748,7 +751,7 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) ->
748 } 751 }
749 let generics = db.generic_params(def.parent_enum(db).id.into()); 752 let generics = db.generic_params(def.parent_enum(db).id.into());
750 let substs = Substs::identity(&generics); 753 let substs = Substs::identity(&generics);
751 Ty::apply(TypeCtor::FnDef(def.into()), substs) 754 Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs)
752} 755}
753 756
754fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty { 757fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty {
@@ -805,18 +808,18 @@ impl From<ModuleDef> for Option<TypableDef> {
805 808
806#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 809#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
807pub enum CallableDef { 810pub enum CallableDef {
808 Function(Function), 811 FunctionId(FunctionId),
809 Struct(Struct), 812 StructId(StructId),
810 EnumVariant(EnumVariant), 813 EnumVariantId(EnumVariantId),
811} 814}
812impl_froms!(CallableDef: Function, Struct, EnumVariant); 815impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId);
813 816
814impl CallableDef { 817impl CallableDef {
815 pub fn krate(self, db: &impl HirDatabase) -> Option<crate::Crate> { 818 pub fn krate(self, db: &impl HirDatabase) -> CrateId {
816 match self { 819 match self {
817 CallableDef::Function(f) => f.krate(db), 820 CallableDef::FunctionId(f) => f.lookup(db).module(db).krate,
818 CallableDef::Struct(s) => s.krate(db), 821 CallableDef::StructId(s) => s.0.module(db).krate,
819 CallableDef::EnumVariant(e) => e.parent_enum(db).krate(db), 822 CallableDef::EnumVariantId(e) => e.parent.module(db).krate,
820 } 823 }
821 } 824 }
822} 825}
@@ -824,9 +827,9 @@ impl CallableDef {
824impl From<CallableDef> for GenericDefId { 827impl From<CallableDef> for GenericDefId {
825 fn from(def: CallableDef) -> GenericDefId { 828 fn from(def: CallableDef) -> GenericDefId {
826 match def { 829 match def {
827 CallableDef::Function(f) => f.id.into(), 830 CallableDef::FunctionId(f) => f.into(),
828 CallableDef::Struct(s) => s.id.into(), 831 CallableDef::StructId(s) => s.into(),
829 CallableDef::EnumVariant(e) => EnumVariantId::from(e).into(), 832 CallableDef::EnumVariantId(e) => e.into(),
830 } 833 }
831 } 834 }
832} 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) => {