aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-01-10 17:40:45 +0000
committerFlorian Diebold <[email protected]>2020-01-11 22:33:04 +0000
commit4496e2a06a91e5825f355ce730af802643e8018a (patch)
treef0f41e2d9ff5045f34b38cd3d23bae11e5d56139 /crates/ra_hir_def
parent15fc643e05bf8273e378243edbfb3be7aea7ce11 (diff)
Apply review suggestions
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/db.rs7
-rw-r--r--crates/ra_hir_def/src/find_path.rs17
-rw-r--r--crates/ra_hir_def/src/item_scope.rs5
-rw-r--r--crates/ra_hir_def/src/path.rs14
-rw-r--r--crates/ra_hir_def/src/resolver.rs14
5 files changed, 23 insertions, 34 deletions
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 0c00627b5..da273eb11 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -107,13 +107,6 @@ pub trait DefDatabase: InternDatabase + AstDatabase {
107 // Remove this query completely, in favor of `Attrs::docs` method 107 // Remove this query completely, in favor of `Attrs::docs` method
108 #[salsa::invoke(Documentation::documentation_query)] 108 #[salsa::invoke(Documentation::documentation_query)]
109 fn documentation(&self, def: AttrDefId) -> Option<Documentation>; 109 fn documentation(&self, def: AttrDefId) -> Option<Documentation>;
110
111 #[salsa::invoke(crate::find_path::importable_locations_in_crate_query)]
112 fn importable_locations_in_crate(
113 &self,
114 item: crate::item_scope::ItemInNs,
115 krate: CrateId,
116 ) -> Arc<[(ModuleId, hir_expand::name::Name, crate::visibility::Visibility)]>;
117} 110}
118 111
119fn crate_def_map(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { 112fn crate_def_map(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs
index 69cdfc943..f7dc8acb7 100644
--- a/crates/ra_hir_def/src/find_path.rs
+++ b/crates/ra_hir_def/src/find_path.rs
@@ -34,7 +34,7 @@ fn find_path_inner(
34 // - if the item is already in scope, return the name under which it is 34 // - if the item is already in scope, return the name under which it is
35 let def_map = db.crate_def_map(from.krate); 35 let def_map = db.crate_def_map(from.krate);
36 let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; 36 let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope;
37 if let Some((name, _)) = from_scope.reverse_get(item) { 37 if let Some((name, _)) = from_scope.name_of(item) {
38 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); 38 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
39 } 39 }
40 40
@@ -77,7 +77,7 @@ fn find_path_inner(
77 let prelude_def_map = db.crate_def_map(prelude_module.krate); 77 let prelude_def_map = db.crate_def_map(prelude_module.krate);
78 let prelude_scope: &crate::item_scope::ItemScope = 78 let prelude_scope: &crate::item_scope::ItemScope =
79 &prelude_def_map.modules[prelude_module.local_id].scope; 79 &prelude_def_map.modules[prelude_module.local_id].scope;
80 if let Some((name, vis)) = prelude_scope.reverse_get(item) { 80 if let Some((name, vis)) = prelude_scope.name_of(item) {
81 if vis.is_visible_from(db, from) { 81 if vis.is_visible_from(db, from) {
82 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); 82 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
83 } 83 }
@@ -146,7 +146,7 @@ fn find_importable_locations(
146 .chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id)) 146 .chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id))
147 { 147 {
148 result.extend( 148 result.extend(
149 db.importable_locations_in_crate(item, krate) 149 importable_locations_in_crate(db, item, krate)
150 .iter() 150 .iter()
151 .filter(|(_, _, vis)| vis.is_visible_from(db, from)) 151 .filter(|(_, _, vis)| vis.is_visible_from(db, from))
152 .map(|(m, n, _)| (*m, n.clone())), 152 .map(|(m, n, _)| (*m, n.clone())),
@@ -160,17 +160,16 @@ fn find_importable_locations(
160/// non-private `use`s. 160/// non-private `use`s.
161/// 161///
162/// Note that the crate doesn't need to be the one in which the item is defined; 162/// Note that the crate doesn't need to be the one in which the item is defined;
163/// it might be re-exported in other crates. We cache this as a query since we 163/// it might be re-exported in other crates.
164/// need to walk the whole def map for it. 164fn importable_locations_in_crate(
165pub(crate) fn importable_locations_in_crate_query(
166 db: &impl DefDatabase, 165 db: &impl DefDatabase,
167 item: ItemInNs, 166 item: ItemInNs,
168 krate: CrateId, 167 krate: CrateId,
169) -> std::sync::Arc<[(ModuleId, Name, Visibility)]> { 168) -> Vec<(ModuleId, Name, Visibility)> {
170 let def_map = db.crate_def_map(krate); 169 let def_map = db.crate_def_map(krate);
171 let mut result = Vec::new(); 170 let mut result = Vec::new();
172 for (local_id, data) in def_map.modules.iter() { 171 for (local_id, data) in def_map.modules.iter() {
173 if let Some((name, vis)) = data.scope.reverse_get(item) { 172 if let Some((name, vis)) = data.scope.name_of(item) {
174 let is_private = if let Visibility::Module(private_to) = vis { 173 let is_private = if let Visibility::Module(private_to) = vis {
175 private_to.local_id == local_id 174 private_to.local_id == local_id
176 } else { 175 } else {
@@ -192,7 +191,7 @@ pub(crate) fn importable_locations_in_crate_query(
192 result.push((ModuleId { krate, local_id }, name.clone(), vis)); 191 result.push((ModuleId { krate, local_id }, name.clone(), vis));
193 } 192 }
194 } 193 }
195 result.into() 194 result
196} 195}
197 196
198#[cfg(test)] 197#[cfg(test)]
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index 87c50b34f..d74a1cef2 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -104,7 +104,7 @@ impl ItemScope {
104 } 104 }
105 } 105 }
106 106
107 pub(crate) fn reverse_get(&self, item: ItemInNs) -> Option<(&Name, Visibility)> { 107 pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
108 for (name, per_ns) in &self.visible { 108 for (name, per_ns) in &self.visible {
109 if let Some(vis) = item.match_with(*per_ns) { 109 if let Some(vis) = item.match_with(*per_ns) {
110 return Some((name, vis)); 110 return Some((name, vis));
@@ -207,8 +207,7 @@ impl ItemInNs {
207 207
208 pub fn as_module_def_id(self) -> Option<ModuleDefId> { 208 pub fn as_module_def_id(self) -> Option<ModuleDefId> {
209 match self { 209 match self {
210 ItemInNs::Types(t) => Some(t), 210 ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),
211 ItemInNs::Values(v) => Some(v),
212 ItemInNs::Macros(_) => None, 211 ItemInNs::Macros(_) => None,
213 } 212 }
214 } 213 }
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 7dd1939b9..9f93a5424 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -1,7 +1,11 @@
1//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`. 1//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`.
2mod lower; 2mod lower;
3 3
4use std::{fmt::Display, iter, sync::Arc}; 4use std::{
5 fmt::{self, Display},
6 iter,
7 sync::Arc,
8};
5 9
6use hir_expand::{ 10use hir_expand::{
7 hygiene::Hygiene, 11 hygiene::Hygiene,
@@ -78,12 +82,6 @@ impl ModPath {
78 } 82 }
79 self.segments.first() 83 self.segments.first()
80 } 84 }
81
82 pub fn to_ast(&self) -> ast::Path {
83 use ast::AstNode;
84 let parse = ast::SourceFile::parse(&self.to_string());
85 parse.tree().syntax().descendants().find_map(ast::Path::cast).unwrap()
86 }
87} 85}
88 86
89#[derive(Debug, Clone, PartialEq, Eq, Hash)] 87#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -255,7 +253,7 @@ impl From<Name> for ModPath {
255} 253}
256 254
257impl Display for ModPath { 255impl Display for ModPath {
258 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 256 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259 let mut first_segment = true; 257 let mut first_segment = true;
260 let mut add_segment = |s| { 258 let mut add_segment = |s| {
261 if !first_segment { 259 if !first_segment {
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 40d0cb588..f7bac5801 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -128,7 +128,7 @@ impl Resolver {
128 path: &ModPath, 128 path: &ModPath,
129 shadow: BuiltinShadowMode, 129 shadow: BuiltinShadowMode,
130 ) -> PerNs { 130 ) -> PerNs {
131 let (item_map, module) = match self.module() { 131 let (item_map, module) = match self.module_scope() {
132 Some(it) => it, 132 Some(it) => it,
133 None => return PerNs::none(), 133 None => return PerNs::none(),
134 }; 134 };
@@ -239,7 +239,7 @@ impl Resolver {
239 ) -> Option<Visibility> { 239 ) -> Option<Visibility> {
240 match visibility { 240 match visibility {
241 RawVisibility::Module(_) => { 241 RawVisibility::Module(_) => {
242 let (item_map, module) = match self.module() { 242 let (item_map, module) = match self.module_scope() {
243 Some(it) => it, 243 Some(it) => it,
244 None => return None, 244 None => return None,
245 }; 245 };
@@ -379,7 +379,7 @@ impl Resolver {
379 db: &impl DefDatabase, 379 db: &impl DefDatabase,
380 path: &ModPath, 380 path: &ModPath,
381 ) -> Option<MacroDefId> { 381 ) -> Option<MacroDefId> {
382 let (item_map, module) = self.module()?; 382 let (item_map, module) = self.module_scope()?;
383 item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros() 383 item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros()
384 } 384 }
385 385
@@ -403,7 +403,7 @@ impl Resolver {
403 traits 403 traits
404 } 404 }
405 405
406 fn module(&self) -> Option<(&CrateDefMap, LocalModuleId)> { 406 fn module_scope(&self) -> Option<(&CrateDefMap, LocalModuleId)> {
407 self.scopes.iter().rev().find_map(|scope| match scope { 407 self.scopes.iter().rev().find_map(|scope| match scope {
408 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), 408 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
409 409
@@ -411,13 +411,13 @@ impl Resolver {
411 }) 411 })
412 } 412 }
413 413
414 pub fn module_id(&self) -> Option<ModuleId> { 414 pub fn module(&self) -> Option<ModuleId> {
415 let (def_map, local_id) = self.module()?; 415 let (def_map, local_id) = self.module_scope()?;
416 Some(ModuleId { krate: def_map.krate, local_id }) 416 Some(ModuleId { krate: def_map.krate, local_id })
417 } 417 }
418 418
419 pub fn krate(&self) -> Option<CrateId> { 419 pub fn krate(&self) -> Option<CrateId> {
420 self.module().map(|t| t.0.krate) 420 self.module_scope().map(|t| t.0.krate)
421 } 421 }
422 422
423 pub fn where_predicates_in_scope<'a>( 423 pub fn where_predicates_in_scope<'a>(