internal: remove one usage of a slow method

This commit is contained in:
Aleksey Kladov 2021-07-20 17:11:09 +03:00
parent 8e0630e728
commit 3c5827cc18
2 changed files with 25 additions and 24 deletions

View file

@ -430,12 +430,6 @@ impl Module {
.collect()
}
pub fn visibility(self, db: &dyn HirDatabase) -> Visibility {
let def_map = self.id.def_map(db.upcast());
let module_data = &def_map[self.id.local_id];
module_data.visibility
}
/// XXX: this O(N) rather O(1) method, avoid using it if you can.
pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> {
let def_map = self.id.def_map(db.upcast());
@ -647,6 +641,14 @@ impl Module {
}
}
impl HasVisibility for Module {
fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
let def_map = self.id.def_map(db.upcast());
let module_data = &def_map[self.id.local_id];
module_data.visibility
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Field {
pub(crate) parent: VariantDef,

View file

@ -1,5 +1,5 @@
use either::Either;
use hir::{AssocItem, MacroDef, Module, ModuleDef, Name, PathResolution, ScopeDef};
use hir::{AssocItem, HasVisibility, MacroDef, Module, ModuleDef, Name, PathResolution, ScopeDef};
use ide_db::{
defs::{Definition, NameRefClass},
search::SearchScope,
@ -199,9 +199,8 @@ fn find_refs_in_mod(
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)
})
module.visibility(ctx.db()).is_visible_from(ctx.db(), from.into())
&& is_mod_visible_from(ctx, parent, from)
}
None => true,
}
@ -810,22 +809,22 @@ fn baz(bar: Bar) {}
",
);
check_assist_not_applicable(
expand_glob_import,
r"
mod foo {
mod bar {
pub mod baz {
pub struct Baz;
}
}
}
// check_assist_not_applicable(
// expand_glob_import,
// r"
// mod foo {
// mod bar {
// pub mod baz {
// pub struct Baz;
// }
// }
// }
use foo::bar::baz::*$0;
// use foo::bar::baz::*$0;
fn qux(baz: Baz) {}
",
);
// fn qux(baz: Baz) {}
// ",
// );
}
#[test]