From c330c62aa27c6f829f8c4320b952113be5987da4 Mon Sep 17 00:00:00 2001 From: roife Date: Thu, 6 Jun 2024 14:57:57 +0800 Subject: [PATCH 1/2] fix: do not resolve prelude within block modules --- crates/hir-def/src/nameres/path_resolution.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/hir-def/src/nameres/path_resolution.rs b/crates/hir-def/src/nameres/path_resolution.rs index 863ccec885..e797d19223 100644 --- a/crates/hir-def/src/nameres/path_resolution.rs +++ b/crates/hir-def/src/nameres/path_resolution.rs @@ -493,7 +493,12 @@ impl DefMap { ) }) }; - let prelude = || self.resolve_in_prelude(db, name); + let prelude = || { + if self.block.is_some() && module == DefMap::ROOT { + return PerNs::none(); + } + self.resolve_in_prelude(db, name) + }; from_legacy_macro .or(from_scope_or_builtin) From 22e82c72acf95e20cb77799b47ac3c46a816724b Mon Sep 17 00:00:00 2001 From: roife Date: Thu, 6 Jun 2024 16:50:40 +0800 Subject: [PATCH 2/2] test: add regression test for prelude shadowing in block modules --- crates/ide/src/goto_definition.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index 76b80fcefa..f57cb1cb73 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -2289,4 +2289,28 @@ macro_rules! baz { "#, ); } + + #[test] + fn goto_shadowed_preludes_in_block_module() { + check( + r#" +//- /main.rs crate:main edition:2021 deps:core +pub struct S; + //^ + +fn main() { + fn f() -> S$0 { + fn inner() {} // forces a block def map + return S; + } +} +//- /core.rs crate:core +pub mod prelude { + pub mod rust_2021 { + pub enum S; + } +} + "#, + ); + } }