diff options
author | Aleksey Kladov <[email protected]> | 2018-12-27 17:07:21 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-27 17:07:21 +0000 |
commit | d963042ca9da93be8d5922ce46ea26dc6a79c929 (patch) | |
tree | 76f7bfc934dda4e9dfc956fc86fe73379d86a332 /crates/ra_hir/src/module | |
parent | 3b820bcca3a66660d0c5960f2a5c8f765095333e (diff) |
introduce hir::Name
Diffstat (limited to 'crates/ra_hir/src/module')
-rw-r--r-- | crates/ra_hir/src/module/imp.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir/src/module/nameres.rs | 25 | ||||
-rw-r--r-- | crates/ra_hir/src/module/nameres/tests.rs | 7 |
3 files changed, 26 insertions, 25 deletions
diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs index 748fdb64e..eded85a63 100644 --- a/crates/ra_hir/src/module/imp.rs +++ b/crates/ra_hir/src/module/imp.rs | |||
@@ -1,16 +1,13 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::ast::{self, NameOwner}; |
4 | ast::{self, NameOwner}, | ||
5 | SmolStr, | ||
6 | }; | ||
7 | use relative_path::RelativePathBuf; | 4 | use relative_path::RelativePathBuf; |
8 | use rustc_hash::{FxHashMap, FxHashSet}; | 5 | use rustc_hash::{FxHashMap, FxHashSet}; |
9 | use arrayvec::ArrayVec; | 6 | use arrayvec::ArrayVec; |
10 | use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; | 7 | use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; |
11 | 8 | ||
12 | use crate::{ | 9 | use crate::{ |
13 | HirDatabase, | 10 | HirDatabase, Name, AsName, |
14 | }; | 11 | }; |
15 | 12 | ||
16 | use super::{ | 13 | use super::{ |
@@ -20,12 +17,12 @@ use super::{ | |||
20 | 17 | ||
21 | #[derive(Clone, Hash, PartialEq, Eq, Debug)] | 18 | #[derive(Clone, Hash, PartialEq, Eq, Debug)] |
22 | pub enum Submodule { | 19 | pub enum Submodule { |
23 | Declaration(SmolStr), | 20 | Declaration(Name), |
24 | Definition(SmolStr, ModuleSource), | 21 | Definition(Name, ModuleSource), |
25 | } | 22 | } |
26 | 23 | ||
27 | impl Submodule { | 24 | impl Submodule { |
28 | fn name(&self) -> &SmolStr { | 25 | fn name(&self) -> &Name { |
29 | match self { | 26 | match self { |
30 | Submodule::Declaration(name) => name, | 27 | Submodule::Declaration(name) => name, |
31 | Submodule::Definition(name, _) => name, | 28 | Submodule::Definition(name, _) => name, |
@@ -35,14 +32,14 @@ impl Submodule { | |||
35 | 32 | ||
36 | pub(crate) fn modules<'a>( | 33 | pub(crate) fn modules<'a>( |
37 | root: impl ast::ModuleItemOwner<'a>, | 34 | root: impl ast::ModuleItemOwner<'a>, |
38 | ) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> { | 35 | ) -> impl Iterator<Item = (Name, ast::Module<'a>)> { |
39 | root.items() | 36 | root.items() |
40 | .filter_map(|item| match item { | 37 | .filter_map(|item| match item { |
41 | ast::ModuleItem::Module(m) => Some(m), | 38 | ast::ModuleItem::Module(m) => Some(m), |
42 | _ => None, | 39 | _ => None, |
43 | }) | 40 | }) |
44 | .filter_map(|module| { | 41 | .filter_map(|module| { |
45 | let name = module.name()?.text(); | 42 | let name = module.name()?.as_name(); |
46 | Some((name, module)) | 43 | Some((name, module)) |
47 | }) | 44 | }) |
48 | } | 45 | } |
@@ -155,7 +152,7 @@ fn build_subtree( | |||
155 | fn resolve_submodule( | 152 | fn resolve_submodule( |
156 | db: &impl HirDatabase, | 153 | db: &impl HirDatabase, |
157 | source: ModuleSource, | 154 | source: ModuleSource, |
158 | name: &SmolStr, | 155 | name: &Name, |
159 | ) -> (Vec<FileId>, Option<Problem>) { | 156 | ) -> (Vec<FileId>, Option<Problem>) { |
160 | // FIXME: handle submodules of inline modules properly | 157 | // FIXME: handle submodules of inline modules properly |
161 | let file_id = source.file_id(); | 158 | let file_id = source.file_id(); |
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 98cd225dd..68eb02a98 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs | |||
@@ -14,14 +14,12 @@ | |||
14 | //! modifications (that is, typing inside a function shold not change IMIs), | 14 | //! modifications (that is, typing inside a function shold not change IMIs), |
15 | //! such that the results of name resolution can be preserved unless the module | 15 | //! such that the results of name resolution can be preserved unless the module |
16 | //! structure itself is modified. | 16 | //! structure itself is modified. |
17 | use std::{ | 17 | use std::sync::Arc; |
18 | sync::Arc, | ||
19 | }; | ||
20 | 18 | ||
21 | use rustc_hash::FxHashMap; | 19 | use rustc_hash::FxHashMap; |
22 | use ra_syntax::{ | 20 | use ra_syntax::{ |
23 | TextRange, | 21 | TextRange, |
24 | SmolStr, SyntaxKind::{self, *}, | 22 | SyntaxKind::{self, *}, |
25 | ast::{self, AstNode} | 23 | ast::{self, AstNode} |
26 | }; | 24 | }; |
27 | use ra_db::SourceRootId; | 25 | use ra_db::SourceRootId; |
@@ -32,6 +30,7 @@ use crate::{ | |||
32 | SourceItemId, SourceFileItemId, SourceFileItems, | 30 | SourceItemId, SourceFileItemId, SourceFileItems, |
33 | Path, PathKind, | 31 | Path, PathKind, |
34 | HirDatabase, Crate, | 32 | HirDatabase, Crate, |
33 | Name, AsName, | ||
35 | module::{Module, ModuleId, ModuleTree}, | 34 | module::{Module, ModuleId, ModuleTree}, |
36 | }; | 35 | }; |
37 | 36 | ||
@@ -45,14 +44,14 @@ pub struct ItemMap { | |||
45 | 44 | ||
46 | #[derive(Debug, Default, PartialEq, Eq, Clone)] | 45 | #[derive(Debug, Default, PartialEq, Eq, Clone)] |
47 | pub struct ModuleScope { | 46 | pub struct ModuleScope { |
48 | items: FxHashMap<SmolStr, Resolution>, | 47 | items: FxHashMap<Name, Resolution>, |
49 | } | 48 | } |
50 | 49 | ||
51 | impl ModuleScope { | 50 | impl ModuleScope { |
52 | pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a SmolStr, &'a Resolution)> + 'a { | 51 | pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a { |
53 | self.items.iter() | 52 | self.items.iter() |
54 | } | 53 | } |
55 | pub fn get(&self, name: &SmolStr) -> Option<&Resolution> { | 54 | pub fn get(&self, name: &Name) -> Option<&Resolution> { |
56 | self.items.get(name) | 55 | self.items.get(name) |
57 | } | 56 | } |
58 | } | 57 | } |
@@ -72,7 +71,7 @@ pub struct InputModuleItems { | |||
72 | #[derive(Debug, PartialEq, Eq)] | 71 | #[derive(Debug, PartialEq, Eq)] |
73 | struct ModuleItem { | 72 | struct ModuleItem { |
74 | id: SourceFileItemId, | 73 | id: SourceFileItemId, |
75 | name: SmolStr, | 74 | name: Name, |
76 | kind: SyntaxKind, | 75 | kind: SyntaxKind, |
77 | vis: Vis, | 76 | vis: Vis, |
78 | } | 77 | } |
@@ -260,7 +259,7 @@ impl InputModuleItems { | |||
260 | 259 | ||
261 | impl ModuleItem { | 260 | impl ModuleItem { |
262 | fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> { | 261 | fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> { |
263 | let name = item.name()?.text(); | 262 | let name = item.name()?.as_name(); |
264 | let kind = item.syntax().kind(); | 263 | let kind = item.syntax().kind(); |
265 | let vis = Vis::Other; | 264 | let vis = Vis::Other; |
266 | let id = file_items.id_of_unchecked(item.syntax()); | 265 | let id = file_items.id_of_unchecked(item.syntax()); |
@@ -328,7 +327,11 @@ where | |||
328 | for dep in krate.dependencies(self.db) { | 327 | for dep in krate.dependencies(self.db) { |
329 | if let Some(module) = dep.krate.root_module(self.db)? { | 328 | if let Some(module) = dep.krate.root_module(self.db)? { |
330 | let def_id = module.def_id(self.db); | 329 | let def_id = module.def_id(self.db); |
331 | self.add_module_item(&mut module_items, dep.name, PerNs::types(def_id)); | 330 | self.add_module_item( |
331 | &mut module_items, | ||
332 | dep.name.clone(), | ||
333 | PerNs::types(def_id), | ||
334 | ); | ||
332 | } | 335 | } |
333 | } | 336 | } |
334 | }; | 337 | }; |
@@ -389,7 +392,7 @@ where | |||
389 | Ok(()) | 392 | Ok(()) |
390 | } | 393 | } |
391 | 394 | ||
392 | fn add_module_item(&self, module_items: &mut ModuleScope, name: SmolStr, def_id: PerNs<DefId>) { | 395 | fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs<DefId>) { |
393 | let resolution = Resolution { | 396 | let resolution = Resolution { |
394 | def_id, | 397 | def_id, |
395 | import: None, | 398 | import: None, |
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs index 03ea5c1d6..165ac81c8 100644 --- a/crates/ra_hir/src/module/nameres/tests.rs +++ b/crates/ra_hir/src/module/nameres/tests.rs | |||
@@ -9,6 +9,7 @@ use crate::{ | |||
9 | self as hir, | 9 | self as hir, |
10 | db::HirDatabase, | 10 | db::HirDatabase, |
11 | mock::MockDatabase, | 11 | mock::MockDatabase, |
12 | Name, | ||
12 | }; | 13 | }; |
13 | 14 | ||
14 | fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { | 15 | fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { |
@@ -38,7 +39,7 @@ fn item_map_smoke_test() { | |||
38 | pub struct Baz; | 39 | pub struct Baz; |
39 | ", | 40 | ", |
40 | ); | 41 | ); |
41 | let name = SmolStr::from("Baz"); | 42 | let name = Name::new(SmolStr::from("Baz")); |
42 | let resolution = &item_map.per_module[&module_id].items[&name]; | 43 | let resolution = &item_map.per_module[&module_id].items[&name]; |
43 | assert!(resolution.def_id.take_types().is_some()); | 44 | assert!(resolution.def_id.take_types().is_some()); |
44 | } | 45 | } |
@@ -57,7 +58,7 @@ fn test_self() { | |||
57 | pub struct Baz; | 58 | pub struct Baz; |
58 | ", | 59 | ", |
59 | ); | 60 | ); |
60 | let name = SmolStr::from("Baz"); | 61 | let name = Name::new(SmolStr::from("Baz")); |
61 | let resolution = &item_map.per_module[&module_id].items[&name]; | 62 | let resolution = &item_map.per_module[&module_id].items[&name]; |
62 | assert!(resolution.def_id.take_types().is_some()); | 63 | assert!(resolution.def_id.take_types().is_some()); |
63 | } | 64 | } |
@@ -90,7 +91,7 @@ fn item_map_across_crates() { | |||
90 | let module_id = module.module_id; | 91 | let module_id = module.module_id; |
91 | let item_map = db.item_map(source_root).unwrap(); | 92 | let item_map = db.item_map(source_root).unwrap(); |
92 | 93 | ||
93 | let name = SmolStr::from("Baz"); | 94 | let name = Name::new(SmolStr::from("Baz")); |
94 | let resolution = &item_map.per_module[&module_id].items[&name]; | 95 | let resolution = &item_map.per_module[&module_id].items[&name]; |
95 | assert!(resolution.def_id.take_types().is_some()); | 96 | assert!(resolution.def_id.take_types().is_some()); |
96 | } | 97 | } |