From 8b4a94f261d77683684dbf4b9f4b1eac0dc5956a Mon Sep 17 00:00:00 2001 From: Khanh Duong Quoc Date: Sat, 19 Oct 2024 20:22:36 +0900 Subject: [PATCH] fix: private items are shown in completions for modules in fn body --- .../crates/hir-def/src/visibility.rs | 6 ++---- .../crates/ide-completion/src/tests/pattern.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/visibility.rs b/src/tools/rust-analyzer/crates/hir-def/src/visibility.rs index 11d91513f12..3aeb88047a0 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/visibility.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/visibility.rs @@ -139,13 +139,11 @@ fn is_visible_from_def_map_( let def_map_block = def_map.block_id(); loop { match (to_module.block, def_map_block) { - // to_module is not a block, so there is no parent def map to use + // `to_module` is not a block, so there is no parent def map to use. (None, _) => (), + // `to_module` is at `def_map`'s block, no need to move further. (Some(a), Some(b)) if a == b => { cov_mark::hit!(is_visible_from_same_block_def_map); - if let Some(parent) = def_map.parent() { - to_module = parent; - } } _ => { if let Some(parent) = to_module.def_map(db).parent() { diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs index bd3e7c72bcd..a5eb0369b14 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs @@ -923,3 +923,21 @@ fn foo() { "#]], ); } + +#[test] +fn private_item_in_module_in_function_body() { + check_empty( + r#" +fn main() { + mod foo { + struct Private; + pub struct Public; + } + foo::$0 +} +"#, + expect![[r#" + st Public Public + "#]], + ); +}