aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 14:17:18 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 14:17:18 +0000
commit2f07976cb51f7be216678f410175ba4c09bc7e71 (patch)
treec1f4896435ecfa48470e08787a24a5e3b73cc4fb /crates/ra_hir/src/nameres
parent562b448f9e49235fd47dabca1a0ce53da65dec6f (diff)
parent946b0ba02c2ab126b1b1d29027e60f21915d631e (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/nameres')
-rw-r--r--crates/ra_hir/src/nameres/tests.rs126
1 files changed, 125 insertions, 1 deletions
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index 82222d1ad..c511c40b2 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -1,7 +1,7 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use salsa::Database; 3use salsa::Database;
4use ra_db::{FilesDatabase, CrateGraph}; 4use ra_db::{FilesDatabase, CrateGraph, SourceRootId};
5use relative_path::RelativePath; 5use relative_path::RelativePath;
6use test_utils::assert_eq_text; 6use test_utils::assert_eq_text;
7 7
@@ -79,6 +79,35 @@ fn item_map_smoke_test() {
79} 79}
80 80
81#[test] 81#[test]
82fn use_trees() {
83 let (item_map, module_id) = item_map(
84 "
85 //- /lib.rs
86 mod foo;
87
88 use crate::foo::bar::{Baz, Quux};
89 <|>
90
91 //- /foo/mod.rs
92 pub mod bar;
93
94 //- /foo/bar.rs
95 pub struct Baz;
96 pub enum Quux {};
97 ",
98 );
99 check_module_item_map(
100 &item_map,
101 module_id,
102 "
103 Baz: t v
104 Quux: t
105 foo: t
106 ",
107 );
108}
109
110#[test]
82fn re_exports() { 111fn re_exports() {
83 let (item_map, module_id) = item_map( 112 let (item_map, module_id) = item_map(
84 " 113 "
@@ -199,6 +228,101 @@ fn item_map_across_crates() {
199} 228}
200 229
201#[test] 230#[test]
231fn import_across_source_roots() {
232 let (mut db, sr) = MockDatabase::with_files(
233 "
234 //- /lib.rs
235 pub mod a {
236 pub mod b {
237 pub struct C;
238 }
239 }
240 ",
241 );
242 let lib_id = sr.files[RelativePath::new("/lib.rs")];
243
244 let source_root = SourceRootId(1);
245
246 let (sr2, pos) = db.add_fixture(
247 source_root,
248 "
249 //- /main.rs
250 use test_crate::a::b::C;
251 ",
252 );
253 assert!(pos.is_none());
254
255 let main_id = sr2.files[RelativePath::new("/main.rs")];
256
257 eprintln!("lib = {:?}, main = {:?}", lib_id, main_id);
258
259 let mut crate_graph = CrateGraph::default();
260 let main_crate = crate_graph.add_crate_root(main_id);
261 let lib_crate = crate_graph.add_crate_root(lib_id);
262 crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate);
263
264 db.set_crate_graph(crate_graph);
265
266 let module = crate::source_binder::module_from_file_id(&db, main_id)
267 .unwrap()
268 .unwrap();
269 let module_id = module.def_id.loc(&db).module_id;
270 let item_map = db.item_map(source_root).unwrap();
271
272 check_module_item_map(
273 &item_map,
274 module_id,
275 "
276 C: t v
277 test_crate: t
278 ",
279 );
280}
281
282#[test]
283fn reexport_across_crates() {
284 let (mut db, sr) = MockDatabase::with_files(
285 "
286 //- /main.rs
287 use test_crate::Baz;
288
289 //- /lib.rs
290 pub use foo::Baz;
291
292 mod foo;
293
294 //- /foo.rs
295 pub struct Baz;
296 ",
297 );
298 let main_id = sr.files[RelativePath::new("/main.rs")];
299 let lib_id = sr.files[RelativePath::new("/lib.rs")];
300
301 let mut crate_graph = CrateGraph::default();
302 let main_crate = crate_graph.add_crate_root(main_id);
303 let lib_crate = crate_graph.add_crate_root(lib_id);
304 crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate);
305
306 db.set_crate_graph(crate_graph);
307
308 let source_root = db.file_source_root(main_id);
309 let module = crate::source_binder::module_from_file_id(&db, main_id)
310 .unwrap()
311 .unwrap();
312 let module_id = module.def_id.loc(&db).module_id;
313 let item_map = db.item_map(source_root).unwrap();
314
315 check_module_item_map(
316 &item_map,
317 module_id,
318 "
319 Baz: t v
320 test_crate: t
321 ",
322 );
323}
324
325#[test]
202fn typing_inside_a_function_should_not_invalidate_item_map() { 326fn typing_inside_a_function_should_not_invalidate_item_map() {
203 let (mut db, pos) = MockDatabase::with_position( 327 let (mut db, pos) = MockDatabase::with_position(
204 " 328 "