From 1c582be63b35c6602638023d4bd0bc426b0ca744 Mon Sep 17 00:00:00 2001 From: Alexander Andreev Date: Sun, 7 Jul 2019 16:06:54 +0300 Subject: Moved module resolution test in mods.rs --- crates/ra_hir/src/nameres.rs | 2 +- crates/ra_hir/src/nameres/tests.rs | 192 +----------------------------- crates/ra_hir/src/nameres/tests/mods.rs | 192 ++++++++++++++++++++++++++++++ crates/ra_hir/src/ty/method_resolution.rs | 2 +- 4 files changed, 195 insertions(+), 193 deletions(-) create mode 100644 crates/ra_hir/src/nameres/tests/mods.rs (limited to 'crates') diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index cf9ff0636..c84d2eada 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -1,6 +1,6 @@ /// This module implements import-resolution/macro expansion algorithm. /// -/// The result of this module is `CrateDefMap`: a datastructure which contains: +/// The result of this module is `CrateDefMap`: a data structure which contains: /// /// * a tree of modules for the crate /// * for each module, a set of items visible in the module (directly declared diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 5e8ea6780..8b0887fb5 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -2,6 +2,7 @@ mod macros; mod globs; mod incremental; mod primitives; +mod mods; use std::sync::Arc; @@ -312,178 +313,6 @@ fn edition_2015_imports() { "###); } -#[test] -fn module_resolution_works_for_non_standard_filenames() { - let map = def_map_with_crate_graph( - " - //- /my_library.rs - mod foo; - use self::foo::Bar; - - //- /foo/mod.rs - pub struct Bar; - ", - crate_graph! { - "my_library": ("/my_library.rs", []), - }, - ); - - assert_snapshot_matches!(map, @r###" - ⋮crate - ⋮Bar: t v - ⋮foo: t - ⋮ - ⋮crate::foo - ⋮Bar: t v - "###); -} - -#[test] -fn module_resolution_works_for_raw_modules() { - let map = def_map_with_crate_graph( - " - //- /library.rs - mod r#async; - use self::r#async::Bar; - - //- /async.rs - pub struct Bar; - ", - crate_graph! { - "library": ("/library.rs", []), - }, - ); - - assert_snapshot_matches!(map, @r###" - ⋮crate - ⋮Bar: t v - ⋮async: t - ⋮ - ⋮crate::async - ⋮Bar: t v - "###); -} - -#[test] -fn module_resolution_decl_path() { - let map = def_map_with_crate_graph( - " - //- /library.rs - #[path = \"bar/baz/foo.rs\"] - mod foo; - use self::foo::Bar; - - //- /bar/baz/foo.rs - pub struct Bar; - ", - crate_graph! { - "library": ("/library.rs", []), - }, - ); - - assert_snapshot_matches!(map, @r###" - ⋮crate - ⋮Bar: t v - ⋮foo: t - ⋮ - ⋮crate::foo - ⋮Bar: t v - "###); -} - -#[test] -fn module_resolution_module_with_path_in_mod_rs() { - let map = def_map_with_crate_graph( - " - //- /main.rs - mod foo; - - //- /foo/mod.rs - #[path = \"baz.rs\"] - pub mod bar; - - use self::bar::Baz; - - //- /foo/baz.rs - pub struct Baz; - ", - crate_graph! { - "main": ("/main.rs", []), - }, - ); - - assert_snapshot_matches!(map, @r###" - ⋮crate - ⋮foo: t - ⋮ - ⋮crate::foo - ⋮Baz: t v - ⋮bar: t - ⋮ - ⋮crate::foo::bar - ⋮Baz: t v - "###); -} - -#[test] -fn module_resolution_module_with_path_non_crate_root() { - let map = def_map_with_crate_graph( - " - //- /main.rs - mod foo; - - //- /foo.rs - #[path = \"baz.rs\"] - pub mod bar; - - use self::bar::Baz; - - //- /baz.rs - pub struct Baz; - ", - crate_graph! { - "main": ("/main.rs", []), - }, - ); - - assert_snapshot_matches!(map, @r###" - ⋮crate - ⋮foo: t - ⋮ - ⋮crate::foo - ⋮Baz: t v - ⋮bar: t - ⋮ - ⋮crate::foo::bar - ⋮Baz: t v - "###); -} - -#[test] -fn name_res_works_for_broken_modules() { - covers!(name_res_works_for_broken_modules); - let map = def_map( - " - //- /lib.rs - mod foo // no `;`, no body - - use self::foo::Baz; - - //- /foo/mod.rs - pub mod bar; - - pub use self::bar::Baz; - - //- /foo/bar.rs - pub struct Baz; - ", - ); - assert_snapshot_matches!(map, @r###" - ⋮crate - ⋮Baz: _ - "###); -} - #[test] fn item_map_using_self() { let map = def_map( @@ -676,22 +505,3 @@ fn values_dont_shadow_extern_crates() { ⋮foo: v "###); } - -#[test] -fn unresolved_module_diagnostics() { - let diagnostics = MockDatabase::with_files( - r" - //- /lib.rs - mod foo; - mod bar; - mod baz {} - //- /foo.rs - ", - ) - .diagnostics(); - - assert_snapshot_matches!(diagnostics, @r###" -"mod bar;": unresolved module -"### - ); -} diff --git a/crates/ra_hir/src/nameres/tests/mods.rs b/crates/ra_hir/src/nameres/tests/mods.rs new file mode 100644 index 000000000..7c8c832fc --- /dev/null +++ b/crates/ra_hir/src/nameres/tests/mods.rs @@ -0,0 +1,192 @@ +use super::*; + +#[test] +fn name_res_works_for_broken_modules() { + covers!(name_res_works_for_broken_modules); + let map = def_map( + " + //- /lib.rs + mod foo // no `;`, no body + + use self::foo::Baz; + + //- /foo/mod.rs + pub mod bar; + + pub use self::bar::Baz; + + //- /foo/bar.rs + pub struct Baz; + ", + ); + assert_snapshot_matches!(map, @r###" + ⋮crate + ⋮Baz: _ + "###); +} + +#[test] +fn module_resolution_works_for_non_standard_filenames() { + let map = def_map_with_crate_graph( + " + //- /my_library.rs + mod foo; + use self::foo::Bar; + + //- /foo/mod.rs + pub struct Bar; + ", + crate_graph! { + "my_library": ("/my_library.rs", []), + }, + ); + + assert_snapshot_matches!(map, @r###" + ⋮crate + ⋮Bar: t v + ⋮foo: t + ⋮ + ⋮crate::foo + ⋮Bar: t v + "###); +} + +#[test] +fn module_resolution_works_for_raw_modules() { + let map = def_map_with_crate_graph( + " + //- /library.rs + mod r#async; + use self::r#async::Bar; + + //- /async.rs + pub struct Bar; + ", + crate_graph! { + "library": ("/library.rs", []), + }, + ); + + assert_snapshot_matches!(map, @r###" + ⋮crate + ⋮Bar: t v + ⋮async: t + ⋮ + ⋮crate::async + ⋮Bar: t v + "###); +} + +#[test] +fn module_resolution_decl_path() { + let map = def_map_with_crate_graph( + " + //- /library.rs + #[path = \"bar/baz/foo.rs\"] + mod foo; + use self::foo::Bar; + + //- /bar/baz/foo.rs + pub struct Bar; + ", + crate_graph! { + "library": ("/library.rs", []), + }, + ); + + assert_snapshot_matches!(map, @r###" + ⋮crate + ⋮Bar: t v + ⋮foo: t + ⋮ + ⋮crate::foo + ⋮Bar: t v + "###); +} + +#[test] +fn module_resolution_module_with_path_in_mod_rs() { + let map = def_map_with_crate_graph( + " + //- /main.rs + mod foo; + + //- /foo/mod.rs + #[path = \"baz.rs\"] + pub mod bar; + + use self::bar::Baz; + + //- /foo/baz.rs + pub struct Baz; + ", + crate_graph! { + "main": ("/main.rs", []), + }, + ); + + assert_snapshot_matches!(map, @r###" + ⋮crate + ⋮foo: t + ⋮ + ⋮crate::foo + ⋮Baz: t v + ⋮bar: t + ⋮ + ⋮crate::foo::bar + ⋮Baz: t v + "###); +} + +#[test] +fn module_resolution_module_with_path_non_crate_root() { + let map = def_map_with_crate_graph( + " + //- /main.rs + mod foo; + + //- /foo.rs + #[path = \"baz.rs\"] + pub mod bar; + + use self::bar::Baz; + + //- /baz.rs + pub struct Baz; + ", + crate_graph! { + "main": ("/main.rs", []), + }, + ); + + assert_snapshot_matches!(map, @r###" + ⋮crate + ⋮foo: t + ⋮ + ⋮crate::foo + ⋮Baz: t v + ⋮bar: t + ⋮ + ⋮crate::foo::bar + ⋮Baz: t v + "###); +} + +#[test] +fn unresolved_module_diagnostics() { + let diagnostics = MockDatabase::with_files( + r" + //- /lib.rs + mod foo; + mod bar; + mod baz {} + //- /foo.rs + ", + ) + .diagnostics(); + + assert_snapshot_matches!(diagnostics, @r###" +"mod bar;": unresolved module +"### + ); +} diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 10a6e0b10..76ace66ea 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -116,7 +116,7 @@ impl CrateImplBlocks { fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option> { // Types like slice can have inherent impls in several crates, (core and alloc). - // The correspoinding impls are marked with lang items, so we can use them to find the required crates. + // The corresponding impls are marked with lang items, so we can use them to find the required crates. macro_rules! lang_item_crate { ($db:expr, $cur_crate:expr, $($name:expr),+ $(,)?) => {{ let mut v = ArrayVec::<[Crate; 2]>::new(); -- cgit v1.2.3