Next gen IDs for functions

The current system with AstIds has two primaraly drawbacks:

* It is possible to manufacture IDs out of thin air.
  For example, it's possible to create IDs for items which are not
  considered in CrateDefMap due to cfg. Or it is possible to mixup
  structs and unions, because they share ID space.

* Getting the ID of a parent requires a secondary index.

Instead, the plan is to pursue the more traditional approach, where
each items stores the id of the parent declaration. This makes
`FromSource` more awkward, but also more correct: now, to get from an
AST to HIR, we first do this recursively for the parent item, and the
just search the children of the parent for the matching def
This commit is contained in:
Aleksey Kladov 2019-11-20 16:03:59 +03:00
parent 06fa3d8389
commit cebeedc66f
12 changed files with 190 additions and 42 deletions

View file

@ -12,7 +12,8 @@ use hir_def::{
builtin_type::BuiltinType,
traits::TraitData,
type_ref::{Mutability, TypeRef},
AssocItemId, CrateModuleId, ImplId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId,
AssocItemId, CrateModuleId, FunctionContainerId, HasModule, ImplId, LocalEnumVariantId,
LocalStructFieldId, Lookup, ModuleId, UnionId,
};
use hir_expand::{
diagnostics::DiagnosticSink,
@ -647,7 +648,7 @@ impl FnData {
impl Function {
pub fn module(self, db: &impl DefDatabase) -> Module {
Module { id: self.id.module(db) }
self.id.lookup(db).module(db).into()
}
pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@ -680,21 +681,25 @@ impl Function {
/// The containing impl block, if this is a method.
pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
ImplBlock::containing(db, self.into())
match self.container(db) {
Some(Container::ImplBlock(it)) => Some(it),
_ => None,
}
}
/// The containing trait, if this is a trait method definition.
pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from)
match self.container(db) {
Some(Container::Trait(it)) => Some(it),
_ => None,
}
}
pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
if let Some(impl_block) = self.impl_block(db) {
Some(impl_block.into())
} else if let Some(trait_) = self.parent_trait(db) {
Some(trait_.into())
} else {
None
match self.id.lookup(db).container {
FunctionContainerId::TraitId(it) => Some(Container::Trait(it.into())),
FunctionContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
FunctionContainerId::ModuleId(_) => None,
}
}