From 683e5e64f42b6c74596015c5d19e2eab186fe68d Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sun, 27 Jan 2019 14:51:26 +0100 Subject: [PATCH 1/7] Test non standard crate root --- crates/ra_hir/src/mock.rs | 16 ++++++++----- crates/ra_hir/src/nameres/tests.rs | 38 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 7da15eca01..2bba11e42e 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -2,7 +2,8 @@ use std::{sync::Arc, panic}; use parking_lot::Mutex; use ra_db::{ - CheckCanceled, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa, + mock::FileMap, CheckCanceled, CrateGraph, FileId, FilePosition, SourceDatabase, + SourceRoot, SourceRootId, salsa }; use relative_path::RelativePathBuf; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; @@ -17,7 +18,7 @@ pub(crate) struct MockDatabase { events: Mutex>>>, runtime: salsa::Runtime, interner: Arc, - file_counter: u32, + file_map: Arc, } impl panic::RefUnwindSafe for MockDatabase {} @@ -43,6 +44,10 @@ impl MockDatabase { (db, position) } + pub(crate) fn file_id(&self, file: &str) -> FileId { + self.file_map.file_id(file) + } + fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option) { let mut db = MockDatabase::default(); @@ -89,8 +94,7 @@ impl MockDatabase { let is_crate_root = path == "/lib.rs" || path == "/main.rs"; let path = RelativePathBuf::from_path(&path[1..]).unwrap(); - let file_id = FileId(self.file_counter); - self.file_counter += 1; + let file_id = Arc::make_mut(&mut self.file_map).add(path.clone()); let text = Arc::new(text.to_string()); self.set_file_text(file_id, text); self.set_file_relative_path(file_id, path.clone()); @@ -137,7 +141,7 @@ impl Default for MockDatabase { events: Default::default(), runtime: salsa::Runtime::default(), interner: Default::default(), - file_counter: 0, + file_map: Default::default(), }; db.set_crate_graph(Default::default()); db @@ -150,7 +154,7 @@ impl salsa::ParallelDatabase for MockDatabase { events: Default::default(), runtime: self.runtime.snapshot(self), interner: Arc::clone(&self.interner), - file_counter: self.file_counter, + file_map: Arc::clone(&self.file_map), }) } } diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index e72781f512..5b161cf497 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -19,6 +19,19 @@ fn item_map(fixture: &str) -> (Arc, ModuleId) { (db.item_map(krate.crate_id), module_id) } +fn item_map_custom_crate_root(fixture: &str, root: &str) -> (Arc, ModuleId) { + let (mut db, pos) = MockDatabase::with_position(fixture); + + let mut crate_graph = CrateGraph::default(); + crate_graph.add_crate_root(db.file_id(root)); + db.set_crate_graph(Arc::new(crate_graph)); + + let module = crate::source_binder::module_from_position(&db, dbg!(pos)).unwrap(); + let krate = module.krate(&db).unwrap(); + let module_id = module.module_id; + (db.item_map(krate.crate_id), module_id) +} + fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { let mut lines = map[module_id] .items @@ -133,6 +146,31 @@ fn re_exports() { ); } +#[test] +fn module_resolution_works_for_non_standard_filenames() { + let (item_map, module_id) = item_map_custom_crate_root( + " + //- /my_library.rs + mod foo; + + use self::foo::Bar; + <|> + + //- /foo/mod.rs + pub struct Bar; + ", + "/my_library.rs", + ); + check_module_item_map( + &item_map, + module_id, + " + Bar: t v + foo: t + ", + ); +} + #[test] fn name_res_works_for_broken_modules() { covers!(name_res_works_for_broken_modules); From c5e6db16b1def5d481358489a3c6d9a6fd5cbdf4 Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sat, 26 Jan 2019 13:12:55 +0100 Subject: [PATCH 2/7] fix #668 --- crates/ra_hir/src/module_tree.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs index d1dc3fa4b7..7caebab0df 100644 --- a/crates/ra_hir/src/module_tree.rs +++ b/crates/ra_hir/src/module_tree.rs @@ -172,6 +172,7 @@ impl ModuleTree { file_id: HirFileId, decl_id: Option, ) -> ModuleId { + let is_root = parent.is_none(); let id = self.alloc_mod(ModuleData { file_id, decl_id, @@ -295,6 +296,7 @@ fn resolve_submodule( db: &impl HirDatabase, file_id: HirFileId, name: &Name, + is_root: bool, ) -> (Vec, Option) { // FIXME: handle submodules of inline modules properly let file_id = file_id.original_file(db); @@ -303,7 +305,7 @@ fn resolve_submodule( let root = RelativePathBuf::default(); let dir_path = path.parent().unwrap_or(&root); let mod_name = path.file_stem().unwrap_or("unknown"); - let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; + let is_dir_owner = is_root || mod_name == "mod"; let file_mod = dir_path.join(format!("{}.rs", name)); let dir_mod = dir_path.join(format!("{}/mod.rs", name)); From 5052bf988918f23801036b2cbc3d1505b5321ee5 Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sat, 26 Jan 2019 16:59:53 +0100 Subject: [PATCH 3/7] Update test to use correct paths --- crates/ra_ide_api/tests/test/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/ra_ide_api/tests/test/main.rs b/crates/ra_ide_api/tests/test/main.rs index 7560751909..c2cb38f7e9 100644 --- a/crates/ra_ide_api/tests/test/main.rs +++ b/crates/ra_ide_api/tests/test/main.rs @@ -1,9 +1,9 @@ +use insta::assert_debug_snapshot_matches; use ra_ide_api::{ - AnalysisChange, - CrateGraph, FileId, mock_analysis::{MockAnalysis, single_file, single_file_with_position}, Query, + mock_analysis::{single_file, single_file_with_position, MockAnalysis}, + AnalysisChange, CrateGraph, FileId, Query, }; use ra_syntax::TextRange; -use insta::assert_debug_snapshot_matches; #[test] fn test_unresolved_module_diagnostic() { @@ -26,12 +26,12 @@ fn test_resolve_crate_root() { " //- /bar.rs mod foo; - //- /bar/foo.rs - // emtpy <|> + //- /foo.rs + // empty <|> ", ); let root_file = mock.id_of("/bar.rs"); - let mod_file = mock.id_of("/bar/foo.rs"); + let mod_file = mock.id_of("/foo.rs"); let mut host = mock.analysis_host(); assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); From e2e1f4cecaa2d373f58260b504fd014efe49cf89 Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sun, 27 Jan 2019 14:49:02 +0100 Subject: [PATCH 4/7] Typos --- crates/ra_syntax/src/algo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 45747e21d4..99b0983b0d 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -17,14 +17,14 @@ pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset< } /// Finds a node of specific Ast type at offset. Note that this is slightly -/// impercise: if the cursor is strictly betwen two nodes of the desired type, +/// imprecise: if the cursor is strictly between two nodes of the desired type, /// as in /// /// ```no-run /// struct Foo {}|struct Bar; /// ``` /// -/// then the left node will be silently prefered. +/// then the left node will be silently preferred. pub fn find_node_at_offset(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> { find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) } From 08e12f974cb1d801920368420174ad4db0eb0df0 Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sun, 27 Jan 2019 15:29:02 +0100 Subject: [PATCH 5/7] Fix bad rebase and rustfmt --- crates/ra_hir/src/module_tree.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs index 7caebab0df..893c375b55 100644 --- a/crates/ra_hir/src/module_tree.rs +++ b/crates/ra_hir/src/module_tree.rs @@ -192,7 +192,7 @@ impl ModuleTree { }); let (points_to, problem) = if sub.is_declaration { - let (points_to, problem) = resolve_submodule(db, file_id, &sub.name); + let (points_to, problem) = resolve_submodule(db, file_id, &sub.name, is_root); let points_to = points_to .into_iter() .map(|file_id| { From b149882d198818d6302c2a6318d68c4e0b4380ff Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sun, 27 Jan 2019 15:44:59 +0100 Subject: [PATCH 6/7] Remove dbg!(...) --- crates/ra_hir/src/nameres/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 5b161cf497..971f716371 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -26,7 +26,7 @@ fn item_map_custom_crate_root(fixture: &str, root: &str) -> (Arc, Modul crate_graph.add_crate_root(db.file_id(root)); db.set_crate_graph(Arc::new(crate_graph)); - let module = crate::source_binder::module_from_position(&db, dbg!(pos)).unwrap(); + let module = crate::source_binder::module_from_position(&db, pos).unwrap(); let krate = module.krate(&db).unwrap(); let module_id = module.module_id; (db.item_map(krate.crate_id), module_id) From b775fa285c985821f38f09c25507d80ee793ecfd Mon Sep 17 00:00:00 2001 From: Erlend Tobiassen Date: Sun, 27 Jan 2019 16:32:31 +0100 Subject: [PATCH 7/7] Revert parts of "Test non standard crate root" Prefer cursor position over file_map --- crates/ra_hir/src/mock.rs | 16 ++++++---------- crates/ra_hir/src/nameres/tests.rs | 8 +++----- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 2bba11e42e..7da15eca01 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -2,8 +2,7 @@ use std::{sync::Arc, panic}; use parking_lot::Mutex; use ra_db::{ - mock::FileMap, CheckCanceled, CrateGraph, FileId, FilePosition, SourceDatabase, - SourceRoot, SourceRootId, salsa + CheckCanceled, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa, }; use relative_path::RelativePathBuf; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; @@ -18,7 +17,7 @@ pub(crate) struct MockDatabase { events: Mutex>>>, runtime: salsa::Runtime, interner: Arc, - file_map: Arc, + file_counter: u32, } impl panic::RefUnwindSafe for MockDatabase {} @@ -44,10 +43,6 @@ impl MockDatabase { (db, position) } - pub(crate) fn file_id(&self, file: &str) -> FileId { - self.file_map.file_id(file) - } - fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option) { let mut db = MockDatabase::default(); @@ -94,7 +89,8 @@ impl MockDatabase { let is_crate_root = path == "/lib.rs" || path == "/main.rs"; let path = RelativePathBuf::from_path(&path[1..]).unwrap(); - let file_id = Arc::make_mut(&mut self.file_map).add(path.clone()); + let file_id = FileId(self.file_counter); + self.file_counter += 1; let text = Arc::new(text.to_string()); self.set_file_text(file_id, text); self.set_file_relative_path(file_id, path.clone()); @@ -141,7 +137,7 @@ impl Default for MockDatabase { events: Default::default(), runtime: salsa::Runtime::default(), interner: Default::default(), - file_map: Default::default(), + file_counter: 0, }; db.set_crate_graph(Default::default()); db @@ -154,7 +150,7 @@ impl salsa::ParallelDatabase for MockDatabase { events: Default::default(), runtime: self.runtime.snapshot(self), interner: Arc::clone(&self.interner), - file_map: Arc::clone(&self.file_map), + file_counter: self.file_counter, }) } } diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 971f716371..3d420467c1 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -19,11 +19,12 @@ fn item_map(fixture: &str) -> (Arc, ModuleId) { (db.item_map(krate.crate_id), module_id) } -fn item_map_custom_crate_root(fixture: &str, root: &str) -> (Arc, ModuleId) { +/// Sets the crate root to the file of the cursor marker +fn item_map_custom_crate_root(fixture: &str) -> (Arc, ModuleId) { let (mut db, pos) = MockDatabase::with_position(fixture); let mut crate_graph = CrateGraph::default(); - crate_graph.add_crate_root(db.file_id(root)); + crate_graph.add_crate_root(pos.file_id); db.set_crate_graph(Arc::new(crate_graph)); let module = crate::source_binder::module_from_position(&db, pos).unwrap(); @@ -152,14 +153,11 @@ fn module_resolution_works_for_non_standard_filenames() { " //- /my_library.rs mod foo; - use self::foo::Bar; <|> - //- /foo/mod.rs pub struct Bar; ", - "/my_library.rs", ); check_module_item_map( &item_map,