Skip to content

Commit 908cd23

Browse files
bors[bot]Veykril
andauthored
Merge #8820
8820: fix: Return absolute paths in find_path if crate start is ambiguous r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents c9c9b4e + 69e0b10 commit 908cd23

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

crates/hir_def/src/find_path.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::iter;
55
use hir_expand::name::{known, AsName, Name};
66
use rustc_hash::FxHashSet;
77

8-
use crate::nameres::DefMap;
98
use crate::{
109
db::DefDatabase,
1110
item_scope::ItemInNs,
11+
nameres::DefMap,
1212
path::{ModPath, PathKind},
1313
visibility::Visibility,
1414
ModuleDefId, ModuleId,
@@ -134,7 +134,16 @@ fn find_path_inner(
134134
for (name, def_id) in root_def_map.extern_prelude() {
135135
if item == ItemInNs::Types(*def_id) {
136136
let name = scope_name.unwrap_or_else(|| name.clone());
137-
return Some(ModPath::from_segments(PathKind::Plain, vec![name]));
137+
138+
let name_already_occupied_in_type_ns = def_map
139+
.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
140+
def_map[local_id].scope.get(&name).take_types().filter(|&id| id != *def_id)
141+
})
142+
.is_some();
143+
return Some(ModPath::from_segments(
144+
if name_already_occupied_in_type_ns { PathKind::Abs } else { PathKind::Plain },
145+
vec![name],
146+
));
138147
}
139148
}
140149

crates/ide_assists/src/handlers/auto_import.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,32 @@ mod bar {
966966
println!("Hallo");
967967
}
968968
}
969+
"#,
970+
);
971+
}
972+
973+
#[test]
974+
fn uses_abs_path_with_extern_crate_clash() {
975+
check_assist(
976+
auto_import,
977+
r#"
978+
//- /main.rs crate:main deps:foo
979+
mod foo {}
980+
981+
const _: () = {
982+
Foo$0
983+
};
984+
//- /foo.rs crate:foo
985+
pub struct Foo
986+
"#,
987+
r#"
988+
use ::foo::Foo;
989+
990+
mod foo {}
991+
992+
const _: () = {
993+
Foo
994+
};
969995
"#,
970996
);
971997
}

0 commit comments

Comments
 (0)