From d4db61b9a151a2a46c4067e61b0a4b1a9e3c73ec Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Dec 2018 22:59:18 +0100 Subject: Resolve the self parameter during type inference --- crates/ra_hir/src/path.rs | 5 +++++ crates/ra_hir/src/ty.rs | 14 ++++++++++++-- crates/ra_hir/src/ty/tests/data/0007_self.txt | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs index 93f7203fe..9fdfa0d13 100644 --- a/crates/ra_hir/src/path.rs +++ b/crates/ra_hir/src/path.rs @@ -70,6 +70,11 @@ impl Path { self.kind == PathKind::Plain && self.segments.len() == 1 } + /// `true` if this path is just a standalone `self` + pub fn is_self(&self) -> bool { + self.kind == PathKind::Self_ && self.segments.len() == 0 + } + /// If this path is a single identifier, like `foo`, return its name. pub fn as_ident(&self) -> Option<&Name> { if self.kind != PathKind::Plain || self.segments.len() > 1 { diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index c762ec606..5ea62a14c 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -496,6 +496,8 @@ impl InferenceResult { struct InferenceContext<'a, D: HirDatabase> { db: &'a D, scopes: Arc, + /// The self param for the current method, if it exists. + self_param: Option, module: Module, var_unification_table: InPlaceUnificationTable, type_of: FxHashMap, @@ -506,6 +508,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { InferenceContext { type_of: FxHashMap::default(), var_unification_table: InPlaceUnificationTable::new(), + self_param: None, // set during parameter typing db, scopes, module, @@ -628,6 +631,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let ty = self.resolve_ty_as_possible(ty.clone()); return Ok(Some(ty)); }; + } else if path.is_self() { + // resolve `self` param + let self_param = ctry!(self.self_param); + let ty = ctry!(self.type_of.get(&self_param)); + let ty = self.resolve_ty_as_possible(ty.clone()); + return Ok(Some(ty)); }; // resolve in module @@ -940,8 +949,9 @@ pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Cancelable