Skip to content

Commit f9eebaf

Browse files
committed
move to check module in resolve_hir_path_qualifier
Signed-off-by: Hayashi Mikihiro <[email protected]>
1 parent bbd1055 commit f9eebaf

File tree

1 file changed

+42
-53
lines changed

1 file changed

+42
-53
lines changed

Diff for: crates/hir/src/source_analyzer.rs

+42-53
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ impl SourceAnalyzer {
849849
// trying to resolve foo::bar.
850850
if let Some(use_tree) = parent().and_then(ast::UseTree::cast) {
851851
if use_tree.coloncolon_token().is_some() {
852-
return resolve_hir_path_qualifier(db, &self.resolver, &hir_path, &types_map)
852+
return resolve_hir_path_qualifier(db, &self.resolver, &hir_path, &types_map, None)
853853
.map(|it| (it, None));
854854
}
855855
}
@@ -872,9 +872,15 @@ impl SourceAnalyzer {
872872
// Case where path is a qualifier of another path, e.g. foo::bar::Baz or foo::bar:: where we are
873873
// trying to resolve foo::bar.
874874
if path.parent_path().is_some() || is_last_token_colon2 {
875-
let parent_hir_path =
875+
let parent_path =
876876
path.parent_path().and_then(|parent_path| Path::from_src(&mut ctx, parent_path));
877-
return match resolve_hir_path_qualifier(db, &self.resolver, &hir_path, &types_map) {
877+
return match resolve_hir_path_qualifier(
878+
db,
879+
&self.resolver,
880+
&hir_path,
881+
&types_map,
882+
parent_path.as_ref(),
883+
) {
878884
None if meta_path.is_some() => path
879885
.first_segment()
880886
.and_then(|it| it.name_ref())
@@ -883,51 +889,6 @@ impl SourceAnalyzer {
883889
.map(PathResolution::ToolModule)
884890
})
885891
.map(|it| (it, None)),
886-
// Case the type name conflict with use module,
887-
// e.g.
888-
// ```
889-
// use std::str;
890-
// fn main() {
891-
// str::from_utf8(); // as module std::str
892-
// str::len(); // as primitive type str
893-
// str::no_exist_item(); // as primitive type str
894-
// }
895-
// ```
896-
Some(it)
897-
if matches!(
898-
it,
899-
PathResolution::Def(ModuleDef::Adt(_) | ModuleDef::BuiltinType(_))
900-
) =>
901-
{
902-
if let Some(mod_path) = hir_path.mod_path() {
903-
if let Some(ModuleDefId::ModuleId(id)) = self
904-
.resolver
905-
.resolve_module_path_in_items(db.upcast(), mod_path)
906-
.take_types()
907-
{
908-
let module = crate::Module { id };
909-
let res = Some((PathResolution::Def(ModuleDef::Module(module)), None));
910-
match it {
911-
PathResolution::Def(ModuleDef::Adt(_)) => return res,
912-
PathResolution::Def(ModuleDef::BuiltinType(_)) => {
913-
if let Some(parent_hir_path) = parent_hir_path {
914-
let parent_hir_name =
915-
parent_hir_path.segments().get(1).map(|it| it.name);
916-
if module
917-
.scope(db, None)
918-
.into_iter()
919-
.any(|(name, _)| Some(&name) == parent_hir_name)
920-
{
921-
return res;
922-
}
923-
}
924-
}
925-
_ => (),
926-
}
927-
}
928-
}
929-
Some((it, None))
930-
}
931892
// FIXME: We do not show substitutions for parts of path, because this is really complex
932893
// due to the interactions with associated items of `impl`s and associated items of associated
933894
// types.
@@ -1003,7 +964,7 @@ impl SourceAnalyzer {
1003964
}
1004965
if parent().is_some_and(|it| ast::Visibility::can_cast(it.kind())) {
1005966
// No substitution because only modules can be inside visibilities, and those have no generics.
1006-
resolve_hir_path_qualifier(db, &self.resolver, &hir_path, &types_map)
967+
resolve_hir_path_qualifier(db, &self.resolver, &hir_path, &types_map, None)
1007968
.map(|it| (it, None))
1008969
} else {
1009970
// Probably a type, no need to show substitutions for those.
@@ -1550,8 +1511,9 @@ fn resolve_hir_path_qualifier(
15501511
resolver: &Resolver,
15511512
path: &Path,
15521513
types_map: &TypesMap,
1514+
parent_path: Option<&Path>,
15531515
) -> Option<PathResolution> {
1554-
(|| {
1516+
let resolve_from_typens = (|| {
15551517
let (ty, unresolved) = match path.type_anchor() {
15561518
Some(type_ref) => {
15571519
let (_, res) = TyLoweringContext::new_maybe_unowned(
@@ -1617,13 +1579,40 @@ fn resolve_hir_path_qualifier(
16171579
.map(PathResolution::Def),
16181580
None => Some(res),
16191581
}
1620-
})()
1621-
.or_else(|| {
1582+
})();
1583+
1584+
let resolve_module = || {
16221585
resolver
16231586
.resolve_module_path_in_items(db.upcast(), path.mod_path()?)
16241587
.take_types()
16251588
.map(|it| PathResolution::Def(it.into()))
1626-
})
1589+
};
1590+
1591+
match resolve_from_typens {
1592+
Some(PathResolution::Def(module_def)) => {
1593+
let item = resolve_module();
1594+
if let Some(PathResolution::Def(ModuleDef::Module(module))) = item {
1595+
match module_def {
1596+
ModuleDef::BuiltinType(_) => {
1597+
if let Some(parent_path) = parent_path {
1598+
let parent_name = parent_path.segments().get(1).map(|it| it.name);
1599+
if module
1600+
.scope(db, None)
1601+
.into_iter()
1602+
.any(|(name, _)| Some(&name) == parent_name)
1603+
{
1604+
return item;
1605+
}
1606+
}
1607+
}
1608+
_ => return item,
1609+
}
1610+
}
1611+
resolve_from_typens
1612+
}
1613+
None => resolve_module(),
1614+
Some(_) => resolve_from_typens,
1615+
}
16271616
}
16281617

16291618
pub(crate) fn name_hygiene(db: &dyn HirDatabase, name: InFile<&SyntaxNode>) -> HygieneId {

0 commit comments

Comments
 (0)