From 08253d5473348e2f3061e0c8d84c62de537a5821 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 25 Jan 2021 19:15:39 +0100 Subject: Traverse parent DefMap for `super` paths --- crates/hir_def/src/nameres/path_resolution.rs | 37 ++++++++++++++++++++------- crates/hir_def/src/nameres/tests/block.rs | 26 +++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 8ce127dde..419e465ed 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -10,8 +10,6 @@ //! //! `ReachedFixedPoint` signals about this. -use std::iter::successors; - use base_db::Edition; use hir_expand::name; use hir_expand::name::Name; @@ -193,14 +191,35 @@ impl DefMap { self.resolve_name_in_module(db, original_module, &segment, prefer_module) } PathKind::Super(lvl) => { - let m = successors(Some(original_module), |m| self.modules[*m].parent) - .nth(lvl as usize); - if let Some(local_id) = m { - PerNs::types(self.module_id(local_id).into(), Visibility::Public) - } else { - log::debug!("super path in root module"); - return ResolvePathResult::empty(ReachedFixedPoint::Yes); + let mut module = original_module; + for i in 0..lvl { + match self.modules[module].parent { + Some(it) => module = it, + None => match &self.block { + Some(block) => { + // Look up remaining path in parent `DefMap` + let new_path = ModPath { + kind: PathKind::Super(lvl - i), + segments: path.segments.clone(), + }; + log::debug!("`super` path: {} -> {} in parent map", path, new_path); + return block.parent.resolve_path_fp_with_macro( + db, + mode, + block.parent_module, + &new_path, + shadow, + ); + } + None => { + log::debug!("super path in root module"); + return ResolvePathResult::empty(ReachedFixedPoint::Yes); + } + }, + } } + + PerNs::types(self.module_id(module).into(), Visibility::Public) } PathKind::Abs => { // 2018-style absolute path -- only extern prelude diff --git a/crates/hir_def/src/nameres/tests/block.rs b/crates/hir_def/src/nameres/tests/block.rs index 01d6326a7..470ca593e 100644 --- a/crates/hir_def/src/nameres/tests/block.rs +++ b/crates/hir_def/src/nameres/tests/block.rs @@ -95,3 +95,29 @@ fn outer() { "#]], ); } + +#[test] +fn super_imports() { + check_at( + r#" +mod module { + fn f() { + use super::Struct; + $0 + } +} + +struct Struct {} +"#, + expect![[r#" + block scope + Struct: t + crate + Struct: t + module: t + + crate::module + f: v + "#]], + ); +} -- cgit v1.2.3