mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
internal: remove one usage of a slow method
This commit is contained in:
parent
5fee2bef19
commit
8e0630e728
3 changed files with 20 additions and 4 deletions
|
@ -436,6 +436,7 @@ impl Module {
|
||||||
module_data.visibility
|
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> {
|
pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> {
|
||||||
let def_map = self.id.def_map(db.upcast());
|
let def_map = self.id.def_map(db.upcast());
|
||||||
let module_data = &def_map[self.id.local_id];
|
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
|
/// A Data Type
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum Adt {
|
pub enum Adt {
|
||||||
|
|
|
@ -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 current_module = ctx.sema.scope(path.syntax()).module()?;
|
||||||
let target_module = def.module(ctx.db())?;
|
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()) {
|
if vis.is_visible_from(ctx.db(), current_module.into()) {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
|
@ -52,9 +52,7 @@ impl Definition {
|
||||||
ModuleDef::Static(it) => Some(it.visibility(db)),
|
ModuleDef::Static(it) => Some(it.visibility(db)),
|
||||||
ModuleDef::Trait(it) => Some(it.visibility(db)),
|
ModuleDef::Trait(it) => Some(it.visibility(db)),
|
||||||
ModuleDef::TypeAlias(it) => Some(it.visibility(db)),
|
ModuleDef::TypeAlias(it) => Some(it.visibility(db)),
|
||||||
// NB: Variants don't have their own visibility, and just inherit
|
ModuleDef::Variant(it) => Some(it.visibility(db)),
|
||||||
// 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::BuiltinType(_) => None,
|
ModuleDef::BuiltinType(_) => None,
|
||||||
},
|
},
|
||||||
Definition::Macro(_)
|
Definition::Macro(_)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue