mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Refactor PerNs construction
This commit is contained in:
parent
fe38fffaa9
commit
e69af85962
2 changed files with 52 additions and 49 deletions
|
@ -5,7 +5,7 @@ use hir_expand::name::Name;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
|
use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq, Eq)]
|
#[derive(Debug, Default, PartialEq, Eq)]
|
||||||
pub struct ItemScope {
|
pub struct ItemScope {
|
||||||
|
@ -153,3 +153,21 @@ pub struct Resolution {
|
||||||
pub def: PerNs,
|
pub def: PerNs,
|
||||||
pub(crate) import: bool,
|
pub(crate) import: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ModuleDefId> for PerNs {
|
||||||
|
fn from(def: ModuleDefId) -> PerNs {
|
||||||
|
match def {
|
||||||
|
ModuleDefId::ModuleId(_) => PerNs::types(def),
|
||||||
|
ModuleDefId::FunctionId(_) => PerNs::values(def),
|
||||||
|
ModuleDefId::AdtId(adt) => match adt {
|
||||||
|
AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def),
|
||||||
|
AdtId::EnumId(_) => PerNs::types(def),
|
||||||
|
},
|
||||||
|
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def),
|
||||||
|
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def),
|
||||||
|
ModuleDefId::TraitId(_) => PerNs::types(def),
|
||||||
|
ModuleDefId::TypeAliasId(_) => PerNs::types(def),
|
||||||
|
ModuleDefId::BuiltinType(_) => PerNs::types(def),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -714,12 +714,9 @@ where
|
||||||
modules[res].scope.define_legacy_macro(name, mac)
|
modules[res].scope.define_legacy_macro(name, mac)
|
||||||
}
|
}
|
||||||
modules[self.module_id].children.insert(name.clone(), res);
|
modules[self.module_id].children.insert(name.clone(), res);
|
||||||
let resolution = Resolution {
|
let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
|
||||||
def: PerNs::types(
|
let def: ModuleDefId = module.into();
|
||||||
ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
|
let resolution = Resolution { def: def.into(), import: false };
|
||||||
),
|
|
||||||
import: false,
|
|
||||||
};
|
|
||||||
self.def_collector.update(self.module_id, None, &[(name, resolution)]);
|
self.def_collector.update(self.module_id, None, &[(name, resolution)]);
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
@ -734,63 +731,51 @@ where
|
||||||
|
|
||||||
let name = def.name.clone();
|
let name = def.name.clone();
|
||||||
let container = ContainerId::ModuleId(module);
|
let container = ContainerId::ModuleId(module);
|
||||||
let def: PerNs = match def.kind {
|
let def: ModuleDefId = match def.kind {
|
||||||
raw::DefKind::Function(ast_id) => {
|
raw::DefKind::Function(ast_id) => FunctionLoc {
|
||||||
let def = FunctionLoc {
|
container: container.into(),
|
||||||
container: container.into(),
|
ast_id: AstId::new(self.file_id, ast_id),
|
||||||
ast_id: AstId::new(self.file_id, ast_id),
|
|
||||||
}
|
|
||||||
.intern(self.def_collector.db);
|
|
||||||
|
|
||||||
PerNs::values(def.into())
|
|
||||||
}
|
}
|
||||||
|
.intern(self.def_collector.db)
|
||||||
|
.into(),
|
||||||
raw::DefKind::Struct(ast_id) => {
|
raw::DefKind::Struct(ast_id) => {
|
||||||
let def = StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
||||||
.intern(self.def_collector.db);
|
.intern(self.def_collector.db)
|
||||||
PerNs::both(def.into(), def.into())
|
.into()
|
||||||
}
|
}
|
||||||
raw::DefKind::Union(ast_id) => {
|
raw::DefKind::Union(ast_id) => {
|
||||||
let def = UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
||||||
.intern(self.def_collector.db);
|
.intern(self.def_collector.db)
|
||||||
PerNs::both(def.into(), def.into())
|
.into()
|
||||||
}
|
}
|
||||||
raw::DefKind::Enum(ast_id) => {
|
raw::DefKind::Enum(ast_id) => {
|
||||||
let def = EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
||||||
.intern(self.def_collector.db);
|
.intern(self.def_collector.db)
|
||||||
PerNs::types(def.into())
|
.into()
|
||||||
}
|
}
|
||||||
raw::DefKind::Const(ast_id) => {
|
raw::DefKind::Const(ast_id) => {
|
||||||
let def = ConstLoc {
|
ConstLoc { container: container.into(), ast_id: AstId::new(self.file_id, ast_id) }
|
||||||
container: container.into(),
|
.intern(self.def_collector.db)
|
||||||
ast_id: AstId::new(self.file_id, ast_id),
|
.into()
|
||||||
}
|
|
||||||
.intern(self.def_collector.db);
|
|
||||||
|
|
||||||
PerNs::values(def.into())
|
|
||||||
}
|
}
|
||||||
raw::DefKind::Static(ast_id) => {
|
raw::DefKind::Static(ast_id) => {
|
||||||
let def = StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
||||||
.intern(self.def_collector.db);
|
.intern(self.def_collector.db)
|
||||||
|
.into()
|
||||||
PerNs::values(def.into())
|
|
||||||
}
|
}
|
||||||
raw::DefKind::Trait(ast_id) => {
|
raw::DefKind::Trait(ast_id) => {
|
||||||
let def = TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
|
||||||
.intern(self.def_collector.db);
|
.intern(self.def_collector.db)
|
||||||
|
.into()
|
||||||
PerNs::types(def.into())
|
|
||||||
}
|
}
|
||||||
raw::DefKind::TypeAlias(ast_id) => {
|
raw::DefKind::TypeAlias(ast_id) => TypeAliasLoc {
|
||||||
let def = TypeAliasLoc {
|
container: container.into(),
|
||||||
container: container.into(),
|
ast_id: AstId::new(self.file_id, ast_id),
|
||||||
ast_id: AstId::new(self.file_id, ast_id),
|
|
||||||
}
|
|
||||||
.intern(self.def_collector.db);
|
|
||||||
|
|
||||||
PerNs::types(def.into())
|
|
||||||
}
|
}
|
||||||
|
.intern(self.def_collector.db)
|
||||||
|
.into(),
|
||||||
};
|
};
|
||||||
let resolution = Resolution { def, import: false };
|
let resolution = Resolution { def: def.into(), import: false };
|
||||||
self.def_collector.update(self.module_id, None, &[(name, resolution)])
|
self.def_collector.update(self.module_id, None, &[(name, resolution)])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue