Introduce ContainerId

This commit is contained in:
Aleksey Kladov 2019-12-20 12:07:23 +01:00
parent 8fc20b6503
commit 94ad07af4b
7 changed files with 42 additions and 19 deletions

View file

@ -25,7 +25,7 @@ use crate::{
path::GenericArgs, path::GenericArgs,
path::Path, path::Path,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AssocContainerId, DefWithBodyId, FunctionLoc, Intern, ContainerId, DefWithBodyId, FunctionLoc, Intern,
}; };
pub(super) fn lower( pub(super) fn lower(
@ -490,7 +490,7 @@ where
} }
fn collect_block_items(&mut self, block: &ast::Block) { fn collect_block_items(&mut self, block: &ast::Block) {
let container = AssocContainerId::DefWithBodyId(self.def); let container = ContainerId::DefWithBodyId(self.def).into();
for item in block.items() { for item in block.items() {
match item { match item {
ast::ModuleItem::FnDef(def) => { ast::ModuleItem::FnDef(def) => {

View file

@ -331,13 +331,19 @@ pub struct LocalTypeParamId(RawId);
impl_arena_id!(LocalTypeParamId); impl_arena_id!(LocalTypeParamId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AssocContainerId { pub enum ContainerId {
ModuleId(ModuleId), ModuleId(ModuleId),
ImplId(ImplId),
TraitId(TraitId),
DefWithBodyId(DefWithBodyId), DefWithBodyId(DefWithBodyId),
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AssocContainerId {
ContainerId(ContainerId),
ImplId(ImplId),
TraitId(TraitId),
}
impl_froms!(AssocContainerId: ContainerId);
/// A Data Type /// A Data Type
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AdtId { pub enum AdtId {
@ -479,13 +485,21 @@ pub trait HasModule {
fn module(&self, db: &impl db::DefDatabase) -> ModuleId; fn module(&self, db: &impl db::DefDatabase) -> ModuleId;
} }
impl HasModule for ContainerId {
fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
match *self {
ContainerId::ModuleId(it) => it,
ContainerId::DefWithBodyId(it) => it.module(db),
}
}
}
impl HasModule for AssocContainerId { impl HasModule for AssocContainerId {
fn module(&self, db: &impl db::DefDatabase) -> ModuleId { fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
match *self { match *self {
AssocContainerId::ModuleId(it) => it, AssocContainerId::ContainerId(it) => it.module(db),
AssocContainerId::ImplId(it) => it.lookup(db).container, AssocContainerId::ImplId(it) => it.lookup(db).container,
AssocContainerId::TraitId(it) => it.lookup(db).container, AssocContainerId::TraitId(it) => it.lookup(db).container,
AssocContainerId::DefWithBodyId(it) => it.module(db),
} }
} }
} }

View file

@ -24,7 +24,7 @@ use crate::{
}, },
path::{ModPath, PathKind}, path::{ModPath, PathKind},
per_ns::PerNs, per_ns::PerNs,
AdtId, AssocContainerId, AstId, ConstLoc, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc,
TypeAliasLoc, UnionLoc, TypeAliasLoc, UnionLoc,
}; };
@ -760,10 +760,11 @@ where
self.collect_derives(attrs, def); self.collect_derives(attrs, def);
let name = def.name.clone(); let name = def.name.clone();
let container = ContainerId::ModuleId(module);
let def: PerNs = match def.kind { let def: PerNs = match def.kind {
raw::DefKind::Function(ast_id) => { raw::DefKind::Function(ast_id) => {
let def = FunctionLoc { let def = FunctionLoc {
container: AssocContainerId::ModuleId(module), 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); .intern(self.def_collector.db);
@ -787,7 +788,7 @@ where
} }
raw::DefKind::Const(ast_id) => { raw::DefKind::Const(ast_id) => {
let def = ConstLoc { let def = ConstLoc {
container: AssocContainerId::ModuleId(module), 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); .intern(self.def_collector.db);
@ -808,7 +809,7 @@ where
} }
raw::DefKind::TypeAlias(ast_id) => { raw::DefKind::TypeAlias(ast_id) => {
let def = TypeAliasLoc { let def = TypeAliasLoc {
container: AssocContainerId::ModuleId(module), 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); .intern(self.def_collector.db);

View file

@ -17,9 +17,9 @@ use crate::{
nameres::{BuiltinShadowMode, CrateDefMap}, nameres::{BuiltinShadowMode, CrateDefMap},
path::{ModPath, PathKind}, path::{ModPath, PathKind},
per_ns::PerNs, per_ns::PerNs,
AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, AdtId, AssocContainerId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId,
GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId,
StructId, TraitId, TypeAliasId, TypeParamId, VariantId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
}; };
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -580,13 +580,21 @@ impl HasResolver for DefWithBodyId {
} }
} }
impl HasResolver for ContainerId {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
match self {
ContainerId::ModuleId(it) => it.resolver(db),
ContainerId::DefWithBodyId(it) => it.resolver(db),
}
}
}
impl HasResolver for AssocContainerId { impl HasResolver for AssocContainerId {
fn resolver(self, db: &impl DefDatabase) -> Resolver { fn resolver(self, db: &impl DefDatabase) -> Resolver {
match self { match self {
AssocContainerId::ContainerId(it) => it.resolver(db),
AssocContainerId::TraitId(it) => it.resolver(db), AssocContainerId::TraitId(it) => it.resolver(db),
AssocContainerId::ImplId(it) => it.resolver(db), AssocContainerId::ImplId(it) => it.resolver(db),
AssocContainerId::ModuleId(it) => it.resolver(db),
AssocContainerId::DefWithBodyId(it) => it.resolver(db),
} }
} }
} }

View file

@ -237,7 +237,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
})); }));
Some(substs) Some(substs)
} }
AssocContainerId::ModuleId(_) | AssocContainerId::DefWithBodyId(_) => None, AssocContainerId::ContainerId(_) => None,
}; };
self.write_assoc_resolution(id, item.into()); self.write_assoc_resolution(id, item.into());

View file

@ -456,7 +456,7 @@ fn transform_receiver_ty(
.fill_with_unknown() .fill_with_unknown()
.build(), .build(),
AssocContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?, AssocContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?,
AssocContainerId::ModuleId(_) | AssocContainerId::DefWithBodyId(_) => unreachable!(), AssocContainerId::ContainerId(_) => unreachable!(),
}; };
let sig = db.callable_item_signature(function_id.into()); let sig = db.callable_item_signature(function_id.into());
Some(sig.params()[0].clone().subst(&substs)) Some(sig.params()[0].clone().subst(&substs))

View file

@ -157,6 +157,6 @@ fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option<Generi
match container { match container {
AssocContainerId::ImplId(it) => Some(it.into()), AssocContainerId::ImplId(it) => Some(it.into()),
AssocContainerId::TraitId(it) => Some(it.into()), AssocContainerId::TraitId(it) => Some(it.into()),
AssocContainerId::ModuleId(_) | AssocContainerId::DefWithBodyId(_) => None, AssocContainerId::ContainerId(_) => None,
} }
} }