aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-14 10:00:46 +0000
committerGitHub <[email protected]>2021-01-14 10:00:46 +0000
commit184f4cbf5df909146b05aaa3b7f2f0b27ac36590 (patch)
treec9d803d2fefdc31278338fc67f6df8d1bf747a12 /crates
parent39167b97d8a62adadecc9b67caac65ec556768cf (diff)
parentcca0dfa79ed968d41464eb7beb7aaa970e2d429f (diff)
Merge #7110
7110: Deduplicate macros when offering completion r=matklad a=AdnoC Closes https://github.com/rust-analyzer/rust-analyzer/issues/7081 When iterating over the names within the `hir_def::resolver::Scope` for a module, track what macros are in the `hir_def::item_scope::ItemScope::legacy_macros` collection for the module. When iterating over names from the prelude, do not proccess the name if it had been in the `legacy_macros` collection. This is implemented with a `FxHashSet` in the `Scope::process_names` function that is populated when iterating over `legacy_macros` and checked when iterating over the prelude. Alternative implementation could instead query the `legacy_macros` `FxHashMap` directly when processing names in the prelude. Also, I'd like to add a test for this, but I'm not sure where it could be added. Co-authored-by: AdnoC <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/hir_def/src/per_ns.rs2
-rw-r--r--crates/hir_def/src/resolver.rs10
-rw-r--r--crates/hir_def/src/visibility.rs2
3 files changed, 10 insertions, 4 deletions
diff --git a/crates/hir_def/src/per_ns.rs b/crates/hir_def/src/per_ns.rs
index 74665c588..a594afce6 100644
--- a/crates/hir_def/src/per_ns.rs
+++ b/crates/hir_def/src/per_ns.rs
@@ -7,7 +7,7 @@ use hir_expand::MacroDefId;
7 7
8use crate::{item_scope::ItemInNs, visibility::Visibility, ModuleDefId}; 8use crate::{item_scope::ItemInNs, visibility::Visibility, ModuleDefId};
9 9
10#[derive(Debug, Copy, Clone, PartialEq, Eq)] 10#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11pub struct PerNs { 11pub struct PerNs {
12 pub types: Option<(ModuleDefId, Visibility)>, 12 pub types: Option<(ModuleDefId, Visibility)>,
13 pub values: Option<(ModuleDefId, Visibility)>, 13 pub values: Option<(ModuleDefId, Visibility)>,
diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs
index e4152a0be..61059c349 100644
--- a/crates/hir_def/src/resolver.rs
+++ b/crates/hir_def/src/resolver.rs
@@ -490,6 +490,7 @@ pub enum ScopeDef {
490 490
491impl Scope { 491impl Scope {
492 fn process_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { 492 fn process_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
493 let mut seen = FxHashSet::default();
493 match self { 494 match self {
494 Scope::ModuleScope(m) => { 495 Scope::ModuleScope(m) => {
495 // FIXME: should we provide `self` here? 496 // FIXME: should we provide `self` here?
@@ -503,7 +504,9 @@ impl Scope {
503 f(name.clone(), ScopeDef::PerNs(def)); 504 f(name.clone(), ScopeDef::PerNs(def));
504 }); 505 });
505 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { 506 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
506 f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_, Visibility::Public))); 507 let scope = PerNs::macros(macro_, Visibility::Public);
508 seen.insert((name.clone(), scope));
509 f(name.clone(), ScopeDef::PerNs(scope));
507 }); 510 });
508 m.crate_def_map.extern_prelude.iter().for_each(|(name, &def)| { 511 m.crate_def_map.extern_prelude.iter().for_each(|(name, &def)| {
509 f(name.clone(), ScopeDef::PerNs(PerNs::types(def, Visibility::Public))); 512 f(name.clone(), ScopeDef::PerNs(PerNs::types(def, Visibility::Public)));
@@ -514,7 +517,10 @@ impl Scope {
514 if let Some(prelude) = m.crate_def_map.prelude { 517 if let Some(prelude) = m.crate_def_map.prelude {
515 let prelude_def_map = db.crate_def_map(prelude.krate); 518 let prelude_def_map = db.crate_def_map(prelude.krate);
516 prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| { 519 prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| {
517 f(name.clone(), ScopeDef::PerNs(def)); 520 let seen_tuple = (name.clone(), def);
521 if !seen.contains(&seen_tuple) {
522 f(seen_tuple.0, ScopeDef::PerNs(def));
523 }
518 }); 524 });
519 } 525 }
520 } 526 }
diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs
index e6e0853a3..f3bc9d680 100644
--- a/crates/hir_def/src/visibility.rs
+++ b/crates/hir_def/src/visibility.rs
@@ -85,7 +85,7 @@ impl RawVisibility {
85} 85}
86 86
87/// Visibility of an item, with the path resolved. 87/// Visibility of an item, with the path resolved.
88#[derive(Debug, Copy, Clone, PartialEq, Eq)] 88#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
89pub enum Visibility { 89pub enum Visibility {
90 /// Visibility is restricted to a certain module. 90 /// Visibility is restricted to a certain module.
91 Module(ModuleId), 91 Module(ModuleId),