aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/module')
-rw-r--r--crates/ra_hir/src/module/imp.rs19
-rw-r--r--crates/ra_hir/src/module/nameres.rs25
-rw-r--r--crates/ra_hir/src/module/nameres/tests.rs66
3 files changed, 77 insertions, 33 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 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::{ 3use ra_syntax::ast::{self, NameOwner};
4 ast::{self, NameOwner},
5 SmolStr,
6};
7use relative_path::RelativePathBuf; 4use relative_path::RelativePathBuf;
8use rustc_hash::{FxHashMap, FxHashSet}; 5use rustc_hash::{FxHashMap, FxHashSet};
9use arrayvec::ArrayVec; 6use arrayvec::ArrayVec;
10use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; 7use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId};
11 8
12use crate::{ 9use crate::{
13 HirDatabase, 10 HirDatabase, Name, AsName,
14}; 11};
15 12
16use super::{ 13use super::{
@@ -20,12 +17,12 @@ use super::{
20 17
21#[derive(Clone, Hash, PartialEq, Eq, Debug)] 18#[derive(Clone, Hash, PartialEq, Eq, Debug)]
22pub enum Submodule { 19pub enum Submodule {
23 Declaration(SmolStr), 20 Declaration(Name),
24 Definition(SmolStr, ModuleSource), 21 Definition(Name, ModuleSource),
25} 22}
26 23
27impl Submodule { 24impl 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
36pub(crate) fn modules<'a>( 33pub(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(
155fn resolve_submodule( 152fn 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.
17use std::{ 17use std::sync::Arc;
18 sync::Arc,
19};
20 18
21use rustc_hash::FxHashMap; 19use rustc_hash::FxHashMap;
22use ra_syntax::{ 20use ra_syntax::{
23 TextRange, 21 TextRange,
24 SmolStr, SyntaxKind::{self, *}, 22 SyntaxKind::{self, *},
25 ast::{self, AstNode} 23 ast::{self, AstNode}
26}; 24};
27use ra_db::SourceRootId; 25use 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)]
47pub struct ModuleScope { 46pub struct ModuleScope {
48 items: FxHashMap<SmolStr, Resolution>, 47 items: FxHashMap<Name, Resolution>,
49} 48}
50 49
51impl ModuleScope { 50impl 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)]
73struct ModuleItem { 72struct 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
261impl ModuleItem { 260impl 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..ca20f064f 100644
--- a/crates/ra_hir/src/module/nameres/tests.rs
+++ b/crates/ra_hir/src/module/nameres/tests.rs
@@ -2,8 +2,8 @@ use std::sync::Arc;
2 2
3use salsa::Database; 3use salsa::Database;
4use ra_db::{FilesDatabase, CrateGraph}; 4use ra_db::{FilesDatabase, CrateGraph};
5use ra_syntax::SmolStr;
6use relative_path::RelativePath; 5use relative_path::RelativePath;
6use test_utils::assert_eq_text;
7 7
8use crate::{ 8use crate::{
9 self as hir, 9 self as hir,
@@ -21,6 +21,35 @@ fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
21 (db.item_map(source_root).unwrap(), module_id) 21 (db.item_map(source_root).unwrap(), module_id)
22} 22}
23 23
24fn check_module_item_map(map: &hir::ItemMap, module_id: hir::ModuleId, expected: &str) {
25 let mut lines = map.per_module[&module_id]
26 .items
27 .iter()
28 .map(|(name, res)| format!("{}: {}", name, dump_resolution(res)))
29 .collect::<Vec<_>>();
30 lines.sort();
31 let actual = lines.join("\n");
32 let expected = expected
33 .trim()
34 .lines()
35 .map(|it| it.trim())
36 .collect::<Vec<_>>()
37 .join("\n");
38 assert_eq_text!(&actual, &expected);
39
40 fn dump_resolution(resolution: &hir::Resolution) -> &'static str {
41 match (
42 resolution.def_id.types.is_some(),
43 resolution.def_id.values.is_some(),
44 ) {
45 (true, true) => "t v",
46 (true, false) => "t",
47 (false, true) => "v",
48 (false, false) => "_",
49 }
50 }
51}
52
24#[test] 53#[test]
25fn item_map_smoke_test() { 54fn item_map_smoke_test() {
26 let (item_map, module_id) = item_map( 55 let (item_map, module_id) = item_map(
@@ -38,13 +67,18 @@ fn item_map_smoke_test() {
38 pub struct Baz; 67 pub struct Baz;
39 ", 68 ",
40 ); 69 );
41 let name = SmolStr::from("Baz"); 70 check_module_item_map(
42 let resolution = &item_map.per_module[&module_id].items[&name]; 71 &item_map,
43 assert!(resolution.def_id.take_types().is_some()); 72 module_id,
73 "
74 Baz: t v
75 foo: t
76 ",
77 );
44} 78}
45 79
46#[test] 80#[test]
47fn test_self() { 81fn item_map_using_self() {
48 let (item_map, module_id) = item_map( 82 let (item_map, module_id) = item_map(
49 " 83 "
50 //- /lib.rs 84 //- /lib.rs
@@ -57,9 +91,14 @@ fn test_self() {
57 pub struct Baz; 91 pub struct Baz;
58 ", 92 ",
59 ); 93 );
60 let name = SmolStr::from("Baz"); 94 check_module_item_map(
61 let resolution = &item_map.per_module[&module_id].items[&name]; 95 &item_map,
62 assert!(resolution.def_id.take_types().is_some()); 96 module_id,
97 "
98 Baz: t v
99 foo: t
100 ",
101 );
63} 102}
64 103
65#[test] 104#[test]
@@ -90,9 +129,14 @@ fn item_map_across_crates() {
90 let module_id = module.module_id; 129 let module_id = module.module_id;
91 let item_map = db.item_map(source_root).unwrap(); 130 let item_map = db.item_map(source_root).unwrap();
92 131
93 let name = SmolStr::from("Baz"); 132 check_module_item_map(
94 let resolution = &item_map.per_module[&module_id].items[&name]; 133 &item_map,
95 assert!(resolution.def_id.take_types().is_some()); 134 module_id,
135 "
136 Baz: t v
137 test_crate: t
138 ",
139 );
96} 140}
97 141
98#[test] 142#[test]