diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-25 13:34:12 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-25 13:34:12 +0000 |
commit | df25dd4d882d7521140458b60f486c2f617608ee (patch) | |
tree | 0f1ec63bc811d36bd3b910c33d23607e40571160 /crates/ra_hir/src | |
parent | 69fa6403d56f44d8351727b8993fbee5e6189b97 (diff) | |
parent | 78791d6fac1ff426264e636545a07664d83d2039 (diff) |
Merge #2402
2402: Use ids for Callable r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer/expr.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 65 |
3 files changed, 56 insertions, 42 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 | }; |
12 | use hir_expand::name; | 13 | use 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 | }; |
19 | use ra_arena::map::ArenaMap; | 20 | use ra_arena::map::ArenaMap; |
21 | use ra_db::CrateId; | ||
20 | 22 | ||
21 | use super::{ | 23 | use 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). |
547 | pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSig { | 549 | pub(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 | ||
633 | fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { | 635 | fn 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 { | |||
643 | fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { | 645 | fn 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 | ||
706 | fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { | 708 | fn 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 | ||
729 | fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { | 731 | fn 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 | ||
754 | fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty { | 757 | fn 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)] |
807 | pub enum CallableDef { | 810 | pub enum CallableDef { |
808 | Function(Function), | 811 | FunctionId(FunctionId), |
809 | Struct(Struct), | 812 | StructId(StructId), |
810 | EnumVariant(EnumVariant), | 813 | EnumVariantId(EnumVariantId), |
811 | } | 814 | } |
812 | impl_froms!(CallableDef: Function, Struct, EnumVariant); | 815 | impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId); |
813 | 816 | ||
814 | impl CallableDef { | 817 | impl 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 { | |||
824 | impl From<CallableDef> for GenericDefId { | 827 | impl 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 | } |