Skip to content

internal: Record import origins in ItemScope and PerNS #15472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading