diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-08 14:17:18 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-08 14:17:18 +0000 |
commit | 2f07976cb51f7be216678f410175ba4c09bc7e71 (patch) | |
tree | c1f4896435ecfa48470e08787a24a5e3b73cc4fb /crates/ra_hir/src/mock.rs | |
parent | 562b448f9e49235fd47dabca1a0ce53da65dec6f (diff) | |
parent | 946b0ba02c2ab126b1b1d29027e60f21915d631e (diff) |
Merge #460
460: Name resolution fixes r=flodiebold a=flodiebold
Found two problems:
- use tree desugaring lost the prefix if the path had just one segment (e.g. in `use foo::{bar, baz}`)
- when resolving imports across source roots, it actually used the name of the segment from the other source root... so e.g. in `use ra_syntax::foo` it'd map `ra_syntax` to the import instead of `foo` :smile:
Both of these are one-line fixes, most of this is making it possible to write tests with multiple source roots.
I also left in debug logs for the name resolution, in case it turns out there's still more to fix ;)
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/mock.rs')
-rw-r--r-- | crates/ra_hir/src/mock.rs | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 8d176662c..c9af38009 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs | |||
@@ -15,6 +15,7 @@ pub(crate) struct MockDatabase { | |||
15 | events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, | 15 | events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, |
16 | runtime: salsa::Runtime<MockDatabase>, | 16 | runtime: salsa::Runtime<MockDatabase>, |
17 | id_maps: Arc<IdMaps>, | 17 | id_maps: Arc<IdMaps>, |
18 | file_counter: u32, | ||
18 | } | 19 | } |
19 | 20 | ||
20 | impl MockDatabase { | 21 | impl MockDatabase { |
@@ -27,7 +28,7 @@ impl MockDatabase { | |||
27 | pub(crate) fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) { | 28 | pub(crate) fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) { |
28 | let mut db = MockDatabase::default(); | 29 | let mut db = MockDatabase::default(); |
29 | let mut source_root = SourceRoot::default(); | 30 | let mut source_root = SourceRoot::default(); |
30 | let file_id = db.add_file(&mut source_root, "/main.rs", text); | 31 | let file_id = db.add_file(WORKSPACE, &mut source_root, "/main.rs", text); |
31 | db.query_mut(ra_db::SourceRootQuery) | 32 | db.query_mut(ra_db::SourceRootQuery) |
32 | .set(WORKSPACE, Arc::new(source_root.clone())); | 33 | .set(WORKSPACE, Arc::new(source_root.clone())); |
33 | 34 | ||
@@ -51,6 +52,16 @@ impl MockDatabase { | |||
51 | fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) { | 52 | fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) { |
52 | let mut db = MockDatabase::default(); | 53 | let mut db = MockDatabase::default(); |
53 | 54 | ||
55 | let (source_root, pos) = db.add_fixture(WORKSPACE, fixture); | ||
56 | |||
57 | (db, source_root, pos) | ||
58 | } | ||
59 | |||
60 | pub fn add_fixture( | ||
61 | &mut self, | ||
62 | source_root_id: SourceRootId, | ||
63 | fixture: &str, | ||
64 | ) -> (SourceRoot, Option<FilePosition>) { | ||
54 | let mut position = None; | 65 | let mut position = None; |
55 | let mut source_root = SourceRoot::default(); | 66 | let mut source_root = SourceRoot::default(); |
56 | for entry in parse_fixture(fixture) { | 67 | for entry in parse_fixture(fixture) { |
@@ -59,39 +70,51 @@ impl MockDatabase { | |||
59 | position.is_none(), | 70 | position.is_none(), |
60 | "only one marker (<|>) per fixture is allowed" | 71 | "only one marker (<|>) per fixture is allowed" |
61 | ); | 72 | ); |
62 | position = | 73 | position = Some(self.add_file_with_position( |
63 | Some(db.add_file_with_position(&mut source_root, &entry.meta, &entry.text)); | 74 | source_root_id, |
75 | &mut source_root, | ||
76 | &entry.meta, | ||
77 | &entry.text, | ||
78 | )); | ||
64 | } else { | 79 | } else { |
65 | db.add_file(&mut source_root, &entry.meta, &entry.text); | 80 | self.add_file(source_root_id, &mut source_root, &entry.meta, &entry.text); |
66 | } | 81 | } |
67 | } | 82 | } |
68 | db.query_mut(ra_db::SourceRootQuery) | 83 | self.query_mut(ra_db::SourceRootQuery) |
69 | .set(WORKSPACE, Arc::new(source_root.clone())); | 84 | .set(source_root_id, Arc::new(source_root.clone())); |
70 | (db, source_root, position) | 85 | (source_root, position) |
71 | } | 86 | } |
72 | 87 | ||
73 | fn add_file(&mut self, source_root: &mut SourceRoot, path: &str, text: &str) -> FileId { | 88 | fn add_file( |
89 | &mut self, | ||
90 | source_root_id: SourceRootId, | ||
91 | source_root: &mut SourceRoot, | ||
92 | path: &str, | ||
93 | text: &str, | ||
94 | ) -> FileId { | ||
74 | assert!(path.starts_with('/')); | 95 | assert!(path.starts_with('/')); |
75 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); | 96 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); |
76 | let file_id = FileId(source_root.files.len() as u32); | 97 | let file_id = FileId(self.file_counter); |
98 | self.file_counter += 1; | ||
77 | let text = Arc::new(text.to_string()); | 99 | let text = Arc::new(text.to_string()); |
78 | self.query_mut(ra_db::FileTextQuery).set(file_id, text); | 100 | self.query_mut(ra_db::FileTextQuery).set(file_id, text); |
79 | self.query_mut(ra_db::FileRelativePathQuery) | 101 | self.query_mut(ra_db::FileRelativePathQuery) |
80 | .set(file_id, path.clone()); | 102 | .set(file_id, path.clone()); |
81 | self.query_mut(ra_db::FileSourceRootQuery) | 103 | self.query_mut(ra_db::FileSourceRootQuery) |
82 | .set(file_id, WORKSPACE); | 104 | .set(file_id, source_root_id); |
83 | source_root.files.insert(path, file_id); | 105 | source_root.files.insert(path, file_id); |
84 | file_id | 106 | file_id |
85 | } | 107 | } |
86 | 108 | ||
87 | fn add_file_with_position( | 109 | fn add_file_with_position( |
88 | &mut self, | 110 | &mut self, |
111 | source_root_id: SourceRootId, | ||
89 | source_root: &mut SourceRoot, | 112 | source_root: &mut SourceRoot, |
90 | path: &str, | 113 | path: &str, |
91 | text: &str, | 114 | text: &str, |
92 | ) -> FilePosition { | 115 | ) -> FilePosition { |
93 | let (offset, text) = extract_offset(text); | 116 | let (offset, text) = extract_offset(text); |
94 | let file_id = self.add_file(source_root, path, &text); | 117 | let file_id = self.add_file(source_root_id, source_root, path, &text); |
95 | FilePosition { file_id, offset } | 118 | FilePosition { file_id, offset } |
96 | } | 119 | } |
97 | } | 120 | } |
@@ -121,6 +144,7 @@ impl Default for MockDatabase { | |||
121 | events: Default::default(), | 144 | events: Default::default(), |
122 | runtime: salsa::Runtime::default(), | 145 | runtime: salsa::Runtime::default(), |
123 | id_maps: Default::default(), | 146 | id_maps: Default::default(), |
147 | file_counter: 0, | ||
124 | }; | 148 | }; |
125 | db.query_mut(ra_db::CrateGraphQuery) | 149 | db.query_mut(ra_db::CrateGraphQuery) |
126 | .set((), Default::default()); | 150 | .set((), Default::default()); |
@@ -138,6 +162,7 @@ impl salsa::ParallelDatabase for MockDatabase { | |||
138 | events: Default::default(), | 162 | events: Default::default(), |
139 | runtime: self.runtime.snapshot(self), | 163 | runtime: self.runtime.snapshot(self), |
140 | id_maps: self.id_maps.clone(), | 164 | id_maps: self.id_maps.clone(), |
165 | file_counter: self.file_counter, | ||
141 | }) | 166 | }) |
142 | } | 167 | } |
143 | } | 168 | } |