From 231918f56b346b1b3057bd3c37e2dc6070850ad6 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Thu, 27 Mar 2025 15:59:20 +0900 Subject: [PATCH 1/5] fix: shadow type by module Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> --- .../crates/hir-def/src/resolver.rs | 114 +++++++++----- .../crates/hir-ty/src/lower/path.rs | 11 +- .../crates/hir/src/source_analyzer.rs | 84 ++++++---- .../crates/ide/src/goto_definition.rs | 144 ++++++++++++++++++ 4 files changed, 281 insertions(+), 72 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs index 4f1be7285c75..10a8192b93ec 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs @@ -107,6 +107,12 @@ pub enum TypeNs { // ModuleId(ModuleId) } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum ModuleOrTypeNs { + ModuleNs(ModuleId), + TypeNs(TypeNs), +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ResolveValueResult { ValueNs(ValueNs, Option), @@ -163,22 +169,33 @@ impl Resolver { self.resolve_module_path(db, path, BuiltinShadowMode::Module) } - pub fn resolve_path_in_type_ns( - &self, - db: &dyn DefDatabase, - path: &Path, - ) -> Option<(TypeNs, Option, Option)> { + pub fn resolve_path_in_type_ns<'a>( + &'a self, + db: &'a dyn DefDatabase, + path: &'a Path, + ) -> impl Iterator, Option)> + 'a + { self.resolve_path_in_type_ns_with_prefix_info(db, path).map( - |(resolution, remaining_segments, import, _)| (resolution, remaining_segments, import), + move |(resolution, remaining_segments, import, _)| { + (resolution, remaining_segments, import) + }, ) } - pub fn resolve_path_in_type_ns_with_prefix_info( - &self, - db: &dyn DefDatabase, - path: &Path, - ) -> Option<(TypeNs, Option, Option, ResolvePathResultPrefixInfo)> - { + pub fn resolve_path_in_type_ns_with_prefix_info<'a>( + &'a self, + db: &'a dyn DefDatabase, + path: &'a Path, + ) -> Box< + dyn Iterator< + Item = ( + ModuleOrTypeNs, + Option, + Option, + ResolvePathResultPrefixInfo, + ), + > + 'a, + > { let path = match path { Path::BarePath(mod_path) => mod_path, Path::Normal(it) => &it.mod_path, @@ -192,67 +209,73 @@ impl Resolver { LangItemTarget::Trait(it) => TypeNs::TraitId(it), LangItemTarget::Function(_) | LangItemTarget::ImplDef(_) - | LangItemTarget::Static(_) => return None, + | LangItemTarget::Static(_) => return Box::new(iter::empty()), }; - return Some(( - type_ns, + return Box::new(iter::once(( + ModuleOrTypeNs::TypeNs(type_ns), seg.as_ref().map(|_| 1), None, ResolvePathResultPrefixInfo::default(), - )); + ))); } }; - let first_name = path.segments().first()?; + let Some(first_name) = path.segments().first() else { return Box::new(iter::empty()) }; let skip_to_mod = path.kind != PathKind::Plain; if skip_to_mod { - return self.module_scope.resolve_path_in_type_ns(db, path); + return Box::new(self.module_scope.resolve_path_in_type_ns(db, path).into_iter()); } let remaining_idx = || { if path.segments().len() == 1 { None } else { Some(1) } }; - for scope in self.scopes() { - match scope { - Scope::ExprScope(_) | Scope::MacroDefScope(_) => continue, + let ns = self + .scopes() + .filter_map(move |scope| match scope { + Scope::ExprScope(_) | Scope::MacroDefScope(_) => None, Scope::GenericParams { params, def } => { if let Some(id) = params.find_type_by_name(first_name, *def) { return Some(( - TypeNs::GenericParam(id), + ModuleOrTypeNs::TypeNs(TypeNs::GenericParam(id)), remaining_idx(), None, ResolvePathResultPrefixInfo::default(), )); } + None } &Scope::ImplDefScope(impl_) => { if *first_name == sym::Self_.clone() { return Some(( - TypeNs::SelfType(impl_), + ModuleOrTypeNs::TypeNs(TypeNs::SelfType(impl_)), remaining_idx(), None, ResolvePathResultPrefixInfo::default(), )); } + None } &Scope::AdtScope(adt) => { if *first_name == sym::Self_.clone() { return Some(( - TypeNs::AdtSelfType(adt), + ModuleOrTypeNs::TypeNs(TypeNs::AdtSelfType(adt)), remaining_idx(), None, ResolvePathResultPrefixInfo::default(), )); } + None } Scope::BlockScope(m) => { if let Some(res) = m.resolve_path_in_type_ns(db, path) { return Some(res); } + None } - } - } - self.module_scope.resolve_path_in_type_ns(db, path) + }) + .chain(self.module_scope.resolve_path_in_type_ns(db, path)); + + Box::new(ns) } pub fn resolve_path_in_type_ns_fully( @@ -260,7 +283,13 @@ impl Resolver { db: &dyn DefDatabase, path: &Path, ) -> Option { - let (res, unresolved, _) = self.resolve_path_in_type_ns(db, path)?; + let (res, unresolved) = self + .resolve_path_in_type_ns(db, path) + .filter_map(|(res, unresolved, _)| match res { + ModuleOrTypeNs::TypeNs(it) => Some((it, unresolved)), + ModuleOrTypeNs::ModuleNs(_) => None, + }) + .next()?; if unresolved.is_some() { return None; } @@ -1158,8 +1187,12 @@ impl ModuleItemMap { &self, db: &dyn DefDatabase, path: &ModPath, - ) -> Option<(TypeNs, Option, Option, ResolvePathResultPrefixInfo)> - { + ) -> Option<( + ModuleOrTypeNs, + Option, + Option, + ResolvePathResultPrefixInfo, + )> { let (module_def, idx, prefix_info) = self.def_map.resolve_path_locally( &self.local_def_map, db, @@ -1167,7 +1200,7 @@ impl ModuleItemMap { path, BuiltinShadowMode::Other, ); - let (res, import) = to_type_ns(module_def)?; + let (res, import) = to_module_or_type_ns(module_def)?; Some((res, idx, import, prefix_info)) } } @@ -1192,23 +1225,24 @@ fn to_value_ns(per_ns: PerNs) -> Option<(ValueNs, Option)> { Some((res, import)) } -fn to_type_ns(per_ns: PerNs) -> Option<(TypeNs, Option)> { +fn to_module_or_type_ns(per_ns: PerNs) -> Option<(ModuleOrTypeNs, Option)> { let def = per_ns.take_types_full()?; let res = match def.def { - ModuleDefId::AdtId(it) => TypeNs::AdtId(it), - ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it), + ModuleDefId::AdtId(it) => ModuleOrTypeNs::TypeNs(TypeNs::AdtId(it)), + ModuleDefId::EnumVariantId(it) => ModuleOrTypeNs::TypeNs(TypeNs::EnumVariantId(it)), - ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), - ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), + ModuleDefId::TypeAliasId(it) => ModuleOrTypeNs::TypeNs(TypeNs::TypeAliasId(it)), + ModuleDefId::BuiltinType(it) => ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(it)), - ModuleDefId::TraitId(it) => TypeNs::TraitId(it), - ModuleDefId::TraitAliasId(it) => TypeNs::TraitAliasId(it), + ModuleDefId::TraitId(it) => ModuleOrTypeNs::TypeNs(TypeNs::TraitId(it)), + ModuleDefId::TraitAliasId(it) => ModuleOrTypeNs::TypeNs(TypeNs::TraitAliasId(it)), + + ModuleDefId::ModuleId(it) => ModuleOrTypeNs::ModuleNs(it), ModuleDefId::FunctionId(_) | ModuleDefId::ConstId(_) | ModuleDefId::MacroId(_) - | ModuleDefId::StaticId(_) - | ModuleDefId::ModuleId(_) => return None, + | ModuleDefId::StaticId(_) => return None, }; Some((res, def.import)) } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs index 042567030887..2064cad597f9 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs @@ -10,7 +10,7 @@ use hir_def::{ expr_store::HygieneId, generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget}, path::{GenericArg, GenericArgs, GenericArgsParentheses, Path, PathSegment, PathSegments}, - resolver::{ResolveValueResult, TypeNs, ValueNs}, + resolver::{ModuleOrTypeNs, ResolveValueResult, TypeNs, ValueNs}, type_ref::{TypeBound, TypeRef, TypesMap}, }; use smallvec::SmallVec; @@ -333,10 +333,15 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> { } pub(crate) fn resolve_path_in_type_ns(&mut self) -> Option<(TypeNs, Option)> { - let (resolution, remaining_index, _, prefix_info) = self + let (resolution, remaining_index, prefix_info) = self .ctx .resolver - .resolve_path_in_type_ns_with_prefix_info(self.ctx.db.upcast(), self.path)?; + .resolve_path_in_type_ns_with_prefix_info(self.ctx.db.upcast(), self.path) + .filter_map(|(res, remaining_index, _, prefix_info)| match res { + ModuleOrTypeNs::TypeNs(type_ns) => Some((type_ns, remaining_index, prefix_info)), + ModuleOrTypeNs::ModuleNs(_) => None, + }) + .next()?; let segments = self.segments; if segments.is_empty() || matches!(self.path, Path::LangItem(..)) { diff --git a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs index ba5ceef00a69..586e55f42a4a 100644 --- a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs +++ b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs @@ -25,7 +25,7 @@ use hir_def::{ lower::LowerCtx, nameres::MacroSubNs, path::{ModPath, Path, PathKind}, - resolver::{Resolver, TypeNs, ValueNs, resolver_for_scope}, + resolver::{ModuleOrTypeNs, Resolver, TypeNs, ValueNs, resolver_for_scope}, type_ref::{Mutability, TypesMap, TypesSourceMap}, }; use hir_expand::{ @@ -1365,6 +1365,23 @@ pub(crate) fn resolve_hir_path_as_attr_macro( .map(Into::into) } +fn resolve_path_in_module_or_type_ns( + db: &dyn HirDatabase, + resolver: &Resolver, + path: &Path, +) -> Option<(ModuleOrTypeNs, Option)> { + let mut types = resolver + .resolve_path_in_type_ns(db.upcast(), path) + .map(|(ty, remaining_idx, _)| (ty, remaining_idx)) + .peekable(); + let (ty, _) = types.peek()?; + match ty { + ModuleOrTypeNs::ModuleNs(_) => types + .find_or_first(|(ty, _)| matches!(ty, ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(_)))), + ModuleOrTypeNs::TypeNs(_) => types.next(), + } +} + fn resolve_hir_path_( db: &dyn HirDatabase, resolver: &Resolver, @@ -1384,10 +1401,10 @@ fn resolve_hir_path_( resolver.type_owner(), ) .lower_ty_ext(type_ref); - res.map(|ty_ns| (ty_ns, path.segments().first())) + res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first())) } None => { - let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?; + let (ty, remaining_idx) = resolve_path_in_module_or_type_ns(db, resolver, path)?; match remaining_idx { Some(remaining_idx) => { if remaining_idx + 1 == path.segments().len() { @@ -1403,25 +1420,30 @@ fn resolve_hir_path_( // If we are in a TypeNs for a Trait, and we have an unresolved name, try to resolve it as a type // within the trait's associated types. - if let (Some(unresolved), &TypeNs::TraitId(trait_id)) = (&unresolved, &ty) { + if let (Some(unresolved), ModuleOrTypeNs::TypeNs(TypeNs::TraitId(trait_id))) = + (&unresolved, &ty) + { if let Some(type_alias_id) = - db.trait_items(trait_id).associated_type_by_name(unresolved.name) + db.trait_items(*trait_id).associated_type_by_name(unresolved.name) { return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into())); } } let res = match ty { - TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), - TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), - TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { - PathResolution::Def(Adt::from(it).into()) - } - TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), - TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), - TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), - TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), - TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), + ModuleOrTypeNs::TypeNs(ty) => match ty { + TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), + TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), + TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { + PathResolution::Def(Adt::from(it).into()) + } + TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), + TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), + TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), + TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), + TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), + }, + ModuleOrTypeNs::ModuleNs(it) => PathResolution::Def(ModuleDef::Module(it.into())), }; match unresolved { Some(unresolved) => resolver @@ -1517,10 +1539,10 @@ fn resolve_hir_path_qualifier( resolver.type_owner(), ) .lower_ty_ext(type_ref); - res.map(|ty_ns| (ty_ns, path.segments().first())) + res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first())) } None => { - let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?; + let (ty, remaining_idx) = resolve_path_in_module_or_type_ns(db, resolver, path)?; match remaining_idx { Some(remaining_idx) => { if remaining_idx + 1 == path.segments().len() { @@ -1536,25 +1558,29 @@ fn resolve_hir_path_qualifier( // If we are in a TypeNs for a Trait, and we have an unresolved name, try to resolve it as a type // within the trait's associated types. - if let (Some(unresolved), &TypeNs::TraitId(trait_id)) = (&unresolved, &ty) { + if let (Some(unresolved), &ModuleOrTypeNs::TypeNs(TypeNs::TraitId(trait_id))) = + (&unresolved, &ty) + { if let Some(type_alias_id) = db.trait_items(trait_id).associated_type_by_name(unresolved.name) { return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into())); } } - let res = match ty { - TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), - TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), - TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { - PathResolution::Def(Adt::from(it).into()) - } - TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), - TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), - TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), - TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), - TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), + ModuleOrTypeNs::TypeNs(ty) => match ty { + TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), + TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), + TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { + PathResolution::Def(Adt::from(it).into()) + } + TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), + TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), + TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), + TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), + TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), + }, + ModuleOrTypeNs::ModuleNs(it) => PathResolution::Def(ModuleDef::Module(it.into())), }; match unresolved { Some(unresolved) => resolver diff --git a/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs b/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs index fe8295ca2d82..c39d6ff3456f 100644 --- a/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs +++ b/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs @@ -3323,6 +3323,150 @@ pub fn foo() {} fn main() { let s = st$0r::f(); } +"#, + ); + } + + #[test] + fn struct_shadow_by_module() { + check( + r#" +mod foo { + pub mod bar { + // ^^^ + pub type baz = usize; + } +} +struct bar; +fn main() { + use foo::bar; + let x: ba$0r::baz = 5; + +} +"#, + ); + } + + #[test] + fn type_alias_shadow_by_module() { + check( + r#" +mod foo { + pub mod bar { + // ^^^ + pub fn baz() {} + } +} + +trait Qux {} + +fn item() { + use foo::bar; + ba$0r::baz(); +} +} +"#, + ); + + check( + r#" +mod foo { + pub mod bar { + // ^^^ + pub fn baz() {} + } +} + +fn item(x: bar) { + use foo::bar; + let x: bar$0 = x; +} +"#, + ); + } + + #[test] + fn trait_shadow_by_module() { + check( + r#" +pub mod foo { + pub mod Bar {} + // ^^^ +} + +trait Bar {} + +fn main() { + use foo::Bar; + fn f() {} +} + "#, + ); + } + + #[test] + fn const_shadow_by_module() { + check( + r#" +pub mod foo { + pub struct u8 {} + pub mod bar { + pub mod u8 {} + } +} + +fn main() { + use foo::u8; + { + use foo::bar::u8; + + fn f1() {} + } + fn f2() {} +} +"#, + ); + + check( + r#" +pub mod foo { + pub struct u8 {} + // ^^ + pub mod bar { + pub mod u8 {} + } +} + +fn main() { + use foo::u8; + { + use foo::bar::u8; + + fn f1() {} + } + fn f2() {} +} +"#, + ); + + check( + r#" +pub mod foo { + pub struct buz {} + pub mod bar { + pub mod buz {} + // ^^^ + } +} + +fn main() { + use foo::buz; + { + use foo::bar::buz; + + fn f1() {} + } +} "#, ); } From b9c7a9b8d50b6cd87fc1c8bf07da40777ba470b3 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Fri, 28 Mar 2025 15:46:41 +0900 Subject: [PATCH 2/5] return single option Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> --- .../crates/hir-def/src/resolver.rs | 103 +++++++++--------- .../crates/hir-ty/src/lower/path.rs | 13 +-- .../crates/hir/src/source_analyzer.rs | 21 +--- 3 files changed, 59 insertions(+), 78 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs index 10a8192b93ec..bd8ab80eadb6 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs @@ -169,33 +169,26 @@ impl Resolver { self.resolve_module_path(db, path, BuiltinShadowMode::Module) } - pub fn resolve_path_in_type_ns<'a>( - &'a self, - db: &'a dyn DefDatabase, - path: &'a Path, - ) -> impl Iterator, Option)> + 'a - { + pub fn resolve_path_in_type_ns( + &self, + db: &dyn DefDatabase, + path: &Path, + ) -> Option<(ModuleOrTypeNs, Option, Option)> { self.resolve_path_in_type_ns_with_prefix_info(db, path).map( - move |(resolution, remaining_segments, import, _)| { - (resolution, remaining_segments, import) - }, + |(resolution, remaining_segments, import, _)| (resolution, remaining_segments, import), ) } - pub fn resolve_path_in_type_ns_with_prefix_info<'a>( - &'a self, - db: &'a dyn DefDatabase, - path: &'a Path, - ) -> Box< - dyn Iterator< - Item = ( - ModuleOrTypeNs, - Option, - Option, - ResolvePathResultPrefixInfo, - ), - > + 'a, - > { + pub fn resolve_path_in_type_ns_with_prefix_info( + &self, + db: &dyn DefDatabase, + path: &Path, + ) -> Option<( + ModuleOrTypeNs, + Option, + Option, + ResolvePathResultPrefixInfo, + )> { let path = match path { Path::BarePath(mod_path) => mod_path, Path::Normal(it) => &it.mod_path, @@ -209,30 +202,29 @@ impl Resolver { LangItemTarget::Trait(it) => TypeNs::TraitId(it), LangItemTarget::Function(_) | LangItemTarget::ImplDef(_) - | LangItemTarget::Static(_) => return Box::new(iter::empty()), + | LangItemTarget::Static(_) => return None, }; - return Box::new(iter::once(( + return Some(( ModuleOrTypeNs::TypeNs(type_ns), seg.as_ref().map(|_| 1), None, ResolvePathResultPrefixInfo::default(), - ))); + )); } }; - let Some(first_name) = path.segments().first() else { return Box::new(iter::empty()) }; + let first_name = path.segments().first()?; let skip_to_mod = path.kind != PathKind::Plain; if skip_to_mod { - return Box::new(self.module_scope.resolve_path_in_type_ns(db, path).into_iter()); + return self.module_scope.resolve_path_in_module_or_type_ns(db, path); } let remaining_idx = || { if path.segments().len() == 1 { None } else { Some(1) } }; - let ns = self - .scopes() - .filter_map(move |scope| match scope { - Scope::ExprScope(_) | Scope::MacroDefScope(_) => None, + for scope in self.scopes() { + match scope { + Scope::ExprScope(_) | Scope::MacroDefScope(_) => continue, Scope::GenericParams { params, def } => { if let Some(id) = params.find_type_by_name(first_name, *def) { return Some(( @@ -242,7 +234,6 @@ impl Resolver { ResolvePathResultPrefixInfo::default(), )); } - None } &Scope::ImplDefScope(impl_) => { if *first_name == sym::Self_.clone() { @@ -253,7 +244,6 @@ impl Resolver { ResolvePathResultPrefixInfo::default(), )); } - None } &Scope::AdtScope(adt) => { if *first_name == sym::Self_.clone() { @@ -264,18 +254,33 @@ impl Resolver { ResolvePathResultPrefixInfo::default(), )); } - None } Scope::BlockScope(m) => { - if let Some(res) = m.resolve_path_in_type_ns(db, path) { + if let Some(res) = m.resolve_path_in_module_or_type_ns(db, path) { + let res = match res.0 { + ModuleOrTypeNs::TypeNs(_) => res, + ModuleOrTypeNs::ModuleNs(_) => { + if let Some(ModuleDefId::BuiltinType(builtin)) = BUILTIN_SCOPE + .get(first_name) + .and_then(|builtin| builtin.take_types()) + { + ( + ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(builtin)), + remaining_idx(), + None, + ResolvePathResultPrefixInfo::default(), + ) + } else { + res + } + } + }; return Some(res); } - None } - }) - .chain(self.module_scope.resolve_path_in_type_ns(db, path)); - - Box::new(ns) + } + } + self.module_scope.resolve_path_in_module_or_type_ns(db, path) } pub fn resolve_path_in_type_ns_fully( @@ -283,17 +288,11 @@ impl Resolver { db: &dyn DefDatabase, path: &Path, ) -> Option { - let (res, unresolved) = self - .resolve_path_in_type_ns(db, path) - .filter_map(|(res, unresolved, _)| match res { - ModuleOrTypeNs::TypeNs(it) => Some((it, unresolved)), - ModuleOrTypeNs::ModuleNs(_) => None, - }) - .next()?; - if unresolved.is_some() { - return None; + if let (ModuleOrTypeNs::TypeNs(res), None, _) = self.resolve_path_in_type_ns(db, path)? { + Some(res) + } else { + None } - Some(res) } pub fn resolve_visibility( @@ -1183,7 +1182,7 @@ impl ModuleItemMap { } } - fn resolve_path_in_type_ns( + fn resolve_path_in_module_or_type_ns( &self, db: &dyn DefDatabase, path: &ModPath, diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs index 2064cad597f9..08917d70b63a 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs @@ -333,15 +333,14 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> { } pub(crate) fn resolve_path_in_type_ns(&mut self) -> Option<(TypeNs, Option)> { - let (resolution, remaining_index, prefix_info) = self + let (resolution, remaining_index, _, prefix_info) = self .ctx .resolver - .resolve_path_in_type_ns_with_prefix_info(self.ctx.db.upcast(), self.path) - .filter_map(|(res, remaining_index, _, prefix_info)| match res { - ModuleOrTypeNs::TypeNs(type_ns) => Some((type_ns, remaining_index, prefix_info)), - ModuleOrTypeNs::ModuleNs(_) => None, - }) - .next()?; + .resolve_path_in_type_ns_with_prefix_info(self.ctx.db.upcast(), self.path)?; + + let ModuleOrTypeNs::TypeNs(resolution) = resolution else { + return None; + }; let segments = self.segments; if segments.is_empty() || matches!(self.path, Path::LangItem(..)) { diff --git a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs index 586e55f42a4a..4d3b8f9f6017 100644 --- a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs +++ b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs @@ -1365,23 +1365,6 @@ pub(crate) fn resolve_hir_path_as_attr_macro( .map(Into::into) } -fn resolve_path_in_module_or_type_ns( - db: &dyn HirDatabase, - resolver: &Resolver, - path: &Path, -) -> Option<(ModuleOrTypeNs, Option)> { - let mut types = resolver - .resolve_path_in_type_ns(db.upcast(), path) - .map(|(ty, remaining_idx, _)| (ty, remaining_idx)) - .peekable(); - let (ty, _) = types.peek()?; - match ty { - ModuleOrTypeNs::ModuleNs(_) => types - .find_or_first(|(ty, _)| matches!(ty, ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(_)))), - ModuleOrTypeNs::TypeNs(_) => types.next(), - } -} - fn resolve_hir_path_( db: &dyn HirDatabase, resolver: &Resolver, @@ -1404,7 +1387,7 @@ fn resolve_hir_path_( res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first())) } None => { - let (ty, remaining_idx) = resolve_path_in_module_or_type_ns(db, resolver, path)?; + let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?; match remaining_idx { Some(remaining_idx) => { if remaining_idx + 1 == path.segments().len() { @@ -1542,7 +1525,7 @@ fn resolve_hir_path_qualifier( res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first())) } None => { - let (ty, remaining_idx) = resolve_path_in_module_or_type_ns(db, resolver, path)?; + let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?; match remaining_idx { Some(remaining_idx) => { if remaining_idx + 1 == path.segments().len() { From a458b7d6bd8d7b637fcb4456471403716f1f0191 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Fri, 28 Mar 2025 17:51:27 +0900 Subject: [PATCH 3/5] TypeNs contain module Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> --- .../crates/hir-def/src/resolver.rs | 75 ++++++++----------- .../rust-analyzer/crates/hir-ty/src/infer.rs | 3 +- .../crates/hir-ty/src/lower/path.rs | 13 ++-- .../rust-analyzer/crates/hir/src/attrs.rs | 3 + .../crates/hir/src/source_analyzer.rs | 65 +++++++--------- 5 files changed, 71 insertions(+), 88 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs index bd8ab80eadb6..52cb5c927f4d 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs @@ -102,15 +102,8 @@ pub enum TypeNs { BuiltinType(BuiltinType), TraitId(TraitId), TraitAliasId(TraitAliasId), - // Module belong to type ns, but the resolver is used when all module paths - // are fully resolved. - // ModuleId(ModuleId) -} -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum ModuleOrTypeNs { - ModuleNs(ModuleId), - TypeNs(TypeNs), + ModuleId(ModuleId), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -173,7 +166,7 @@ impl Resolver { &self, db: &dyn DefDatabase, path: &Path, - ) -> Option<(ModuleOrTypeNs, Option, Option)> { + ) -> Option<(TypeNs, Option, Option)> { self.resolve_path_in_type_ns_with_prefix_info(db, path).map( |(resolution, remaining_segments, import, _)| (resolution, remaining_segments, import), ) @@ -183,12 +176,8 @@ impl Resolver { &self, db: &dyn DefDatabase, path: &Path, - ) -> Option<( - ModuleOrTypeNs, - Option, - Option, - ResolvePathResultPrefixInfo, - )> { + ) -> Option<(TypeNs, Option, Option, ResolvePathResultPrefixInfo)> + { let path = match path { Path::BarePath(mod_path) => mod_path, Path::Normal(it) => &it.mod_path, @@ -205,7 +194,7 @@ impl Resolver { | LangItemTarget::Static(_) => return None, }; return Some(( - ModuleOrTypeNs::TypeNs(type_ns), + type_ns, seg.as_ref().map(|_| 1), None, ResolvePathResultPrefixInfo::default(), @@ -215,7 +204,7 @@ impl Resolver { let first_name = path.segments().first()?; let skip_to_mod = path.kind != PathKind::Plain; if skip_to_mod { - return self.module_scope.resolve_path_in_module_or_type_ns(db, path); + return self.module_scope.resolve_path_in_type_ns(db, path); } let remaining_idx = || { @@ -228,7 +217,7 @@ impl Resolver { Scope::GenericParams { params, def } => { if let Some(id) = params.find_type_by_name(first_name, *def) { return Some(( - ModuleOrTypeNs::TypeNs(TypeNs::GenericParam(id)), + TypeNs::GenericParam(id), remaining_idx(), None, ResolvePathResultPrefixInfo::default(), @@ -238,7 +227,7 @@ impl Resolver { &Scope::ImplDefScope(impl_) => { if *first_name == sym::Self_.clone() { return Some(( - ModuleOrTypeNs::TypeNs(TypeNs::SelfType(impl_)), + TypeNs::SelfType(impl_), remaining_idx(), None, ResolvePathResultPrefixInfo::default(), @@ -248,7 +237,7 @@ impl Resolver { &Scope::AdtScope(adt) => { if *first_name == sym::Self_.clone() { return Some(( - ModuleOrTypeNs::TypeNs(TypeNs::AdtSelfType(adt)), + TypeNs::AdtSelfType(adt), remaining_idx(), None, ResolvePathResultPrefixInfo::default(), @@ -256,16 +245,15 @@ impl Resolver { } } Scope::BlockScope(m) => { - if let Some(res) = m.resolve_path_in_module_or_type_ns(db, path) { + if let Some(res) = m.resolve_path_in_type_ns(db, path) { let res = match res.0 { - ModuleOrTypeNs::TypeNs(_) => res, - ModuleOrTypeNs::ModuleNs(_) => { + TypeNs::ModuleId(_) => { if let Some(ModuleDefId::BuiltinType(builtin)) = BUILTIN_SCOPE .get(first_name) .and_then(|builtin| builtin.take_types()) { ( - ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(builtin)), + TypeNs::BuiltinType(builtin), remaining_idx(), None, ResolvePathResultPrefixInfo::default(), @@ -274,13 +262,14 @@ impl Resolver { res } } + _ => res, }; return Some(res); } } } } - self.module_scope.resolve_path_in_module_or_type_ns(db, path) + self.module_scope.resolve_path_in_type_ns(db, path) } pub fn resolve_path_in_type_ns_fully( @@ -288,11 +277,11 @@ impl Resolver { db: &dyn DefDatabase, path: &Path, ) -> Option { - if let (ModuleOrTypeNs::TypeNs(res), None, _) = self.resolve_path_in_type_ns(db, path)? { - Some(res) - } else { - None + let (res, unresolved, _) = self.resolve_path_in_type_ns(db, path)?; + if unresolved.is_some() { + return None; } + Some(res) } pub fn resolve_visibility( @@ -1182,16 +1171,12 @@ impl ModuleItemMap { } } - fn resolve_path_in_module_or_type_ns( + fn resolve_path_in_type_ns( &self, db: &dyn DefDatabase, path: &ModPath, - ) -> Option<( - ModuleOrTypeNs, - Option, - Option, - ResolvePathResultPrefixInfo, - )> { + ) -> Option<(TypeNs, Option, Option, ResolvePathResultPrefixInfo)> + { let (module_def, idx, prefix_info) = self.def_map.resolve_path_locally( &self.local_def_map, db, @@ -1199,7 +1184,7 @@ impl ModuleItemMap { path, BuiltinShadowMode::Other, ); - let (res, import) = to_module_or_type_ns(module_def)?; + let (res, import) = to_type_ns(module_def)?; Some((res, idx, import, prefix_info)) } } @@ -1224,19 +1209,19 @@ fn to_value_ns(per_ns: PerNs) -> Option<(ValueNs, Option)> { Some((res, import)) } -fn to_module_or_type_ns(per_ns: PerNs) -> Option<(ModuleOrTypeNs, Option)> { +fn to_type_ns(per_ns: PerNs) -> Option<(TypeNs, Option)> { let def = per_ns.take_types_full()?; let res = match def.def { - ModuleDefId::AdtId(it) => ModuleOrTypeNs::TypeNs(TypeNs::AdtId(it)), - ModuleDefId::EnumVariantId(it) => ModuleOrTypeNs::TypeNs(TypeNs::EnumVariantId(it)), + ModuleDefId::AdtId(it) => TypeNs::AdtId(it), + ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it), - ModuleDefId::TypeAliasId(it) => ModuleOrTypeNs::TypeNs(TypeNs::TypeAliasId(it)), - ModuleDefId::BuiltinType(it) => ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(it)), + ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it), + ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), - ModuleDefId::TraitId(it) => ModuleOrTypeNs::TypeNs(TypeNs::TraitId(it)), - ModuleDefId::TraitAliasId(it) => ModuleOrTypeNs::TypeNs(TypeNs::TraitAliasId(it)), + ModuleDefId::TraitId(it) => TypeNs::TraitId(it), + ModuleDefId::TraitAliasId(it) => TypeNs::TraitAliasId(it), - ModuleDefId::ModuleId(it) => ModuleOrTypeNs::ModuleNs(it), + ModuleDefId::ModuleId(it) => TypeNs::ModuleId(it), ModuleDefId::FunctionId(_) | ModuleDefId::ConstId(_) diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs index 0bd605c18b4a..00741399cbb8 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs @@ -1648,7 +1648,8 @@ impl<'a> InferenceContext<'a> { TypeNs::AdtId(AdtId::EnumId(_)) | TypeNs::BuiltinType(_) | TypeNs::TraitId(_) - | TypeNs::TraitAliasId(_) => { + | TypeNs::TraitAliasId(_) + | TypeNs::ModuleId(_) => { // FIXME diagnostic (self.err_ty(), None) } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs index 08917d70b63a..cc3e630ee9ba 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs @@ -10,7 +10,7 @@ use hir_def::{ expr_store::HygieneId, generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget}, path::{GenericArg, GenericArgs, GenericArgsParentheses, Path, PathSegment, PathSegments}, - resolver::{ModuleOrTypeNs, ResolveValueResult, TypeNs, ValueNs}, + resolver::{ResolveValueResult, TypeNs, ValueNs}, type_ref::{TypeBound, TypeRef, TypesMap}, }; use smallvec::SmallVec; @@ -285,7 +285,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> { TypeNs::BuiltinType(it) => self.lower_path_inner(it.into(), infer_args), TypeNs::TypeAliasId(it) => self.lower_path_inner(it.into(), infer_args), // FIXME: report error - TypeNs::EnumVariantId(_) => return (TyKind::Error.intern(Interner), None), + TypeNs::EnumVariantId(_) | TypeNs::ModuleId(_) => { + return (TyKind::Error.intern(Interner), None); + } }; self.skip_resolved_segment(); @@ -316,6 +318,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> { TypeNs::BuiltinType(_) => { prohibit_generics_on_resolved(GenericArgsProhibitedReason::PrimitiveTy) } + TypeNs::ModuleId(_) => { + prohibit_generics_on_resolved(GenericArgsProhibitedReason::Module) + } TypeNs::AdtId(_) | TypeNs::EnumVariantId(_) | TypeNs::TypeAliasId(_) @@ -338,10 +343,6 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> { .resolver .resolve_path_in_type_ns_with_prefix_info(self.ctx.db.upcast(), self.path)?; - let ModuleOrTypeNs::TypeNs(resolution) = resolution else { - return None; - }; - let segments = self.segments; if segments.is_empty() || matches!(self.path, Path::LangItem(..)) { // `segments.is_empty()` can occur with `self`. diff --git a/src/tools/rust-analyzer/crates/hir/src/attrs.rs b/src/tools/rust-analyzer/crates/hir/src/attrs.rs index e71b51bfa432..3e67cc33d9c5 100644 --- a/src/tools/rust-analyzer/crates/hir/src/attrs.rs +++ b/src/tools/rust-analyzer/crates/hir/src/attrs.rs @@ -207,6 +207,9 @@ fn resolve_assoc_or_field( // XXX: Do these get resolved? return None; } + TypeNs::ModuleId(_) => { + return None; + } }; // Resolve inherent items first, then trait items, then fields. diff --git a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs index 4d3b8f9f6017..8a5d241f2b0a 100644 --- a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs +++ b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs @@ -25,7 +25,7 @@ use hir_def::{ lower::LowerCtx, nameres::MacroSubNs, path::{ModPath, Path, PathKind}, - resolver::{ModuleOrTypeNs, Resolver, TypeNs, ValueNs, resolver_for_scope}, + resolver::{Resolver, TypeNs, ValueNs, resolver_for_scope}, type_ref::{Mutability, TypesMap, TypesSourceMap}, }; use hir_expand::{ @@ -1384,7 +1384,7 @@ fn resolve_hir_path_( resolver.type_owner(), ) .lower_ty_ext(type_ref); - res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first())) + res.map(|ty_ns| (ty_ns, path.segments().first())) } None => { let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?; @@ -1403,30 +1403,26 @@ fn resolve_hir_path_( // If we are in a TypeNs for a Trait, and we have an unresolved name, try to resolve it as a type // within the trait's associated types. - if let (Some(unresolved), ModuleOrTypeNs::TypeNs(TypeNs::TraitId(trait_id))) = - (&unresolved, &ty) - { + if let (Some(unresolved), &TypeNs::TraitId(trait_id)) = (&unresolved, &ty) { if let Some(type_alias_id) = - db.trait_items(*trait_id).associated_type_by_name(unresolved.name) + db.trait_items(trait_id).associated_type_by_name(unresolved.name) { return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into())); } } let res = match ty { - ModuleOrTypeNs::TypeNs(ty) => match ty { - TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), - TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), - TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { - PathResolution::Def(Adt::from(it).into()) - } - TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), - TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), - TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), - TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), - TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), - }, - ModuleOrTypeNs::ModuleNs(it) => PathResolution::Def(ModuleDef::Module(it.into())), + TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), + TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), + TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { + PathResolution::Def(Adt::from(it).into()) + } + TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), + TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), + TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), + TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), + TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), + TypeNs::ModuleId(it) => PathResolution::Def(ModuleDef::Module(it.into())), }; match unresolved { Some(unresolved) => resolver @@ -1522,7 +1518,7 @@ fn resolve_hir_path_qualifier( resolver.type_owner(), ) .lower_ty_ext(type_ref); - res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first())) + res.map(|ty_ns| (ty_ns, path.segments().first())) } None => { let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?; @@ -1541,29 +1537,26 @@ fn resolve_hir_path_qualifier( // If we are in a TypeNs for a Trait, and we have an unresolved name, try to resolve it as a type // within the trait's associated types. - if let (Some(unresolved), &ModuleOrTypeNs::TypeNs(TypeNs::TraitId(trait_id))) = - (&unresolved, &ty) - { + if let (Some(unresolved), &TypeNs::TraitId(trait_id)) = (&unresolved, &ty) { if let Some(type_alias_id) = db.trait_items(trait_id).associated_type_by_name(unresolved.name) { return Some(PathResolution::Def(ModuleDefId::from(type_alias_id).into())); } } + let res = match ty { - ModuleOrTypeNs::TypeNs(ty) => match ty { - TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), - TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), - TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { - PathResolution::Def(Adt::from(it).into()) - } - TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), - TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), - TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), - TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), - TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), - }, - ModuleOrTypeNs::ModuleNs(it) => PathResolution::Def(ModuleDef::Module(it.into())), + TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), + TypeNs::GenericParam(id) => PathResolution::TypeParam(id.into()), + TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { + PathResolution::Def(Adt::from(it).into()) + } + TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), + TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), + TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), + TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), + TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()), + TypeNs::ModuleId(it) => PathResolution::Def(ModuleDef::Module(it.into())), }; match unresolved { Some(unresolved) => resolver From b6cf8f24999d52012d5dc63f2dbb4558dcce1924 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Tue, 8 Apr 2025 18:07:14 +0900 Subject: [PATCH 4/5] Update crates/hir-def/src/resolver.rs Co-authored-by: Lukas Wirth --- src/tools/rust-analyzer/crates/hir-def/src/resolver.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs index 52cb5c927f4d..621b24dc6d3b 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/resolver.rs @@ -247,7 +247,7 @@ impl Resolver { Scope::BlockScope(m) => { if let Some(res) = m.resolve_path_in_type_ns(db, path) { let res = match res.0 { - TypeNs::ModuleId(_) => { + TypeNs::ModuleId(_) if res.1.is_none() => { if let Some(ModuleDefId::BuiltinType(builtin)) = BUILTIN_SCOPE .get(first_name) .and_then(|builtin| builtin.take_types()) From 6bc123f4aa5f70d425f6f7a4c9a7b22f300ee7b4 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Tue, 8 Apr 2025 18:14:37 +0900 Subject: [PATCH 5/5] Update crates/hir-def/src/resolver.rs Co-authored-by: Lukas Wirth