Skip to content

Commit

Permalink
Auto merge of #15472 - Veykril:import-ide-support, r=Veykril
Browse files Browse the repository at this point in the history
internal: Record import origins in ItemScope and PerNS

This records the import items definitions come from in the module scope (as well as what an import resolves to in an ItemScope). It does ignore glob imports as thats a lot more work for little to no gain, glob imports act as if the importing items are "inlined" into the scope which suffices for almost all use cases I believe (to my knowledge, attributes on them have little effect).

There is still a lot of work needed to make this available to the IDE layer, but this lays out the ground work for havin IDE layer support.

cc #14079
  • Loading branch information
bors committed Aug 17, 2023
2 parents e69b96b + a17d73a commit 49716e6
Show file tree
Hide file tree
Showing 27 changed files with 730 additions and 387 deletions.
20 changes: 10 additions & 10 deletions crates/hir-def/src/body/tests/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ fn outer() {
"#,
expect![[r#"
block scope
CrateStruct: t
PlainStruct: t v
SelfStruct: t
CrateStruct: ti
PlainStruct: ti vi
SelfStruct: ti
Struct: v
SuperStruct: _
Expand All @@ -66,7 +66,7 @@ fn outer() {
"#,
expect![[r#"
block scope
imported: t v
imported: ti vi
name: v
crate
Expand All @@ -92,9 +92,9 @@ fn outer() {
"#,
expect![[r#"
block scope
inner1: t
inner1: ti
inner2: v
outer: v
outer: vi
block scope
inner: v
Expand All @@ -121,7 +121,7 @@ struct Struct {}
"#,
expect![[r#"
block scope
Struct: t
Struct: ti
crate
Struct: t
Expand Down Expand Up @@ -153,7 +153,7 @@ fn outer() {
"#,
expect![[r#"
block scope
ResolveMe: t
ResolveMe: ti
block scope
m2: t
Expand Down Expand Up @@ -214,7 +214,7 @@ fn f() {
"#,
expect![[r#"
block scope
ResolveMe: t
ResolveMe: ti
block scope
h: v
Expand Down Expand Up @@ -292,7 +292,7 @@ pub mod cov_mark {
nested: v
crate
cov_mark: t
cov_mark: ti
f: v
"#]],
);
Expand Down
18 changes: 18 additions & 0 deletions crates/hir-def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,4 +1293,22 @@ pub mod prelude {
"None",
);
}

#[test]
fn different_crate_renamed_through_dep() {
check_found_path(
r#"
//- /main.rs crate:main deps:intermediate
$0
//- /intermediate.rs crate:intermediate deps:std
pub extern crate std as std_renamed;
//- /std.rs crate:std
pub struct S;
"#,
"intermediate::std_renamed::S",
"intermediate::std_renamed::S",
"intermediate::std_renamed::S",
"intermediate::std_renamed::S",
);
}
}
17 changes: 11 additions & 6 deletions crates/hir-def/src/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,27 @@ fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> FxIndexMap<ItemIn

for (name, per_ns) in visible_items {
for item in per_ns.iter_items() {
// FIXME: Not yet used, but will be once we handle doc(hidden) import sources
let is_doc_hidden = false;

let import_info = ImportInfo {
name: name.clone(),
container: module,
is_trait_assoc_item: false,
};

match depth_map.entry(item) {
Entry::Vacant(entry) => {
entry.insert(depth);
}
Entry::Vacant(entry) => _ = entry.insert((depth, is_doc_hidden)),
Entry::Occupied(mut entry) => {
if depth < *entry.get() {
entry.insert(depth);
} else {
let &(occ_depth, occ_is_doc_hidden) = entry.get();
// Prefer the one that is not doc(hidden),
// Otherwise, if both have the same doc(hidden)-ness and the new path is shorter, prefer that one.
let overwrite_entry = occ_is_doc_hidden && !is_doc_hidden
|| occ_is_doc_hidden == is_doc_hidden && depth < occ_depth;
if !overwrite_entry {
continue;
}
entry.insert((depth, is_doc_hidden));
}
}

Expand Down

0 comments on commit 49716e6

Please sign in to comment.