diff options
Diffstat (limited to 'crates/hir_def/src/find_path.rs')
-rw-r--r-- | crates/hir_def/src/find_path.rs | 37 |
1 files changed, 13 insertions, 24 deletions
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index db2d125ae..94a1d567d 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs | |||
@@ -53,12 +53,8 @@ fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option< | |||
53 | Some(ModPath::from_segments(PathKind::Super(0), Vec::new())) | 53 | Some(ModPath::from_segments(PathKind::Super(0), Vec::new())) |
54 | } else if let Some(parent_id) = def_map[from.local_id].parent { | 54 | } else if let Some(parent_id) = def_map[from.local_id].parent { |
55 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) | 55 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) |
56 | if item | 56 | let parent_id = def_map.module_id(parent_id); |
57 | == ItemInNs::Types(ModuleDefId::ModuleId(ModuleId { | 57 | if item == ItemInNs::Types(ModuleDefId::ModuleId(parent_id)) { |
58 | krate: from.krate, | ||
59 | local_id: parent_id, | ||
60 | })) | ||
61 | { | ||
62 | Some(ModPath::from_segments(PathKind::Super(1), Vec::new())) | 58 | Some(ModPath::from_segments(PathKind::Super(1), Vec::new())) |
63 | } else { | 59 | } else { |
64 | None | 60 | None |
@@ -110,7 +106,7 @@ fn find_path_inner( | |||
110 | // Base cases: | 106 | // Base cases: |
111 | 107 | ||
112 | // - if the item is already in scope, return the name under which it is | 108 | // - if the item is already in scope, return the name under which it is |
113 | let def_map = db.crate_def_map(from.krate); | 109 | let def_map = from.def_map(db); |
114 | let from_scope: &crate::item_scope::ItemScope = &def_map[from.local_id].scope; | 110 | let from_scope: &crate::item_scope::ItemScope = &def_map[from.local_id].scope; |
115 | let scope_name = | 111 | let scope_name = |
116 | if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None }; | 112 | if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None }; |
@@ -120,12 +116,8 @@ fn find_path_inner( | |||
120 | } | 116 | } |
121 | 117 | ||
122 | // - if the item is the crate root, return `crate` | 118 | // - if the item is the crate root, return `crate` |
123 | if item | 119 | let root = def_map.module_id(def_map.root()); |
124 | == ItemInNs::Types(ModuleDefId::ModuleId(ModuleId { | 120 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) { |
125 | krate: from.krate, | ||
126 | local_id: def_map.root(), | ||
127 | })) | ||
128 | { | ||
129 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); | 121 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); |
130 | } | 122 | } |
131 | 123 | ||
@@ -145,7 +137,7 @@ fn find_path_inner( | |||
145 | 137 | ||
146 | // - if the item is in the prelude, return the name from there | 138 | // - if the item is in the prelude, return the name from there |
147 | if let Some(prelude_module) = def_map.prelude() { | 139 | if let Some(prelude_module) = def_map.prelude() { |
148 | let prelude_def_map = db.crate_def_map(prelude_module.krate); | 140 | let prelude_def_map = prelude_module.def_map(db); |
149 | let prelude_scope: &crate::item_scope::ItemScope = | 141 | let prelude_scope: &crate::item_scope::ItemScope = |
150 | &prelude_def_map[prelude_module.local_id].scope; | 142 | &prelude_def_map[prelude_module.local_id].scope; |
151 | if let Some((name, vis)) = prelude_scope.name_of(item) { | 143 | if let Some((name, vis)) = prelude_scope.name_of(item) { |
@@ -175,7 +167,7 @@ fn find_path_inner( | |||
175 | 167 | ||
176 | // - otherwise, look for modules containing (reexporting) it and import it from one of those | 168 | // - otherwise, look for modules containing (reexporting) it and import it from one of those |
177 | 169 | ||
178 | let crate_root = ModuleId { local_id: def_map.root(), krate: from.krate }; | 170 | let crate_root = def_map.module_id(def_map.root()); |
179 | let crate_attrs = db.attrs(crate_root.into()); | 171 | let crate_attrs = db.attrs(crate_root.into()); |
180 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); | 172 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); |
181 | let mut best_path = None; | 173 | let mut best_path = None; |
@@ -283,19 +275,16 @@ fn find_local_import_locations( | |||
283 | // above `from` with any visibility. That means we do not need to descend into private siblings | 275 | // above `from` with any visibility. That means we do not need to descend into private siblings |
284 | // of `from` (and similar). | 276 | // of `from` (and similar). |
285 | 277 | ||
286 | let def_map = db.crate_def_map(from.krate); | 278 | let def_map = from.def_map(db); |
287 | 279 | ||
288 | // Compute the initial worklist. We start with all direct child modules of `from` as well as all | 280 | // Compute the initial worklist. We start with all direct child modules of `from` as well as all |
289 | // of its (recursive) parent modules. | 281 | // of its (recursive) parent modules. |
290 | let data = &def_map[from.local_id]; | 282 | let data = &def_map[from.local_id]; |
291 | let mut worklist = data | 283 | let mut worklist = |
292 | .children | 284 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); |
293 | .values() | ||
294 | .map(|child| ModuleId { krate: from.krate, local_id: *child }) | ||
295 | .collect::<Vec<_>>(); | ||
296 | let mut parent = data.parent; | 285 | let mut parent = data.parent; |
297 | while let Some(p) = parent { | 286 | while let Some(p) = parent { |
298 | worklist.push(ModuleId { krate: from.krate, local_id: p }); | 287 | worklist.push(def_map.module_id(p)); |
299 | parent = def_map[p].parent; | 288 | parent = def_map[p].parent; |
300 | } | 289 | } |
301 | 290 | ||
@@ -312,7 +301,7 @@ fn find_local_import_locations( | |||
312 | &def_map[module.local_id] | 301 | &def_map[module.local_id] |
313 | } else { | 302 | } else { |
314 | // The crate might reexport a module defined in another crate. | 303 | // The crate might reexport a module defined in another crate. |
315 | ext_def_map = db.crate_def_map(module.krate); | 304 | ext_def_map = module.def_map(db); |
316 | &ext_def_map[module.local_id] | 305 | &ext_def_map[module.local_id] |
317 | }; | 306 | }; |
318 | 307 | ||
@@ -375,7 +364,7 @@ mod tests { | |||
375 | parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap(); | 364 | parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap(); |
376 | let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap(); | 365 | let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap(); |
377 | 366 | ||
378 | let crate_def_map = db.crate_def_map(module.krate); | 367 | let crate_def_map = module.def_map(&db); |
379 | let resolved = crate_def_map | 368 | let resolved = crate_def_map |
380 | .resolve_path( | 369 | .resolve_path( |
381 | &db, | 370 | &db, |