diff options
author | Jonas Schievink <[email protected]> | 2021-01-25 18:15:39 +0000 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-01-25 18:15:39 +0000 |
commit | 08253d5473348e2f3061e0c8d84c62de537a5821 (patch) | |
tree | 06981374f0c397b45e31b74c97ed337853fe59d2 /crates | |
parent | 98a88ebb9ec46ed69dc7a3dbdc53a62ab9c1d14a (diff) |
Traverse parent DefMap for `super` paths
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir_def/src/nameres/path_resolution.rs | 37 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/block.rs | 26 |
2 files changed, 54 insertions, 9 deletions
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 @@ | |||
10 | //! | 10 | //! |
11 | //! `ReachedFixedPoint` signals about this. | 11 | //! `ReachedFixedPoint` signals about this. |
12 | 12 | ||
13 | use std::iter::successors; | ||
14 | |||
15 | use base_db::Edition; | 13 | use base_db::Edition; |
16 | use hir_expand::name; | 14 | use hir_expand::name; |
17 | use hir_expand::name::Name; | 15 | use hir_expand::name::Name; |
@@ -193,14 +191,35 @@ impl DefMap { | |||
193 | self.resolve_name_in_module(db, original_module, &segment, prefer_module) | 191 | self.resolve_name_in_module(db, original_module, &segment, prefer_module) |
194 | } | 192 | } |
195 | PathKind::Super(lvl) => { | 193 | PathKind::Super(lvl) => { |
196 | let m = successors(Some(original_module), |m| self.modules[*m].parent) | 194 | let mut module = original_module; |
197 | .nth(lvl as usize); | 195 | for i in 0..lvl { |
198 | if let Some(local_id) = m { | 196 | match self.modules[module].parent { |
199 | PerNs::types(self.module_id(local_id).into(), Visibility::Public) | 197 | Some(it) => module = it, |
200 | } else { | 198 | None => match &self.block { |
201 | log::debug!("super path in root module"); | 199 | Some(block) => { |
202 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); | 200 | // Look up remaining path in parent `DefMap` |
201 | let new_path = ModPath { | ||
202 | kind: PathKind::Super(lvl - i), | ||
203 | segments: path.segments.clone(), | ||
204 | }; | ||
205 | log::debug!("`super` path: {} -> {} in parent map", path, new_path); | ||
206 | return block.parent.resolve_path_fp_with_macro( | ||
207 | db, | ||
208 | mode, | ||
209 | block.parent_module, | ||
210 | &new_path, | ||
211 | shadow, | ||
212 | ); | ||
213 | } | ||
214 | None => { | ||
215 | log::debug!("super path in root module"); | ||
216 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); | ||
217 | } | ||
218 | }, | ||
219 | } | ||
203 | } | 220 | } |
221 | |||
222 | PerNs::types(self.module_id(module).into(), Visibility::Public) | ||
204 | } | 223 | } |
205 | PathKind::Abs => { | 224 | PathKind::Abs => { |
206 | // 2018-style absolute path -- only extern prelude | 225 | // 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() { | |||
95 | "#]], | 95 | "#]], |
96 | ); | 96 | ); |
97 | } | 97 | } |
98 | |||
99 | #[test] | ||
100 | fn super_imports() { | ||
101 | check_at( | ||
102 | r#" | ||
103 | mod module { | ||
104 | fn f() { | ||
105 | use super::Struct; | ||
106 | $0 | ||
107 | } | ||
108 | } | ||
109 | |||
110 | struct Struct {} | ||
111 | "#, | ||
112 | expect![[r#" | ||
113 | block scope | ||
114 | Struct: t | ||
115 | crate | ||
116 | Struct: t | ||
117 | module: t | ||
118 | |||
119 | crate::module | ||
120 | f: v | ||
121 | "#]], | ||
122 | ); | ||
123 | } | ||