diff options
Diffstat (limited to 'crates/ra_hir/src/ty/lower.rs')
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 65 |
1 files changed, 34 insertions, 31 deletions
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 | } |