internal: remove one usage of a slow method

This commit is contained in:
Aleksey Kladov 2021-07-20 17:02:16 +03:00
parent 5fee2bef19
commit 8e0630e728
3 changed files with 20 additions and 4 deletions

View File

@ -436,6 +436,7 @@ impl Module {
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());
let module_data = &def_map[self.id.local_id];
@ -841,6 +842,13 @@ impl Variant {
}
}
/// Variants inherit visibility from the parent enum.
impl HasVisibility for Variant {
fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
self.parent_enum(db).visibility(db)
}
}
/// A Data Type
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Adt {

View File

@ -46,7 +46,17 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O
let current_module = ctx.sema.scope(path.syntax()).module()?;
let target_module = def.module(ctx.db())?;
let vis = target_module.visibility_of(ctx.db(), &def)?;
let vis = match def {
hir::ModuleDef::Module(it) => it.visibility(ctx.db()),
hir::ModuleDef::Function(it) => it.visibility(ctx.db()),
hir::ModuleDef::Adt(it) => it.visibility(ctx.db()),
hir::ModuleDef::Variant(it) => it.visibility(ctx.db()),
hir::ModuleDef::Const(it) => it.visibility(ctx.db()),
hir::ModuleDef::Static(it) => it.visibility(ctx.db()),
hir::ModuleDef::Trait(it) => it.visibility(ctx.db()),
hir::ModuleDef::TypeAlias(it) => it.visibility(ctx.db()),
hir::ModuleDef::BuiltinType(_) => return None,
};
if vis.is_visible_from(ctx.db(), current_module.into()) {
return None;
};

View File

@ -52,9 +52,7 @@ impl Definition {
ModuleDef::Static(it) => Some(it.visibility(db)),
ModuleDef::Trait(it) => Some(it.visibility(db)),
ModuleDef::TypeAlias(it) => Some(it.visibility(db)),
// NB: Variants don't have their own visibility, and just inherit
// one from the parent. Not sure if that's the right thing to do.
ModuleDef::Variant(it) => Some(it.parent_enum(db).visibility(db)),
ModuleDef::Variant(it) => Some(it.visibility(db)),
ModuleDef::BuiltinType(_) => None,
},
Definition::Macro(_)