Skip to content

Commit 5cff4b6

Browse files
committed
Fix importing private modules in expand glob import
1 parent 5d28dec commit 5cff4b6

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

crates/assists/src/handlers/expand_glob_import.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,28 @@ fn find_refs_in_mod(
162162
module: Module,
163163
visible_from: Option<Module>,
164164
) -> Option<Refs> {
165+
if let Some(from) = visible_from {
166+
if !is_mod_visible_from(ctx, module, from) {
167+
return None;
168+
}
169+
}
170+
165171
let module_scope = module.scope(ctx.db(), visible_from);
166172
let refs = module_scope.into_iter().filter_map(|(n, d)| Ref::from_scope_def(n, d)).collect();
167173
Some(Refs(refs))
168174
}
169175

176+
fn is_mod_visible_from(ctx: &AssistContext, module: Module, from: Module) -> bool {
177+
match module.parent(ctx.db()) {
178+
Some(parent) => {
179+
parent.visibility_of(ctx.db(), &ModuleDef::Module(module)).map_or(true, |vis| {
180+
vis.is_visible_from(ctx.db(), from.into()) && is_mod_visible_from(ctx, parent, from)
181+
})
182+
}
183+
None => true,
184+
}
185+
}
186+
170187
// looks for name refs in parent use block's siblings
171188
//
172189
// mod bar {
@@ -815,6 +832,41 @@ fn main() {
815832
);
816833
}
817834

835+
#[test]
836+
fn expanding_is_not_applicable_if_target_module_is_not_accessible_from_current_scope() {
837+
check_assist_not_applicable(
838+
expand_glob_import,
839+
r"
840+
mod foo {
841+
mod bar {
842+
pub struct Bar;
843+
}
844+
}
845+
846+
use foo::bar::*<|>;
847+
848+
fn baz(bar: Bar) {}
849+
",
850+
);
851+
852+
check_assist_not_applicable(
853+
expand_glob_import,
854+
r"
855+
mod foo {
856+
mod bar {
857+
pub mod baz {
858+
pub struct Baz;
859+
}
860+
}
861+
}
862+
863+
use foo::bar::baz::*<|>;
864+
865+
fn qux(baz: Baz) {}
866+
",
867+
);
868+
}
869+
818870
#[test]
819871
fn expanding_is_not_applicable_if_cursor_is_not_in_star_token() {
820872
check_assist_not_applicable(

0 commit comments

Comments
 (0)