Skip to content

Commit 9568a66

Browse files
committed
conflict with type name and module name
Signed-off-by: Hayashi Mikihiro <[email protected]>
1 parent 13c17db commit 9568a66

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

crates/hir/src/source_analyzer.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,8 @@ impl SourceAnalyzer {
866866

867867
// Case where path is a qualifier of another path, e.g. foo::bar::Baz where we are
868868
// trying to resolve foo::bar.
869-
if path.parent_path().is_some() {
869+
if let Some(parent_path) = path.parent_path() {
870+
let parent_hir_path = Path::from_src(&mut ctx, parent_path);
870871
return match resolve_hir_path_qualifier(db, &self.resolver, &hir_path, &types_map) {
871872
None if meta_path.is_some() => path
872873
.first_segment()
@@ -876,6 +877,47 @@ impl SourceAnalyzer {
876877
.map(PathResolution::ToolModule)
877878
})
878879
.map(|it| (it, None)),
880+
// Case the type name conflict with use module,
881+
// e.g.
882+
// ```
883+
// use std::str;
884+
// fn main() {
885+
// str::from_utf8(); // as module std::str
886+
// str::len(); // as primitive type str
887+
// str::no_exist_item(); // as primitive type str
888+
// }
889+
// ```
890+
Some(it)
891+
if matches!(
892+
it,
893+
PathResolution::Def(ModuleDef::Adt(_) | ModuleDef::BuiltinType(_))
894+
) =>
895+
{
896+
if let (Some(mod_path), Some(parent_hir_path)) =
897+
(hir_path.mod_path(), parent_hir_path)
898+
{
899+
if let Some(ModuleDefId::ModuleId(id)) = self
900+
.resolver
901+
.resolve_module_path_in_items(db.upcast(), mod_path)
902+
.take_types()
903+
{
904+
let parent_hir_name =
905+
parent_hir_path.segments().get(1).map(|it| it.name);
906+
let module = crate::Module { id };
907+
if module
908+
.scope(db, None)
909+
.into_iter()
910+
.any(|(name, _)| Some(&name) == parent_hir_name)
911+
{
912+
return Some((
913+
PathResolution::Def(ModuleDef::Module(module)),
914+
None,
915+
));
916+
};
917+
}
918+
}
919+
Some((it, None))
920+
}
879921
// FIXME: We do not show substitutions for parts of path, because this is really complex
880922
// due to the interactions with associated items of `impl`s and associated items of associated
881923
// types.

crates/ide-assists/src/handlers/remove_unused_imports.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,42 @@ fn test(_: Bar) {
10091009
let a = ();
10101010
a.quxx();
10111011
}
1012+
"#,
1013+
);
1014+
}
1015+
1016+
#[test]
1017+
fn duplicate_struct_module_in_use() {
1018+
check_assist(
1019+
remove_unused_imports,
1020+
r#"
1021+
//- /main.rs
1022+
struct sub2;
1023+
1024+
mod sub1;
1025+
1026+
fn main() {
1027+
use $0{sub1, sub1::sub2};$0
1028+
sub2::fn_sub2();
1029+
}
1030+
//- /sub1.rs
1031+
pub mod sub2;
1032+
1033+
//- /sub1/sub2.rs
1034+
pub mod sample {
1035+
pub fn fn_sub2() {}
1036+
}
1037+
pub use sample::fn_sub2;
1038+
"#,
1039+
r#"
1040+
struct sub2;
1041+
1042+
mod sub1;
1043+
1044+
fn main() {
1045+
use sub1::sub2;
1046+
sub2::fn_sub2();
1047+
}
10121048
"#,
10131049
);
10141050
}

0 commit comments

Comments
 (0)