Delete ContainerId

This commit is contained in:
Jonas Schievink 2021-03-09 19:09:02 +01:00
parent 9a5c72d9f0
commit b885e6bdee
11 changed files with 61 additions and 100 deletions

View File

@ -535,7 +535,7 @@ pub struct Struct {
impl Struct {
pub fn module(self, db: &dyn HirDatabase) -> Module {
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
Module { id: self.id.lookup(db.upcast()).container }
}
pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
@ -556,11 +556,7 @@ impl Struct {
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
Type::from_def(
db,
self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
self.id,
)
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
}
pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> {
@ -587,15 +583,11 @@ impl Union {
}
pub fn module(self, db: &dyn HirDatabase) -> Module {
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
Module { id: self.id.lookup(db.upcast()).container }
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
Type::from_def(
db,
self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
self.id,
)
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
}
pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
@ -619,7 +611,7 @@ pub struct Enum {
impl Enum {
pub fn module(self, db: &dyn HirDatabase) -> Module {
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
Module { id: self.id.lookup(db.upcast()).container }
}
pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
@ -635,11 +627,7 @@ impl Enum {
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
Type::from_def(
db,
self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
self.id,
)
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
}
}
@ -1001,7 +989,7 @@ pub struct Trait {
impl Trait {
pub fn module(self, db: &dyn HirDatabase) -> Module {
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
Module { id: self.id.lookup(db.upcast()).container }
}
pub fn name(self, db: &dyn HirDatabase) -> Name {
@ -1510,7 +1498,7 @@ impl Impl {
pub fn target_ty(self, db: &dyn HirDatabase) -> Type {
let impl_data = db.impl_data(self.id);
let resolver = self.id.resolver(db.upcast());
let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate();
let krate = self.id.lookup(db.upcast()).container.krate();
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let ty = Ty::from_hir(&ctx, &impl_data.target_type);
Type::new_with_resolver_inner(db, krate, &resolver, ty)
@ -1525,7 +1513,7 @@ impl Impl {
}
pub fn module(self, db: &dyn HirDatabase) -> Module {
self.id.lookup(db.upcast()).container.module(db.upcast()).into()
self.id.lookup(db.upcast()).container.into()
}
pub fn krate(self, db: &dyn HirDatabase) -> Crate {

View File

@ -21,8 +21,7 @@ use crate::{
trace::Trace,
type_ref::TypeRef,
visibility::RawVisibility,
EnumId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StructId, UnionId,
VariantId,
EnumId, LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StructId, UnionId, VariantId,
};
use cfg::CfgOptions;
@ -92,10 +91,10 @@ fn parse_repr_tt(tt: &Subtree) -> Option<ReprKind> {
impl StructData {
pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc<StructData> {
let loc = id.lookup(db);
let krate = loc.container.module(db).krate;
let krate = loc.container.krate;
let item_tree = db.item_tree(loc.id.file_id);
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into());
let cfg_options = db.crate_graph()[loc.container.module(db).krate].cfg_options.clone();
let cfg_options = db.crate_graph()[loc.container.krate].cfg_options.clone();
let strukt = &item_tree[loc.id.value];
let variant_data = lower_fields(db, krate, &item_tree, &cfg_options, &strukt.fields, None);
@ -107,10 +106,10 @@ impl StructData {
}
pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> {
let loc = id.lookup(db);
let krate = loc.container.module(db).krate;
let krate = loc.container.krate;
let item_tree = db.item_tree(loc.id.file_id);
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into());
let cfg_options = db.crate_graph()[loc.container.module(db).krate].cfg_options.clone();
let cfg_options = db.crate_graph()[loc.container.krate].cfg_options.clone();
let union = &item_tree[loc.id.value];
let variant_data = lower_fields(db, krate, &item_tree, &cfg_options, &union.fields, None);
@ -126,7 +125,7 @@ impl StructData {
impl EnumData {
pub(crate) fn enum_data_query(db: &dyn DefDatabase, e: EnumId) -> Arc<EnumData> {
let loc = e.lookup(db);
let krate = loc.container.module(db).krate;
let krate = loc.container.krate;
let item_tree = db.item_tree(loc.id.file_id);
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
@ -168,7 +167,7 @@ impl HasChildSource<LocalEnumVariantId> for EnumId {
) -> InFile<ArenaMap<LocalEnumVariantId, Self::Value>> {
let src = self.lookup(db).source(db);
let mut trace = Trace::new_for_map();
lower_enum(db, &mut trace, &src, self.lookup(db).container.module(db));
lower_enum(db, &mut trace, &src, self.lookup(db).container);
src.with_value(trace.into_map())
}
}
@ -238,10 +237,10 @@ impl HasChildSource<LocalFieldId> for VariantId {
// I don't really like the fact that we call into parent source
// here, this might add to more queries then necessary.
let src = it.parent.child_source(db);
(src.map(|map| map[it.local_id].kind()), it.parent.lookup(db).container.module(db))
(src.map(|map| map[it.local_id].kind()), it.parent.lookup(db).container)
}
VariantId::StructId(it) => {
(it.lookup(db).source(db).map(|it| it.kind()), it.lookup(db).container.module(db))
(it.lookup(db).source(db).map(|it| it.kind()), it.lookup(db).container)
}
VariantId::UnionId(it) => (
it.lookup(db).source(db).map(|it| {
@ -249,7 +248,7 @@ impl HasChildSource<LocalFieldId> for VariantId {
.map(ast::StructKind::Record)
.unwrap_or(ast::StructKind::Unit)
}),
it.lookup(db).container.module(db),
it.lookup(db).container,
),
};
let mut expander = CfgExpander::new(db, src.file_id, module_id.krate);

View File

@ -267,7 +267,7 @@ impl Attrs {
db: &dyn DefDatabase,
e: EnumId,
) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>> {
let krate = e.lookup(db).container.module(db).krate;
let krate = e.lookup(db).container.krate;
let src = e.child_source(db);
let mut res = ArenaMap::default();

View File

@ -97,7 +97,7 @@ impl TraitData {
let tr_def = &item_tree[tr_loc.id.value];
let name = tr_def.name.clone();
let auto = tr_def.auto;
let module_id = tr_loc.container.module(db);
let module_id = tr_loc.container;
let container = AssocContainerId::TraitId(tr);
let mut expander = Expander::new(db, tr_loc.id.file_id, module_id);
@ -147,7 +147,7 @@ impl ImplData {
let target_trait = impl_def.target_trait.map(|id| item_tree[id].clone());
let target_type = item_tree[impl_def.target_type].clone();
let is_negative = impl_def.is_negative;
let module_id = impl_loc.container.module(db);
let module_id = impl_loc.container;
let container = AssocContainerId::ImplId(id);
let mut expander = Expander::new(db, impl_loc.id.file_id, module_id);

View File

@ -108,7 +108,7 @@ pub type LocalModuleId = Idx<nameres::ModuleData>;
#[derive(Debug)]
pub struct ItemLoc<N: ItemTreeNode> {
pub container: ContainerId,
pub container: ModuleId,
pub id: ItemTreeId<N>,
}
@ -278,12 +278,6 @@ pub struct ConstParamId {
}
pub type LocalConstParamId = Idx<generics::ConstParamData>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ContainerId {
ModuleId(ModuleId),
DefWithBodyId(DefWithBodyId),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AssocContainerId {
ModuleId(ModuleId),
@ -447,21 +441,12 @@ pub trait HasModule {
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
}
impl HasModule for ContainerId {
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
match *self {
ContainerId::ModuleId(it) => it,
ContainerId::DefWithBodyId(it) => it.module(db),
}
}
}
impl HasModule for AssocContainerId {
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
match *self {
AssocContainerId::ModuleId(it) => it,
AssocContainerId::ImplId(it) => it.lookup(db).container.module(db),
AssocContainerId::TraitId(it) => it.lookup(db).container.module(db),
AssocContainerId::ImplId(it) => it.lookup(db).container,
AssocContainerId::TraitId(it) => it.lookup(db).container,
}
}
}
@ -479,16 +464,15 @@ impl HasModule for AdtId {
AdtId::UnionId(it) => it.lookup(db).container,
AdtId::EnumId(it) => it.lookup(db).container,
}
.module(db)
}
}
impl HasModule for VariantId {
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
match self {
VariantId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
VariantId::StructId(it) => it.lookup(db).container.module(db),
VariantId::UnionId(it) => it.lookup(db).container.module(db),
VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
VariantId::StructId(it) => it.lookup(db).container,
VariantId::UnionId(it) => it.lookup(db).container,
}
}
}
@ -518,18 +502,18 @@ impl HasModule for GenericDefId {
match self {
GenericDefId::FunctionId(it) => it.lookup(db).module(db),
GenericDefId::AdtId(it) => it.module(db),
GenericDefId::TraitId(it) => it.lookup(db).container.module(db),
GenericDefId::TraitId(it) => it.lookup(db).container,
GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
GenericDefId::ImplId(it) => it.lookup(db).container.module(db),
GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
GenericDefId::ImplId(it) => it.lookup(db).container,
GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
GenericDefId::ConstId(it) => it.lookup(db).module(db),
}
}
}
impl HasModule for StaticLoc {
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
self.container.module(db)
fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
self.container
}
}
@ -542,10 +526,10 @@ impl ModuleDefId {
ModuleDefId::ModuleId(id) => *id,
ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
ModuleDefId::AdtId(id) => id.module(db),
ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container.module(db),
ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
ModuleDefId::StaticId(id) => id.lookup(db).container.module(db),
ModuleDefId::TraitId(id) => id.lookup(db).container.module(db),
ModuleDefId::StaticId(id) => id.lookup(db).container,
ModuleDefId::TraitId(id) => id.lookup(db).container,
ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
ModuleDefId::BuiltinType(_) => return None,
})
@ -559,12 +543,12 @@ impl AttrDefId {
AttrDefId::FieldId(it) => it.parent.module(db).krate,
AttrDefId::AdtId(it) => it.module(db).krate,
AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db).krate,
AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
AttrDefId::TraitId(it) => it.lookup(db).container.module(db).krate,
AttrDefId::TraitId(it) => it.lookup(db).container.krate,
AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
AttrDefId::ImplId(it) => it.lookup(db).container.module(db).krate,
AttrDefId::ImplId(it) => it.lookup(db).container.krate,
AttrDefId::GenericParamId(it) => {
match it {
GenericParamId::TypeParamId(it) => it.parent,

View File

@ -37,9 +37,9 @@ use crate::{
path::{ImportAlias, ModPath, PathKind},
per_ns::PerNs,
visibility::{RawVisibility, Visibility},
AdtId, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc,
ImplLoc, Intern, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc,
UnionLoc, UnresolvedMacro,
AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
UnresolvedMacro,
};
const GLOB_RECURSION_LIMIT: usize = 100;
@ -1042,7 +1042,6 @@ impl ModCollector<'_, '_> {
}
}
let module = self.def_collector.def_map.module_id(self.module_id);
let container = ContainerId::ModuleId(module);
let mut def = None;
match item {
@ -1109,9 +1108,9 @@ impl ModCollector<'_, '_> {
}
ModItem::Impl(imp) => {
let module = self.def_collector.def_map.module_id(self.module_id);
let container = ContainerId::ModuleId(module);
let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) }
.intern(self.def_collector.db);
let impl_id =
ImplLoc { container: module, id: ItemTreeId::new(self.file_id, imp) }
.intern(self.def_collector.db);
self.def_collector.def_map.modules[self.module_id].scope.define_impl(impl_id)
}
ModItem::Function(id) => {
@ -1140,7 +1139,7 @@ impl ModCollector<'_, '_> {
self.collect_derives(&attrs, it.ast_id.upcast());
def = Some(DefData {
id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) }
id: StructLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
@ -1157,7 +1156,7 @@ impl ModCollector<'_, '_> {
self.collect_derives(&attrs, it.ast_id.upcast());
def = Some(DefData {
id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) }
id: UnionLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
@ -1174,7 +1173,7 @@ impl ModCollector<'_, '_> {
self.collect_derives(&attrs, it.ast_id.upcast());
def = Some(DefData {
id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) }
id: EnumLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
@ -1203,7 +1202,7 @@ impl ModCollector<'_, '_> {
let it = &self.item_tree[id];
def = Some(DefData {
id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) }
id: StaticLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,
@ -1215,7 +1214,7 @@ impl ModCollector<'_, '_> {
let it = &self.item_tree[id];
def = Some(DefData {
id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) }
id: TraitLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
.intern(self.def_collector.db)
.into(),
name: &it.name,

View File

@ -19,10 +19,10 @@ use crate::{
path::{ModPath, PathKind},
per_ns::PerNs,
visibility::{RawVisibility, Visibility},
AdtId, AssocContainerId, ConstId, ConstParamId, ContainerId, DefWithBodyId, EnumId,
EnumVariantId, FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId,
LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
TypeParamId, VariantId,
AdtId, AssocContainerId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId,
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId, LocalModuleId,
Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
VariantId,
};
#[derive(Debug, Clone, Default)]
@ -688,15 +688,6 @@ impl HasResolver for DefWithBodyId {
}
}
impl HasResolver for ContainerId {
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
match self {
ContainerId::ModuleId(it) => it.resolver(db),
ContainerId::DefWithBodyId(it) => it.module(db).resolver(db),
}
}
}
impl HasResolver for AssocContainerId {
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
match self {

View File

@ -6,7 +6,7 @@ use arrayvec::ArrayVec;
use chalk_ir::Mutability;
use hir_def::{
db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs,
AssocContainerId, HasModule, Lookup, ModuleId, TraitId,
AssocContainerId, Lookup, ModuleId, TraitId,
};
use hir_expand::name::Name;
@ -611,7 +611,7 @@ impl HirDisplay for CallableSig {
}
fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator<Item = TraitId> {
let krate = trait_.lookup(db).container.module(db).krate();
let krate = trait_.lookup(db).container.krate();
let fn_traits = [
db.lang_item(krate, "fn".into()),
db.lang_item(krate, "fn_mut".into()),

View File

@ -1130,8 +1130,8 @@ impl CallableDefId {
let db = db.upcast();
match self {
CallableDefId::FunctionId(f) => f.lookup(db).module(db),
CallableDefId::StructId(s) => s.lookup(db).container.module(db),
CallableDefId::EnumVariantId(e) => e.parent.lookup(db).container.module(db),
CallableDefId::StructId(s) => s.lookup(db).container,
CallableDefId::EnumVariantId(e) => e.parent.lookup(db).container,
}
.krate()
}

View File

@ -267,7 +267,7 @@ impl Ty {
LangItemTarget::ImplDefId(it) => Some(it),
_ => None,
})
.map(|it| it.lookup(db.upcast()).container.module(db.upcast()).krate())
.map(|it| it.lookup(db.upcast()).container.krate())
.collect();
Some(res)
}

View File

@ -424,7 +424,7 @@ pub(crate) fn trait_datum_query(
let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
let flags = rust_ir::TraitFlags {
auto: trait_data.auto,
upstream: trait_.lookup(db.upcast()).container.module(db.upcast()).krate() != krate,
upstream: trait_.lookup(db.upcast()).container.krate() != krate,
non_enumerable: true,
coinductive: false, // only relevant for Chalk testing
// FIXME: set these flags correctly
@ -548,7 +548,7 @@ fn impl_def_datum(
let generic_params = generics(db.upcast(), impl_id.into());
let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
let trait_ = trait_ref.trait_;
let impl_type = if impl_id.lookup(db.upcast()).container.module(db.upcast()).krate() == krate {
let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate {
rust_ir::ImplType::Local
} else {
rust_ir::ImplType::External