diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 0692d3b2a..fa46ddfe9 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -28,6 +28,7 @@ use log; | |||
28 | use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError}; | 28 | use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError}; |
29 | use ra_arena::map::ArenaMap; | 29 | use ra_arena::map::ArenaMap; |
30 | use join_to_string::join; | 30 | use join_to_string::join; |
31 | use rustc_hash::FxHashMap; | ||
31 | 32 | ||
32 | use ra_db::Cancelable; | 33 | use ra_db::Cancelable; |
33 | 34 | ||
@@ -448,14 +449,14 @@ fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> { | |||
448 | }) | 449 | }) |
449 | } | 450 | } |
450 | 451 | ||
451 | pub fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> { | 452 | pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> { |
452 | Ok(Ty::Adt { | 453 | Ok(Ty::Adt { |
453 | def_id: s.def_id(), | 454 | def_id: s.def_id(), |
454 | name: s.name(db)?.unwrap_or_else(Name::missing), | 455 | name: s.name(db)?.unwrap_or_else(Name::missing), |
455 | }) | 456 | }) |
456 | } | 457 | } |
457 | 458 | ||
458 | pub fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable<Ty> { | 459 | pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable<Ty> { |
459 | let enum_parent = ev.parent_enum(db)?; | 460 | let enum_parent = ev.parent_enum(db)?; |
460 | 461 | ||
461 | type_for_enum(db, enum_parent) | 462 | type_for_enum(db, enum_parent) |
@@ -512,10 +513,18 @@ pub(super) fn type_for_field( | |||
512 | /// The result of type inference: A mapping from expressions and patterns to types. | 513 | /// The result of type inference: A mapping from expressions and patterns to types. |
513 | #[derive(Clone, PartialEq, Eq, Debug)] | 514 | #[derive(Clone, PartialEq, Eq, Debug)] |
514 | pub struct InferenceResult { | 515 | pub struct InferenceResult { |
516 | /// For each method call expr, record the function it resolved to. | ||
517 | method_resolutions: FxHashMap<ExprId, DefId>, | ||
515 | type_of_expr: ArenaMap<ExprId, Ty>, | 518 | type_of_expr: ArenaMap<ExprId, Ty>, |
516 | type_of_pat: ArenaMap<PatId, Ty>, | 519 | type_of_pat: ArenaMap<PatId, Ty>, |
517 | } | 520 | } |
518 | 521 | ||
522 | impl InferenceResult { | ||
523 | pub fn method_resolution(&self, expr: ExprId) -> Option<DefId> { | ||
524 | self.method_resolutions.get(&expr).map(|it| *it) | ||
525 | } | ||
526 | } | ||
527 | |||
519 | impl Index<ExprId> for InferenceResult { | 528 | impl Index<ExprId> for InferenceResult { |
520 | type Output = Ty; | 529 | type Output = Ty; |
521 | 530 | ||
@@ -541,6 +550,7 @@ struct InferenceContext<'a, D: HirDatabase> { | |||
541 | module: Module, | 550 | module: Module, |
542 | impl_block: Option<ImplBlock>, | 551 | impl_block: Option<ImplBlock>, |
543 | var_unification_table: InPlaceUnificationTable<TypeVarId>, | 552 | var_unification_table: InPlaceUnificationTable<TypeVarId>, |
553 | method_resolutions: FxHashMap<ExprId, DefId>, | ||
544 | type_of_expr: ArenaMap<ExprId, Ty>, | 554 | type_of_expr: ArenaMap<ExprId, Ty>, |
545 | type_of_pat: ArenaMap<PatId, Ty>, | 555 | type_of_pat: ArenaMap<PatId, Ty>, |
546 | /// The return type of the function being inferred. | 556 | /// The return type of the function being inferred. |
@@ -631,6 +641,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
631 | impl_block: Option<ImplBlock>, | 641 | impl_block: Option<ImplBlock>, |
632 | ) -> Self { | 642 | ) -> Self { |
633 | InferenceContext { | 643 | InferenceContext { |
644 | method_resolutions: FxHashMap::default(), | ||
634 | type_of_expr: ArenaMap::default(), | 645 | type_of_expr: ArenaMap::default(), |
635 | type_of_pat: ArenaMap::default(), | 646 | type_of_pat: ArenaMap::default(), |
636 | var_unification_table: InPlaceUnificationTable::new(), | 647 | var_unification_table: InPlaceUnificationTable::new(), |
@@ -655,6 +666,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
655 | *ty = resolved; | 666 | *ty = resolved; |
656 | } | 667 | } |
657 | InferenceResult { | 668 | InferenceResult { |
669 | method_resolutions: mem::replace(&mut self.method_resolutions, Default::default()), | ||
658 | type_of_expr: expr_types, | 670 | type_of_expr: expr_types, |
659 | type_of_pat: pat_types, | 671 | type_of_pat: pat_types, |
660 | } | 672 | } |
@@ -664,6 +676,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
664 | self.type_of_expr.insert(expr, ty); | 676 | self.type_of_expr.insert(expr, ty); |
665 | } | 677 | } |
666 | 678 | ||
679 | fn write_method_resolution(&mut self, expr: ExprId, def_id: DefId) { | ||
680 | self.method_resolutions.insert(expr, def_id); | ||
681 | } | ||
682 | |||
667 | fn write_pat_ty(&mut self, pat: PatId, ty: Ty) { | 683 | fn write_pat_ty(&mut self, pat: PatId, ty: Ty) { |
668 | self.type_of_pat.insert(pat, ty); | 684 | self.type_of_pat.insert(pat, ty); |
669 | } | 685 | } |
@@ -900,7 +916,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
900 | let receiver_ty = self.infer_expr(*receiver, &Expectation::none())?; | 916 | let receiver_ty = self.infer_expr(*receiver, &Expectation::none())?; |
901 | let resolved = receiver_ty.clone().lookup_method(self.db, method_name)?; | 917 | let resolved = receiver_ty.clone().lookup_method(self.db, method_name)?; |
902 | let method_ty = match resolved { | 918 | let method_ty = match resolved { |
903 | Some(def_id) => self.db.type_for_def(def_id)?, | 919 | Some(def_id) => { |
920 | self.write_method_resolution(expr, def_id); | ||
921 | self.db.type_for_def(def_id)? | ||
922 | } | ||
904 | None => Ty::Unknown, | 923 | None => Ty::Unknown, |
905 | }; | 924 | }; |
906 | let method_ty = self.insert_type_vars(method_ty); | 925 | let method_ty = self.insert_type_vars(method_ty); |