From ce0d4a3b1deaca318a1ed2ff83cca652ef8e8c82 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Oct 2019 14:45:05 +0300 Subject: Refactor and fix some more edge cases around name resolution --- crates/ra_hir/src/nameres/tests/macros.rs | 29 +++++-- crates/ra_hir/src/nameres/tests/mod_resolution.rs | 100 ++++++++++++++++++++-- 2 files changed, 114 insertions(+), 15 deletions(-) (limited to 'crates/ra_hir/src/nameres/tests') diff --git a/crates/ra_hir/src/nameres/tests/macros.rs b/crates/ra_hir/src/nameres/tests/macros.rs index e4b408394..4f52ad2c5 100644 --- a/crates/ra_hir/src/nameres/tests/macros.rs +++ b/crates/ra_hir/src/nameres/tests/macros.rs @@ -38,21 +38,34 @@ fn macro_rules_can_define_modules() { } m!(n1); + mod m { + m!(n3) + } + //- /n1.rs m!(n2) //- /n1/n2.rs struct X; + //- /m/n3.rs + struct Y; ", ); assert_snapshot!(map, @r###" - ⋮crate - ⋮n1: t - ⋮ - ⋮crate::n1 - ⋮n2: t - ⋮ - ⋮crate::n1::n2 - ⋮X: t v + crate + m: t + n1: t + + crate::m + n3: t + + crate::m::n3 + Y: t v + + crate::n1 + n2: t + + crate::n1::n2 + X: t v "###); } diff --git a/crates/ra_hir/src/nameres/tests/mod_resolution.rs b/crates/ra_hir/src/nameres/tests/mod_resolution.rs index e3e6f1e95..755f5723b 100644 --- a/crates/ra_hir/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir/src/nameres/tests/mod_resolution.rs @@ -25,6 +25,33 @@ fn name_res_works_for_broken_modules() { "###); } +#[test] +fn nested_module_resolution() { + let map = def_map( + " + //- /lib.rs + mod n1; + + //- /n1.rs + mod n2; + + //- /n1/n2.rs + struct X; + ", + ); + + assert_snapshot!(map, @r###" + ⋮crate + ⋮n1: t + ⋮ + ⋮crate::n1 + ⋮n2: t + ⋮ + ⋮crate::n1::n2 + ⋮X: t v + "###); +} + #[test] fn module_resolution_works_for_non_standard_filenames() { let map = def_map_with_crate_graph( @@ -467,7 +494,7 @@ fn module_resolution_decl_inside_inline_module_empty_path() { mod bar; } - //- /foo/users.rs + //- /users.rs pub struct Baz; "###, crate_graph! { @@ -492,7 +519,7 @@ fn module_resolution_decl_empty_path() { let map = def_map_with_crate_graph( r###" //- /main.rs - #[path = ""] + #[path = ""] // Should try to read `/` (a directory) mod foo; //- /foo.rs @@ -505,10 +532,6 @@ fn module_resolution_decl_empty_path() { assert_snapshot!(map, @r###" ⋮crate - ⋮foo: t - ⋮ - ⋮crate::foo - ⋮Baz: t v "###); } @@ -626,7 +649,7 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root() { } use self::bar::baz::Baz; - //- /bar/qwe.rs + //- /foo/bar/qwe.rs pub struct Baz; "###, crate_graph! { @@ -737,3 +760,66 @@ fn module_resolution_decl_inside_module_in_non_crate_root_2() { ⋮Baz: t v "###); } + +#[test] +fn nested_out_of_line_module() { + let map = def_map( + r###" + //- /lib.rs + mod a { + mod b { + mod c; + } + } + + //- /a/b/c.rs + struct X; + "###, + ); + + assert_snapshot!(map, @r###" + crate + a: t + + crate::a + b: t + + crate::a::b + c: t + + crate::a::b::c + X: t v + "###); +} + +#[test] +fn nested_out_of_line_module_with_path() { + let map = def_map( + r###" + //- /lib.rs + mod a { + #[path = "d/e"] + mod b { + mod c; + } + } + + //- /a/d/e/c.rs + struct X; + "###, + ); + + assert_snapshot!(map, @r###" + crate + a: t + + crate::a + b: t + + crate::a::b + c: t + + crate::a::b::c + X: t v + "###); +} -- cgit v1.2.3 From 51402832774d876cdae8ac7042717e61d466b527 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Oct 2019 14:51:35 +0300 Subject: simplify some tests --- crates/ra_hir/src/nameres/tests/mod_resolution.rs | 114 +++++----------------- 1 file changed, 24 insertions(+), 90 deletions(-) (limited to 'crates/ra_hir/src/nameres/tests') diff --git a/crates/ra_hir/src/nameres/tests/mod_resolution.rs b/crates/ra_hir/src/nameres/tests/mod_resolution.rs index 755f5723b..f569aacdc 100644 --- a/crates/ra_hir/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir/src/nameres/tests/mod_resolution.rs @@ -80,18 +80,15 @@ fn module_resolution_works_for_non_standard_filenames() { #[test] fn module_resolution_works_for_raw_modules() { - let map = def_map_with_crate_graph( + let map = def_map( " - //- /library.rs + //- /lib.rs mod r#async; use self::r#async::Bar; //- /async.rs pub struct Bar; ", - crate_graph! { - "library": ("/library.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -106,9 +103,9 @@ fn module_resolution_works_for_raw_modules() { #[test] fn module_resolution_decl_path() { - let map = def_map_with_crate_graph( + let map = def_map( r###" - //- /library.rs + //- /lib.rs #[path = "bar/baz/foo.rs"] mod foo; use self::foo::Bar; @@ -116,9 +113,6 @@ fn module_resolution_decl_path() { //- /bar/baz/foo.rs pub struct Bar; "###, - crate_graph! { - "library": ("/library.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -133,7 +127,7 @@ fn module_resolution_decl_path() { #[test] fn module_resolution_module_with_path_in_mod_rs() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo; @@ -147,9 +141,6 @@ fn module_resolution_module_with_path_in_mod_rs() { //- /foo/baz.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -167,7 +158,7 @@ fn module_resolution_module_with_path_in_mod_rs() { #[test] fn module_resolution_module_with_path_non_crate_root() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo; @@ -181,9 +172,6 @@ fn module_resolution_module_with_path_non_crate_root() { //- /baz.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -201,7 +189,7 @@ fn module_resolution_module_with_path_non_crate_root() { #[test] fn module_resolution_module_decl_path_super() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "bar/baz/module.rs"] @@ -211,9 +199,6 @@ fn module_resolution_module_decl_path_super() { //- /bar/baz/module.rs use super::Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -228,7 +213,7 @@ fn module_resolution_module_decl_path_super() { #[test] fn module_resolution_explicit_path_mod_rs() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "module/mod.rs"] @@ -237,9 +222,6 @@ fn module_resolution_explicit_path_mod_rs() { //- /module/mod.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -253,7 +235,7 @@ fn module_resolution_explicit_path_mod_rs() { #[test] fn module_resolution_relative_path() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo; @@ -265,9 +247,6 @@ fn module_resolution_relative_path() { //- /sub.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -284,7 +263,7 @@ fn module_resolution_relative_path() { #[test] fn module_resolution_relative_path_2() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo; @@ -296,9 +275,6 @@ fn module_resolution_relative_path_2() { //- /sub.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -315,7 +291,7 @@ fn module_resolution_relative_path_2() { #[test] fn module_resolution_explicit_path_mod_rs_2() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "module/bar/mod.rs"] @@ -324,9 +300,6 @@ fn module_resolution_explicit_path_mod_rs_2() { //- /module/bar/mod.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -340,7 +313,7 @@ fn module_resolution_explicit_path_mod_rs_2() { #[test] fn module_resolution_explicit_path_mod_rs_with_win_separator() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "module\bar\mod.rs"] @@ -349,9 +322,6 @@ fn module_resolution_explicit_path_mod_rs_with_win_separator() { //- /module/bar/mod.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -365,7 +335,7 @@ fn module_resolution_explicit_path_mod_rs_with_win_separator() { #[test] fn module_resolution_decl_inside_inline_module_with_path_attribute() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "models"] @@ -376,9 +346,6 @@ fn module_resolution_decl_inside_inline_module_with_path_attribute() { //- /models/bar.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -395,7 +362,7 @@ fn module_resolution_decl_inside_inline_module_with_path_attribute() { #[test] fn module_resolution_decl_inside_inline_module() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo { @@ -405,9 +372,6 @@ fn module_resolution_decl_inside_inline_module() { //- /foo/bar.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -424,7 +388,7 @@ fn module_resolution_decl_inside_inline_module() { #[test] fn module_resolution_decl_inside_inline_module_2_with_path_attribute() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "models/db"] @@ -435,9 +399,6 @@ fn module_resolution_decl_inside_inline_module_2_with_path_attribute() { //- /models/db/bar.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -454,7 +415,7 @@ fn module_resolution_decl_inside_inline_module_2_with_path_attribute() { #[test] fn module_resolution_decl_inside_inline_module_3() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "models/db"] @@ -466,9 +427,6 @@ fn module_resolution_decl_inside_inline_module_3() { //- /models/db/users.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -485,7 +443,7 @@ fn module_resolution_decl_inside_inline_module_3() { #[test] fn module_resolution_decl_inside_inline_module_empty_path() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = ""] @@ -497,9 +455,6 @@ fn module_resolution_decl_inside_inline_module_empty_path() { //- /users.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -516,7 +471,7 @@ fn module_resolution_decl_inside_inline_module_empty_path() { #[test] fn module_resolution_decl_empty_path() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = ""] // Should try to read `/` (a directory) @@ -525,9 +480,6 @@ fn module_resolution_decl_empty_path() { //- /foo.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -537,7 +489,7 @@ fn module_resolution_decl_empty_path() { #[test] fn module_resolution_decl_inside_inline_module_relative_path() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path = "./models"] @@ -548,9 +500,6 @@ fn module_resolution_decl_inside_inline_module_relative_path() { //- /models/bar.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -567,7 +516,7 @@ fn module_resolution_decl_inside_inline_module_relative_path() { #[test] fn module_resolution_decl_inside_inline_module_in_crate_root() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo { @@ -579,9 +528,6 @@ fn module_resolution_decl_inside_inline_module_in_crate_root() { //- /foo/baz.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -599,7 +545,7 @@ fn module_resolution_decl_inside_inline_module_in_crate_root() { #[test] fn module_resolution_decl_inside_inline_module_in_mod_rs() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo; @@ -614,9 +560,6 @@ fn module_resolution_decl_inside_inline_module_in_mod_rs() { //- /foo/bar/qwe.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -637,7 +580,7 @@ fn module_resolution_decl_inside_inline_module_in_mod_rs() { #[test] fn module_resolution_decl_inside_inline_module_in_non_crate_root() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo; @@ -652,9 +595,6 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root() { //- /foo/bar/qwe.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -675,7 +615,7 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root() { #[test] fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs mod foo; @@ -690,9 +630,6 @@ fn module_resolution_decl_inside_inline_module_in_non_crate_root_2() { //- /bar/baz.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" @@ -732,7 +669,7 @@ fn unresolved_module_diagnostics() { #[test] fn module_resolution_decl_inside_module_in_non_crate_root_2() { - let map = def_map_with_crate_graph( + let map = def_map( r###" //- /main.rs #[path="module/m2.rs"] @@ -744,9 +681,6 @@ fn module_resolution_decl_inside_module_in_non_crate_root_2() { //- /module/submod.rs pub struct Baz; "###, - crate_graph! { - "main": ("/main.rs", []), - }, ); assert_snapshot!(map, @r###" -- cgit v1.2.3