From 5cff4b60bedf4d0b2e30d08aa19686584202a560 Mon Sep 17 00:00:00 2001 From: unexge Date: Mon, 17 Aug 2020 20:22:14 +0300 Subject: Fix importing private modules in expand glob import --- crates/assists/src/handlers/expand_glob_import.rs | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'crates/assists/src') diff --git a/crates/assists/src/handlers/expand_glob_import.rs b/crates/assists/src/handlers/expand_glob_import.rs index 194fae63e..eb6dd68bb 100644 --- a/crates/assists/src/handlers/expand_glob_import.rs +++ b/crates/assists/src/handlers/expand_glob_import.rs @@ -162,11 +162,28 @@ fn find_refs_in_mod( module: Module, visible_from: Option, ) -> Option { + if let Some(from) = visible_from { + if !is_mod_visible_from(ctx, module, from) { + return None; + } + } + let module_scope = module.scope(ctx.db(), visible_from); let refs = module_scope.into_iter().filter_map(|(n, d)| Ref::from_scope_def(n, d)).collect(); Some(Refs(refs)) } +fn is_mod_visible_from(ctx: &AssistContext, module: Module, from: Module) -> bool { + match module.parent(ctx.db()) { + Some(parent) => { + parent.visibility_of(ctx.db(), &ModuleDef::Module(module)).map_or(true, |vis| { + vis.is_visible_from(ctx.db(), from.into()) && is_mod_visible_from(ctx, parent, from) + }) + } + None => true, + } +} + // looks for name refs in parent use block's siblings // // mod bar { @@ -815,6 +832,41 @@ fn main() { ); } + #[test] + fn expanding_is_not_applicable_if_target_module_is_not_accessible_from_current_scope() { + check_assist_not_applicable( + expand_glob_import, + r" +mod foo { + mod bar { + pub struct Bar; + } +} + +use foo::bar::*<|>; + +fn baz(bar: Bar) {} +", + ); + + check_assist_not_applicable( + expand_glob_import, + r" +mod foo { + mod bar { + pub mod baz { + pub struct Baz; + } + } +} + +use foo::bar::baz::*<|>; + +fn qux(baz: Baz) {} +", + ); + } + #[test] fn expanding_is_not_applicable_if_cursor_is_not_in_star_token() { check_assist_not_applicable( -- cgit v1.2.3