aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-21 11:48:12 +0000
committerGitHub <[email protected]>2019-12-21 11:48:12 +0000
commitcdc9d682b066b110e0a44e5f8f1c574b38c16ba9 (patch)
treed1429d913719f73b4dee771e0c9ce85e871fc54a /crates/ra_hir_def/src/nameres
parent90ef070db3dce0a7acb9cd11d0b0d72de13c9d79 (diff)
parent2d3fdf3fb52f32ececdfa19df7ab2971a24bae71 (diff)
Merge #2629
2629: Remove imports from hir r=matklad a=matklad We only used them to avoid self-confirming completions (`use self::foo`), but that can be handled more locally. bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs46
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs68
2 files changed, 35 insertions, 79 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 45199fa11..9419461a8 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -26,8 +26,7 @@ use crate::{
26 path::{ModPath, PathKind}, 26 path::{ModPath, PathKind},
27 per_ns::PerNs, 27 per_ns::PerNs,
28 AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, 28 AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
29 LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, 29 LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
30 TypeAliasLoc, UnionLoc,
31}; 30};
32 31
33pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { 32pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -93,7 +92,7 @@ impl PartialResolvedImport {
93#[derive(Clone, Debug, Eq, PartialEq)] 92#[derive(Clone, Debug, Eq, PartialEq)]
94struct ImportDirective { 93struct ImportDirective {
95 module_id: LocalModuleId, 94 module_id: LocalModuleId,
96 import_id: LocalImportId, 95 import_id: raw::LocalImportId,
97 import: raw::ImportData, 96 import: raw::ImportData,
98 status: PartialResolvedImport, 97 status: PartialResolvedImport,
99} 98}
@@ -110,7 +109,7 @@ struct MacroDirective {
110struct DefCollector<'a, DB> { 109struct DefCollector<'a, DB> {
111 db: &'a DB, 110 db: &'a DB,
112 def_map: CrateDefMap, 111 def_map: CrateDefMap,
113 glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, LocalImportId)>>, 112 glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, raw::LocalImportId)>>,
114 unresolved_imports: Vec<ImportDirective>, 113 unresolved_imports: Vec<ImportDirective>,
115 resolved_imports: Vec<ImportDirective>, 114 resolved_imports: Vec<ImportDirective>,
116 unexpanded_macros: Vec<MacroDirective>, 115 unexpanded_macros: Vec<MacroDirective>,
@@ -218,8 +217,7 @@ where
218 if export { 217 if export {
219 self.update( 218 self.update(
220 self.def_map.root, 219 self.def_map.root,
221 None, 220 &[(name, Resolution { def: PerNs::macros(macro_), declaration: false })],
222 &[(name, Resolution { def: PerNs::macros(macro_), import: None })],
223 ); 221 );
224 } 222 }
225 } 223 }
@@ -374,7 +372,7 @@ where
374 // Module scoped macros is included 372 // Module scoped macros is included
375 let items = scope.collect_resolutions(); 373 let items = scope.collect_resolutions();
376 374
377 self.update(module_id, Some(import_id), &items); 375 self.update(module_id, &items);
378 } else { 376 } else {
379 // glob import from same crate => we do an initial 377 // glob import from same crate => we do an initial
380 // import, and then need to propagate any further 378 // import, and then need to propagate any further
@@ -384,7 +382,7 @@ where
384 // Module scoped macros is included 382 // Module scoped macros is included
385 let items = scope.collect_resolutions(); 383 let items = scope.collect_resolutions();
386 384
387 self.update(module_id, Some(import_id), &items); 385 self.update(module_id, &items);
388 // record the glob import in case we add further items 386 // record the glob import in case we add further items
389 let glob = self.glob_imports.entry(m.local_id).or_default(); 387 let glob = self.glob_imports.entry(m.local_id).or_default();
390 if !glob.iter().any(|it| *it == (module_id, import_id)) { 388 if !glob.iter().any(|it| *it == (module_id, import_id)) {
@@ -404,12 +402,12 @@ where
404 let variant = EnumVariantId { parent: e, local_id }; 402 let variant = EnumVariantId { parent: e, local_id };
405 let res = Resolution { 403 let res = Resolution {
406 def: PerNs::both(variant.into(), variant.into()), 404 def: PerNs::both(variant.into(), variant.into()),
407 import: Some(import_id), 405 declaration: false,
408 }; 406 };
409 (name, res) 407 (name, res)
410 }) 408 })
411 .collect::<Vec<_>>(); 409 .collect::<Vec<_>>();
412 self.update(module_id, Some(import_id), &resolutions); 410 self.update(module_id, &resolutions);
413 } 411 }
414 Some(d) => { 412 Some(d) => {
415 log::debug!("glob import {:?} from non-module/enum {:?}", import, d); 413 log::debug!("glob import {:?} from non-module/enum {:?}", import, d);
@@ -431,27 +429,21 @@ where
431 } 429 }
432 } 430 }
433 431
434 let resolution = Resolution { def, import: Some(import_id) }; 432 let resolution = Resolution { def, declaration: false };
435 self.update(module_id, Some(import_id), &[(name, resolution)]); 433 self.update(module_id, &[(name, resolution)]);
436 } 434 }
437 None => tested_by!(bogus_paths), 435 None => tested_by!(bogus_paths),
438 } 436 }
439 } 437 }
440 } 438 }
441 439
442 fn update( 440 fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, Resolution)]) {
443 &mut self, 441 self.update_recursive(module_id, resolutions, 0)
444 module_id: LocalModuleId,
445 import: Option<LocalImportId>,
446 resolutions: &[(Name, Resolution)],
447 ) {
448 self.update_recursive(module_id, import, resolutions, 0)
449 } 442 }
450 443
451 fn update_recursive( 444 fn update_recursive(
452 &mut self, 445 &mut self,
453 module_id: LocalModuleId, 446 module_id: LocalModuleId,
454 import: Option<LocalImportId>,
455 resolutions: &[(Name, Resolution)], 447 resolutions: &[(Name, Resolution)],
456 depth: usize, 448 depth: usize,
457 ) { 449 ) {
@@ -462,7 +454,7 @@ where
462 let scope = &mut self.def_map.modules[module_id].scope; 454 let scope = &mut self.def_map.modules[module_id].scope;
463 let mut changed = false; 455 let mut changed = false;
464 for (name, res) in resolutions { 456 for (name, res) in resolutions {
465 changed |= scope.push_res(name.clone(), res, import); 457 changed |= scope.push_res(name.clone(), res, depth == 0 && res.declaration);
466 } 458 }
467 459
468 if !changed { 460 if !changed {
@@ -475,9 +467,9 @@ where
475 .flat_map(|v| v.iter()) 467 .flat_map(|v| v.iter())
476 .cloned() 468 .cloned()
477 .collect::<Vec<_>>(); 469 .collect::<Vec<_>>();
478 for (glob_importing_module, glob_import) in glob_imports { 470 for (glob_importing_module, _glob_import) in glob_imports {
479 // We pass the glob import so that the tracked import in those modules is that glob import 471 // We pass the glob import so that the tracked import in those modules is that glob import
480 self.update_recursive(glob_importing_module, Some(glob_import), resolutions, depth + 1); 472 self.update_recursive(glob_importing_module, resolutions, depth + 1);
481 } 473 }
482 } 474 }
483 475
@@ -719,9 +711,9 @@ where
719 def: PerNs::types( 711 def: PerNs::types(
720 ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), 712 ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
721 ), 713 ),
722 import: None, 714 declaration: true,
723 }; 715 };
724 self.def_collector.update(self.module_id, None, &[(name, resolution)]); 716 self.def_collector.update(self.module_id, &[(name, resolution)]);
725 res 717 res
726 } 718 }
727 719
@@ -791,8 +783,8 @@ where
791 PerNs::types(def.into()) 783 PerNs::types(def.into())
792 } 784 }
793 }; 785 };
794 let resolution = Resolution { def, import: None }; 786 let resolution = Resolution { def, declaration: true };
795 self.def_collector.update(self.module_id, None, &[(name, resolution)]) 787 self.def_collector.update(self.module_id, &[(name, resolution)])
796 } 788 }
797 789
798 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { 790 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index ecb4d7c03..b10e458a2 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -7,24 +7,24 @@
7 7
8use std::{ops::Index, sync::Arc}; 8use std::{ops::Index, sync::Arc};
9 9
10use either::Either;
11use hir_expand::{ 10use hir_expand::{
12 ast_id_map::AstIdMap, 11 ast_id_map::AstIdMap,
13 db::AstDatabase, 12 db::AstDatabase,
14 hygiene::Hygiene, 13 hygiene::Hygiene,
15 name::{AsName, Name}, 14 name::{AsName, Name},
16}; 15};
17use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; 16use ra_arena::{impl_arena_id, Arena, RawId};
18use ra_syntax::{ 17use ra_syntax::{
19 ast::{self, AttrsOwner, NameOwner}, 18 ast::{self, AttrsOwner, NameOwner},
20 AstNode, AstPtr, 19 AstNode,
21}; 20};
22use test_utils::tested_by; 21use test_utils::tested_by;
23 22
24use crate::{ 23use crate::{attr::Attrs, db::DefDatabase, path::ModPath, FileAstId, HirFileId, InFile};
25 attr::Attrs, db::DefDatabase, path::ModPath, trace::Trace, FileAstId, HirFileId, InFile, 24
26 LocalImportId, 25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27}; 26pub(super) struct LocalImportId(RawId);
27impl_arena_id!(LocalImportId);
28 28
29/// `RawItems` is a set of top-level items in a file (except for impls). 29/// `RawItems` is a set of top-level items in a file (except for impls).
30/// 30///
@@ -41,35 +41,14 @@ pub struct RawItems {
41 items: Vec<RawItem>, 41 items: Vec<RawItem>,
42} 42}
43 43
44#[derive(Debug, Default, PartialEq, Eq)]
45pub struct ImportSourceMap {
46 map: ArenaMap<LocalImportId, ImportSourcePtr>,
47}
48
49type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
50
51impl ImportSourceMap {
52 pub fn get(&self, import: LocalImportId) -> ImportSourcePtr {
53 self.map[import].clone()
54 }
55}
56
57impl RawItems { 44impl RawItems {
58 pub(crate) fn raw_items_query( 45 pub(crate) fn raw_items_query(
59 db: &(impl DefDatabase + AstDatabase), 46 db: &(impl DefDatabase + AstDatabase),
60 file_id: HirFileId, 47 file_id: HirFileId,
61 ) -> Arc<RawItems> { 48 ) -> Arc<RawItems> {
62 db.raw_items_with_source_map(file_id).0
63 }
64
65 pub(crate) fn raw_items_with_source_map_query(
66 db: &(impl DefDatabase + AstDatabase),
67 file_id: HirFileId,
68 ) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
69 let mut collector = RawItemsCollector { 49 let mut collector = RawItemsCollector {
70 raw_items: RawItems::default(), 50 raw_items: RawItems::default(),
71 source_ast_id_map: db.ast_id_map(file_id), 51 source_ast_id_map: db.ast_id_map(file_id),
72 imports: Trace::new(),
73 file_id, 52 file_id,
74 hygiene: Hygiene::new(db, file_id), 53 hygiene: Hygiene::new(db, file_id),
75 }; 54 };
@@ -80,11 +59,8 @@ impl RawItems {
80 collector.process_module(None, item_list); 59 collector.process_module(None, item_list);
81 } 60 }
82 } 61 }
83 let mut raw_items = collector.raw_items; 62 let raw_items = collector.raw_items;
84 let (arena, map) = collector.imports.into_arena_and_map(); 63 Arc::new(raw_items)
85 raw_items.imports = arena;
86 let source_map = ImportSourceMap { map };
87 (Arc::new(raw_items), Arc::new(source_map))
88 } 64 }
89 65
90 pub(super) fn items(&self) -> &[RawItem] { 66 pub(super) fn items(&self) -> &[RawItem] {
@@ -223,7 +199,6 @@ pub(super) struct ImplData {
223 199
224struct RawItemsCollector { 200struct RawItemsCollector {
225 raw_items: RawItems, 201 raw_items: RawItems,
226 imports: Trace<LocalImportId, ImportData, ImportSourcePtr>,
227 source_ast_id_map: Arc<AstIdMap>, 202 source_ast_id_map: Arc<AstIdMap>,
228 file_id: HirFileId, 203 file_id: HirFileId,
229 hygiene: Hygiene, 204 hygiene: Hygiene,
@@ -330,7 +305,7 @@ impl RawItemsCollector {
330 ModPath::expand_use_item( 305 ModPath::expand_use_item(
331 InFile { value: use_item, file_id: self.file_id }, 306 InFile { value: use_item, file_id: self.file_id },
332 &self.hygiene, 307 &self.hygiene,
333 |path, use_tree, is_glob, alias| { 308 |path, _use_tree, is_glob, alias| {
334 let import_data = ImportData { 309 let import_data = ImportData {
335 path, 310 path,
336 alias, 311 alias,
@@ -339,11 +314,11 @@ impl RawItemsCollector {
339 is_extern_crate: false, 314 is_extern_crate: false,
340 is_macro_use: false, 315 is_macro_use: false,
341 }; 316 };
342 buf.push((import_data, Either::Left(AstPtr::new(use_tree)))); 317 buf.push(import_data);
343 }, 318 },
344 ); 319 );
345 for (import_data, ptr) in buf { 320 for import_data in buf {
346 self.push_import(current_module, attrs.clone(), import_data, ptr); 321 self.push_import(current_module, attrs.clone(), import_data);
347 } 322 }
348 } 323 }
349 324
@@ -366,12 +341,7 @@ impl RawItemsCollector {
366 is_extern_crate: true, 341 is_extern_crate: true,
367 is_macro_use, 342 is_macro_use,
368 }; 343 };
369 self.push_import( 344 self.push_import(current_module, attrs, import_data);
370 current_module,
371 attrs,
372 import_data,
373 Either::Right(AstPtr::new(&extern_crate)),
374 );
375 } 345 }
376 } 346 }
377 347
@@ -402,14 +372,8 @@ impl RawItemsCollector {
402 self.push_item(current_module, attrs, RawItemKind::Impl(imp)) 372 self.push_item(current_module, attrs, RawItemKind::Impl(imp))
403 } 373 }
404 374
405 fn push_import( 375 fn push_import(&mut self, current_module: Option<Module>, attrs: Attrs, data: ImportData) {
406 &mut self, 376 let import = self.raw_items.imports.alloc(data);
407 current_module: Option<Module>,
408 attrs: Attrs,
409 data: ImportData,
410 source: ImportSourcePtr,
411 ) {
412 let import = self.imports.alloc(|| source, || data);
413 self.push_item(current_module, attrs, RawItemKind::Import(import)) 377 self.push_item(current_module, attrs, RawItemKind::Import(import))
414 } 378 }
415 379