Skip to content

Commit f01f900

Browse files
Merge pull request #19088 from Hmikihiro/all_remove_duplicate_module_adt
fix: if item exsits on module, resolve as module instead of type
2 parents 37355b3 + c84cec1 commit f01f900

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

crates/hir/src/source_analyzer.rs

+38-1
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,42 @@ 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) if matches!(it, PathResolution::Def(ModuleDef::BuiltinType(_))) => {
891+
if let (Some(mod_path), Some(parent_hir_path)) =
892+
(hir_path.mod_path(), parent_hir_path)
893+
{
894+
if let Some(ModuleDefId::ModuleId(id)) = self
895+
.resolver
896+
.resolve_module_path_in_items(db.upcast(), mod_path)
897+
.take_types()
898+
{
899+
let parent_hir_name =
900+
parent_hir_path.segments().get(1).map(|it| it.name);
901+
let module = crate::Module { id };
902+
if module
903+
.scope(db, None)
904+
.into_iter()
905+
.any(|(name, _)| Some(&name) == parent_hir_name)
906+
{
907+
return Some((
908+
PathResolution::Def(ModuleDef::Module(module)),
909+
None,
910+
));
911+
};
912+
}
913+
}
914+
Some((it, None))
915+
}
879916
// FIXME: We do not show substitutions for parts of path, because this is really complex
880917
// due to the interactions with associated items of `impl`s and associated items of associated
881918
// types.

crates/ide/src/goto_definition.rs

+34
Original file line numberDiff line numberDiff line change
@@ -3290,4 +3290,38 @@ fn main() {
32903290
"#,
32913291
);
32923292
}
3293+
3294+
#[test]
3295+
fn shadow_builtin_type_by_module() {
3296+
check(
3297+
r#"
3298+
mod Foo{
3299+
pub mod str {
3300+
// ^^^
3301+
pub fn foo() {}
3302+
}
3303+
}
3304+
3305+
fn main() {
3306+
use Foo::str;
3307+
let s = st$0r::foo();
3308+
}
3309+
"#,
3310+
);
3311+
}
3312+
3313+
#[test]
3314+
fn not_goto_module_because_str_is_builtin_type() {
3315+
check(
3316+
r#"
3317+
mod str {
3318+
pub fn foo() {}
3319+
}
3320+
3321+
fn main() {
3322+
let s = st$0r::f();
3323+
}
3324+
"#,
3325+
);
3326+
}
32933327
}

0 commit comments

Comments
 (0)