From b885e6bdeeb1023df53b3f292cfe301448eeaeef Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 9 Mar 2021 19:09:02 +0100 Subject: [PATCH] Delete `ContainerId` --- crates/hir/src/lib.rs | 30 +++++---------- crates/hir_def/src/adt.rs | 21 +++++------ crates/hir_def/src/attr.rs | 2 +- crates/hir_def/src/data.rs | 4 +- crates/hir_def/src/lib.rs | 50 +++++++++---------------- crates/hir_def/src/nameres/collector.rs | 23 ++++++------ crates/hir_def/src/resolver.rs | 17 ++------- crates/hir_ty/src/display.rs | 4 +- crates/hir_ty/src/lower.rs | 4 +- crates/hir_ty/src/method_resolution.rs | 2 +- crates/hir_ty/src/traits/chalk.rs | 4 +- 11 files changed, 61 insertions(+), 100 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index c6d424c487..4ef38c0f0d 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/crates/hir_def/src/adt.rs b/crates/hir_def/src/adt.rs index ed36c31096..efbde17d88 100644 --- a/crates/hir_def/src/adt.rs +++ b/crates/hir_def/src/adt.rs @@ -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 { impl StructData { pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc { 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 { 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 { 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 for EnumId { ) -> InFile> { 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 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 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); diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index b716d5f6e8..97cdbbb9e9 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -267,7 +267,7 @@ impl Attrs { db: &dyn DefDatabase, e: EnumId, ) -> Arc> { - 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(); diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index d3380e0f44..aea53d527e 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs @@ -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); diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index ab3b17f6cd..6d11c5be41 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -108,7 +108,7 @@ pub type LocalModuleId = Idx; #[derive(Debug)] pub struct ItemLoc { - pub container: ContainerId, + pub container: ModuleId, pub id: ItemTreeId, } @@ -278,12 +278,6 @@ pub struct ConstParamId { } pub type LocalConstParamId = Idx; -#[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, diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 5bf2ba721b..9ed48c506c 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -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, diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index 6f036c8c42..42736171eb 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -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 { diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index a0882a2a10..ab51cb0a6e 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -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 { - 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()), diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index b90fdc3820..5fa83567b3 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -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() } diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 2483672674..ccc12c0757 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -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) } diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 4378a9723c..565672b6bf 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -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