diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index ccc9f22eb5..c984148c3a 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -387,7 +387,13 @@ impl DefMap { .get_legacy_macro(name) .map_or_else(PerNs::none, |m| PerNs::macros(m, Visibility::Public)); let from_scope = self[module].scope.get(name); - let from_builtin = BUILTIN_SCOPE.get(name).copied().unwrap_or_else(PerNs::none); + let from_builtin = match self.block { + Some(_) => { + // Only resolve to builtins in the root `DefMap`. + PerNs::none() + } + None => BUILTIN_SCOPE.get(name).copied().unwrap_or_else(PerNs::none), + }; let from_scope_or_builtin = match shadow { BuiltinShadowMode::Module => from_scope.or(from_builtin), BuiltinShadowMode::Other => { diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index 84c5c05fd3..5948d0bc22 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -1764,6 +1764,24 @@ fn main() { ); } +#[test] +fn shadowing_primitive_with_inner_items() { + check_types( + r#" +struct i32; +struct Foo; + +impl i32 { fn foo(&self) -> Foo { Foo } } + +fn main() { + fn inner() {} + let x: i32 = i32; + x.foo(); + //^ Foo +}"#, + ); +} + #[test] fn not_shadowing_primitive_by_module() { check_types(