mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Merge #9583
9583: internal: get rid of a call to slow O(N) visibility_of function r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
7988c80dd5
4 changed files with 29 additions and 13 deletions
|
@ -430,8 +430,16 @@ impl Module {
|
||||||
.collect()
|
.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
|
||||||
|
}
|
||||||
|
|
||||||
pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> {
|
pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> {
|
||||||
self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of((*def).into())
|
let def_map = self.id.def_map(db.upcast());
|
||||||
|
let module_data = &def_map[self.id.local_id];
|
||||||
|
module_data.scope.visibility_of((*def).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
|
pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
|
||||||
|
|
|
@ -72,6 +72,7 @@ use crate::{
|
||||||
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
||||||
path::ModPath,
|
path::ModPath,
|
||||||
per_ns::PerNs,
|
per_ns::PerNs,
|
||||||
|
visibility::Visibility,
|
||||||
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId,
|
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -192,12 +193,14 @@ impl ModuleOrigin {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct ModuleData {
|
pub struct ModuleData {
|
||||||
|
/// Where does this module come from?
|
||||||
|
pub origin: ModuleOrigin,
|
||||||
|
/// Declared visibility of this module.
|
||||||
|
pub visibility: Visibility,
|
||||||
|
|
||||||
pub parent: Option<LocalModuleId>,
|
pub parent: Option<LocalModuleId>,
|
||||||
pub children: FxHashMap<Name, LocalModuleId>,
|
pub children: FxHashMap<Name, LocalModuleId>,
|
||||||
pub scope: ItemScope,
|
pub scope: ItemScope,
|
||||||
|
|
||||||
/// Where does this module come from?
|
|
||||||
pub origin: ModuleOrigin,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefMap {
|
impl DefMap {
|
||||||
|
@ -243,7 +246,15 @@ impl DefMap {
|
||||||
|
|
||||||
fn empty(krate: CrateId, edition: Edition, root_module_origin: ModuleOrigin) -> DefMap {
|
fn empty(krate: CrateId, edition: Edition, root_module_origin: ModuleOrigin) -> DefMap {
|
||||||
let mut modules: Arena<ModuleData> = Arena::default();
|
let mut modules: Arena<ModuleData> = Arena::default();
|
||||||
let root = modules.alloc(ModuleData::new(root_module_origin));
|
|
||||||
|
let local_id = LocalModuleId::from_raw(la_arena::RawIdx::from(0));
|
||||||
|
// NB: we use `None` as block here, which would be wrong for implicit
|
||||||
|
// modules declared by blocks with items. At the moment, we don't use
|
||||||
|
// this visibility for anything outside IDE, so that's probably OK.
|
||||||
|
let visibility = Visibility::Module(ModuleId { krate, local_id, block: None });
|
||||||
|
let root = modules.alloc(ModuleData::new(root_module_origin, visibility));
|
||||||
|
assert_eq!(local_id, root);
|
||||||
|
|
||||||
DefMap {
|
DefMap {
|
||||||
_c: Count::new(),
|
_c: Count::new(),
|
||||||
block: None,
|
block: None,
|
||||||
|
@ -448,12 +459,13 @@ impl DefMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleData {
|
impl ModuleData {
|
||||||
pub(crate) fn new(origin: ModuleOrigin) -> Self {
|
pub(crate) fn new(origin: ModuleOrigin, visibility: Visibility) -> Self {
|
||||||
ModuleData {
|
ModuleData {
|
||||||
|
origin,
|
||||||
|
visibility,
|
||||||
parent: None,
|
parent: None,
|
||||||
children: FxHashMap::default(),
|
children: FxHashMap::default(),
|
||||||
scope: ItemScope::default(),
|
scope: ItemScope::default(),
|
||||||
origin,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1629,7 +1629,7 @@ impl ModCollector<'_, '_> {
|
||||||
ModuleOrigin::File { declaration, definition, is_mod_rs }
|
ModuleOrigin::File { declaration, definition, is_mod_rs }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let res = modules.alloc(ModuleData::new(origin));
|
let res = modules.alloc(ModuleData::new(origin, vis));
|
||||||
modules[res].parent = Some(self.module_id);
|
modules[res].parent = Some(self.module_id);
|
||||||
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
|
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
|
||||||
modules[res].scope.define_legacy_macro(name, mac)
|
modules[res].scope.define_legacy_macro(name, mac)
|
||||||
|
|
|
@ -45,11 +45,7 @@ impl Definition {
|
||||||
match self {
|
match self {
|
||||||
Definition::Field(sf) => Some(sf.visibility(db)),
|
Definition::Field(sf) => Some(sf.visibility(db)),
|
||||||
Definition::ModuleDef(def) => match def {
|
Definition::ModuleDef(def) => match def {
|
||||||
ModuleDef::Module(it) => {
|
ModuleDef::Module(it) => Some(it.visibility(db)),
|
||||||
// FIXME: should work like other cases here.
|
|
||||||
let parent = it.parent(db)?;
|
|
||||||
parent.visibility_of(db, def)
|
|
||||||
}
|
|
||||||
ModuleDef::Function(it) => Some(it.visibility(db)),
|
ModuleDef::Function(it) => Some(it.visibility(db)),
|
||||||
ModuleDef::Adt(it) => Some(it.visibility(db)),
|
ModuleDef::Adt(it) => Some(it.visibility(db)),
|
||||||
ModuleDef::Const(it) => Some(it.visibility(db)),
|
ModuleDef::Const(it) => Some(it.visibility(db)),
|
||||||
|
|
Loading…
Reference in a new issue