diff options
author | Jonas Schievink <[email protected]> | 2021-04-19 18:50:11 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-04-19 18:50:11 +0100 |
commit | 59630977a5c5376d9210ae61d5baba52239f1bc1 (patch) | |
tree | 0139b400772d8456844a70bef4821cdb2a8dbf10 /crates/hir_def | |
parent | 0741de87e7697a221b56e9cb458e7c3f81afc9c9 (diff) |
Fix some find_path bugs around inner items
Diffstat (limited to 'crates/hir_def')
-rw-r--r-- | crates/hir_def/src/find_path.rs | 63 |
1 files changed, 58 insertions, 5 deletions
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index 2c4bbe585..dc3f2908f 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs | |||
@@ -119,8 +119,7 @@ fn find_path_inner( | |||
119 | 119 | ||
120 | // - if the item is the crate root, return `crate` | 120 | // - if the item is the crate root, return `crate` |
121 | let root = def_map.crate_root(db); | 121 | let root = def_map.crate_root(db); |
122 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() { | 122 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) { |
123 | // FIXME: the `block_id()` check should be unnecessary, but affects the result | ||
124 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); | 123 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); |
125 | } | 124 | } |
126 | 125 | ||
@@ -131,7 +130,7 @@ fn find_path_inner( | |||
131 | } | 130 | } |
132 | 131 | ||
133 | // - if the item is the crate root of a dependency crate, return the name from the extern prelude | 132 | // - if the item is the crate root of a dependency crate, return the name from the extern prelude |
134 | for (name, def_id) in def_map.extern_prelude() { | 133 | for (name, def_id) in root.def_map(db).extern_prelude() { |
135 | if item == ItemInNs::Types(*def_id) { | 134 | if item == ItemInNs::Types(*def_id) { |
136 | let name = scope_name.unwrap_or_else(|| name.clone()); | 135 | let name = scope_name.unwrap_or_else(|| name.clone()); |
137 | return Some(ModPath::from_segments(PathKind::Plain, vec![name])); | 136 | return Some(ModPath::from_segments(PathKind::Plain, vec![name])); |
@@ -298,6 +297,7 @@ fn find_local_import_locations( | |||
298 | let data = &def_map[from.local_id]; | 297 | let data = &def_map[from.local_id]; |
299 | let mut worklist = | 298 | let mut worklist = |
300 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); | 299 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); |
300 | // FIXME: do we need to traverse out of block expressions here? | ||
301 | for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) { | 301 | for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) { |
302 | worklist.push(ancestor); | 302 | worklist.push(ancestor); |
303 | } | 303 | } |
@@ -947,10 +947,11 @@ fn main() { | |||
947 | $0 | 947 | $0 |
948 | } | 948 | } |
949 | "#, | 949 | "#, |
950 | // FIXME: these could use fewer/better prefixes | ||
950 | "module::CompleteMe", | 951 | "module::CompleteMe", |
951 | "module::CompleteMe", | ||
952 | "crate::module::CompleteMe", | 952 | "crate::module::CompleteMe", |
953 | "self::module::CompleteMe", | 953 | "crate::module::CompleteMe", |
954 | "crate::module::CompleteMe", | ||
954 | ) | 955 | ) |
955 | } | 956 | } |
956 | 957 | ||
@@ -978,6 +979,28 @@ mod bar { | |||
978 | } | 979 | } |
979 | 980 | ||
980 | #[test] | 981 | #[test] |
982 | fn from_inside_module_with_inner_items() { | ||
983 | check_found_path( | ||
984 | r#" | ||
985 | mod baz { | ||
986 | pub struct Foo {} | ||
987 | } | ||
988 | |||
989 | mod bar { | ||
990 | fn bar() { | ||
991 | fn inner() {} | ||
992 | $0 | ||
993 | } | ||
994 | } | ||
995 | "#, | ||
996 | "crate::baz::Foo", | ||
997 | "crate::baz::Foo", | ||
998 | "crate::baz::Foo", | ||
999 | "crate::baz::Foo", | ||
1000 | ) | ||
1001 | } | ||
1002 | |||
1003 | #[test] | ||
981 | fn recursive_pub_mod_reexport() { | 1004 | fn recursive_pub_mod_reexport() { |
982 | cov_mark::check!(recursive_imports); | 1005 | cov_mark::check!(recursive_imports); |
983 | check_found_path( | 1006 | check_found_path( |
@@ -1004,4 +1027,34 @@ pub mod name { | |||
1004 | "self::name::AsName", | 1027 | "self::name::AsName", |
1005 | ); | 1028 | ); |
1006 | } | 1029 | } |
1030 | |||
1031 | #[test] | ||
1032 | fn extern_crate() { | ||
1033 | check_found_path( | ||
1034 | r#" | ||
1035 | //- /main.rs crate:main deps:dep | ||
1036 | $0 | ||
1037 | //- /dep.rs crate:dep | ||
1038 | "#, | ||
1039 | "dep", | ||
1040 | "dep", | ||
1041 | "dep", | ||
1042 | "dep", | ||
1043 | ); | ||
1044 | |||
1045 | check_found_path( | ||
1046 | r#" | ||
1047 | //- /main.rs crate:main deps:dep | ||
1048 | fn f() { | ||
1049 | fn inner() {} | ||
1050 | $0 | ||
1051 | } | ||
1052 | //- /dep.rs crate:dep | ||
1053 | "#, | ||
1054 | "dep", | ||
1055 | "dep", | ||
1056 | "dep", | ||
1057 | "dep", | ||
1058 | ); | ||
1059 | } | ||
1007 | } | 1060 | } |