Skip to content

Commit b885e6b

Browse files
Delete ContainerId
1 parent 9a5c72d commit b885e6b

File tree

11 files changed

+61
-100
lines changed

11 files changed

+61
-100
lines changed

crates/hir/src/lib.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ pub struct Struct {
535535

536536
impl Struct {
537537
pub fn module(self, db: &dyn HirDatabase) -> Module {
538-
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
538+
Module { id: self.id.lookup(db.upcast()).container }
539539
}
540540

541541
pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
@@ -556,11 +556,7 @@ impl Struct {
556556
}
557557

558558
pub fn ty(self, db: &dyn HirDatabase) -> Type {
559-
Type::from_def(
560-
db,
561-
self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
562-
self.id,
563-
)
559+
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
564560
}
565561

566562
pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> {
@@ -587,15 +583,11 @@ impl Union {
587583
}
588584

589585
pub fn module(self, db: &dyn HirDatabase) -> Module {
590-
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
586+
Module { id: self.id.lookup(db.upcast()).container }
591587
}
592588

593589
pub fn ty(self, db: &dyn HirDatabase) -> Type {
594-
Type::from_def(
595-
db,
596-
self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
597-
self.id,
598-
)
590+
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
599591
}
600592

601593
pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
@@ -619,7 +611,7 @@ pub struct Enum {
619611

620612
impl Enum {
621613
pub fn module(self, db: &dyn HirDatabase) -> Module {
622-
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
614+
Module { id: self.id.lookup(db.upcast()).container }
623615
}
624616

625617
pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
@@ -635,11 +627,7 @@ impl Enum {
635627
}
636628

637629
pub fn ty(self, db: &dyn HirDatabase) -> Type {
638-
Type::from_def(
639-
db,
640-
self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
641-
self.id,
642-
)
630+
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
643631
}
644632
}
645633

@@ -1001,7 +989,7 @@ pub struct Trait {
1001989

1002990
impl Trait {
1003991
pub fn module(self, db: &dyn HirDatabase) -> Module {
1004-
Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
992+
Module { id: self.id.lookup(db.upcast()).container }
1005993
}
1006994

1007995
pub fn name(self, db: &dyn HirDatabase) -> Name {
@@ -1510,7 +1498,7 @@ impl Impl {
15101498
pub fn target_ty(self, db: &dyn HirDatabase) -> Type {
15111499
let impl_data = db.impl_data(self.id);
15121500
let resolver = self.id.resolver(db.upcast());
1513-
let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate();
1501+
let krate = self.id.lookup(db.upcast()).container.krate();
15141502
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
15151503
let ty = Ty::from_hir(&ctx, &impl_data.target_type);
15161504
Type::new_with_resolver_inner(db, krate, &resolver, ty)
@@ -1525,7 +1513,7 @@ impl Impl {
15251513
}
15261514

15271515
pub fn module(self, db: &dyn HirDatabase) -> Module {
1528-
self.id.lookup(db.upcast()).container.module(db.upcast()).into()
1516+
self.id.lookup(db.upcast()).container.into()
15291517
}
15301518

15311519
pub fn krate(self, db: &dyn HirDatabase) -> Crate {

crates/hir_def/src/adt.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use crate::{
2121
trace::Trace,
2222
type_ref::TypeRef,
2323
visibility::RawVisibility,
24-
EnumId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StructId, UnionId,
25-
VariantId,
24+
EnumId, LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StructId, UnionId, VariantId,
2625
};
2726
use cfg::CfgOptions;
2827

@@ -92,10 +91,10 @@ fn parse_repr_tt(tt: &Subtree) -> Option<ReprKind> {
9291
impl StructData {
9392
pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc<StructData> {
9493
let loc = id.lookup(db);
95-
let krate = loc.container.module(db).krate;
94+
let krate = loc.container.krate;
9695
let item_tree = db.item_tree(loc.id.file_id);
9796
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into());
98-
let cfg_options = db.crate_graph()[loc.container.module(db).krate].cfg_options.clone();
97+
let cfg_options = db.crate_graph()[loc.container.krate].cfg_options.clone();
9998

10099
let strukt = &item_tree[loc.id.value];
101100
let variant_data = lower_fields(db, krate, &item_tree, &cfg_options, &strukt.fields, None);
@@ -107,10 +106,10 @@ impl StructData {
107106
}
108107
pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> {
109108
let loc = id.lookup(db);
110-
let krate = loc.container.module(db).krate;
109+
let krate = loc.container.krate;
111110
let item_tree = db.item_tree(loc.id.file_id);
112111
let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into());
113-
let cfg_options = db.crate_graph()[loc.container.module(db).krate].cfg_options.clone();
112+
let cfg_options = db.crate_graph()[loc.container.krate].cfg_options.clone();
114113

115114
let union = &item_tree[loc.id.value];
116115
let variant_data = lower_fields(db, krate, &item_tree, &cfg_options, &union.fields, None);
@@ -126,7 +125,7 @@ impl StructData {
126125
impl EnumData {
127126
pub(crate) fn enum_data_query(db: &dyn DefDatabase, e: EnumId) -> Arc<EnumData> {
128127
let loc = e.lookup(db);
129-
let krate = loc.container.module(db).krate;
128+
let krate = loc.container.krate;
130129
let item_tree = db.item_tree(loc.id.file_id);
131130
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
132131

@@ -168,7 +167,7 @@ impl HasChildSource<LocalEnumVariantId> for EnumId {
168167
) -> InFile<ArenaMap<LocalEnumVariantId, Self::Value>> {
169168
let src = self.lookup(db).source(db);
170169
let mut trace = Trace::new_for_map();
171-
lower_enum(db, &mut trace, &src, self.lookup(db).container.module(db));
170+
lower_enum(db, &mut trace, &src, self.lookup(db).container);
172171
src.with_value(trace.into_map())
173172
}
174173
}
@@ -238,18 +237,18 @@ impl HasChildSource<LocalFieldId> for VariantId {
238237
// I don't really like the fact that we call into parent source
239238
// here, this might add to more queries then necessary.
240239
let src = it.parent.child_source(db);
241-
(src.map(|map| map[it.local_id].kind()), it.parent.lookup(db).container.module(db))
240+
(src.map(|map| map[it.local_id].kind()), it.parent.lookup(db).container)
242241
}
243242
VariantId::StructId(it) => {
244-
(it.lookup(db).source(db).map(|it| it.kind()), it.lookup(db).container.module(db))
243+
(it.lookup(db).source(db).map(|it| it.kind()), it.lookup(db).container)
245244
}
246245
VariantId::UnionId(it) => (
247246
it.lookup(db).source(db).map(|it| {
248247
it.record_field_list()
249248
.map(ast::StructKind::Record)
250249
.unwrap_or(ast::StructKind::Unit)
251250
}),
252-
it.lookup(db).container.module(db),
251+
it.lookup(db).container,
253252
),
254253
};
255254
let mut expander = CfgExpander::new(db, src.file_id, module_id.krate);

crates/hir_def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl Attrs {
267267
db: &dyn DefDatabase,
268268
e: EnumId,
269269
) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>> {
270-
let krate = e.lookup(db).container.module(db).krate;
270+
let krate = e.lookup(db).container.krate;
271271
let src = e.child_source(db);
272272
let mut res = ArenaMap::default();
273273

crates/hir_def/src/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl TraitData {
9797
let tr_def = &item_tree[tr_loc.id.value];
9898
let name = tr_def.name.clone();
9999
let auto = tr_def.auto;
100-
let module_id = tr_loc.container.module(db);
100+
let module_id = tr_loc.container;
101101
let container = AssocContainerId::TraitId(tr);
102102
let mut expander = Expander::new(db, tr_loc.id.file_id, module_id);
103103

@@ -147,7 +147,7 @@ impl ImplData {
147147
let target_trait = impl_def.target_trait.map(|id| item_tree[id].clone());
148148
let target_type = item_tree[impl_def.target_type].clone();
149149
let is_negative = impl_def.is_negative;
150-
let module_id = impl_loc.container.module(db);
150+
let module_id = impl_loc.container;
151151
let container = AssocContainerId::ImplId(id);
152152
let mut expander = Expander::new(db, impl_loc.id.file_id, module_id);
153153

crates/hir_def/src/lib.rs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub type LocalModuleId = Idx<nameres::ModuleData>;
108108

109109
#[derive(Debug)]
110110
pub struct ItemLoc<N: ItemTreeNode> {
111-
pub container: ContainerId,
111+
pub container: ModuleId,
112112
pub id: ItemTreeId<N>,
113113
}
114114

@@ -278,12 +278,6 @@ pub struct ConstParamId {
278278
}
279279
pub type LocalConstParamId = Idx<generics::ConstParamData>;
280280

281-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
282-
pub enum ContainerId {
283-
ModuleId(ModuleId),
284-
DefWithBodyId(DefWithBodyId),
285-
}
286-
287281
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
288282
pub enum AssocContainerId {
289283
ModuleId(ModuleId),
@@ -447,21 +441,12 @@ pub trait HasModule {
447441
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
448442
}
449443

450-
impl HasModule for ContainerId {
451-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
452-
match *self {
453-
ContainerId::ModuleId(it) => it,
454-
ContainerId::DefWithBodyId(it) => it.module(db),
455-
}
456-
}
457-
}
458-
459444
impl HasModule for AssocContainerId {
460445
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
461446
match *self {
462447
AssocContainerId::ModuleId(it) => it,
463-
AssocContainerId::ImplId(it) => it.lookup(db).container.module(db),
464-
AssocContainerId::TraitId(it) => it.lookup(db).container.module(db),
448+
AssocContainerId::ImplId(it) => it.lookup(db).container,
449+
AssocContainerId::TraitId(it) => it.lookup(db).container,
465450
}
466451
}
467452
}
@@ -479,16 +464,15 @@ impl HasModule for AdtId {
479464
AdtId::UnionId(it) => it.lookup(db).container,
480465
AdtId::EnumId(it) => it.lookup(db).container,
481466
}
482-
.module(db)
483467
}
484468
}
485469

486470
impl HasModule for VariantId {
487471
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
488472
match self {
489-
VariantId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
490-
VariantId::StructId(it) => it.lookup(db).container.module(db),
491-
VariantId::UnionId(it) => it.lookup(db).container.module(db),
473+
VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
474+
VariantId::StructId(it) => it.lookup(db).container,
475+
VariantId::UnionId(it) => it.lookup(db).container,
492476
}
493477
}
494478
}
@@ -518,18 +502,18 @@ impl HasModule for GenericDefId {
518502
match self {
519503
GenericDefId::FunctionId(it) => it.lookup(db).module(db),
520504
GenericDefId::AdtId(it) => it.module(db),
521-
GenericDefId::TraitId(it) => it.lookup(db).container.module(db),
505+
GenericDefId::TraitId(it) => it.lookup(db).container,
522506
GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
523-
GenericDefId::ImplId(it) => it.lookup(db).container.module(db),
524-
GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
507+
GenericDefId::ImplId(it) => it.lookup(db).container,
508+
GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
525509
GenericDefId::ConstId(it) => it.lookup(db).module(db),
526510
}
527511
}
528512
}
529513

530514
impl HasModule for StaticLoc {
531-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
532-
self.container.module(db)
515+
fn module(&self, _db: &dyn db::DefDatabase) -> ModuleId {
516+
self.container
533517
}
534518
}
535519

@@ -542,10 +526,10 @@ impl ModuleDefId {
542526
ModuleDefId::ModuleId(id) => *id,
543527
ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
544528
ModuleDefId::AdtId(id) => id.module(db),
545-
ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container.module(db),
529+
ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container,
546530
ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
547-
ModuleDefId::StaticId(id) => id.lookup(db).container.module(db),
548-
ModuleDefId::TraitId(id) => id.lookup(db).container.module(db),
531+
ModuleDefId::StaticId(id) => id.lookup(db).container,
532+
ModuleDefId::TraitId(id) => id.lookup(db).container,
549533
ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db),
550534
ModuleDefId::BuiltinType(_) => return None,
551535
})
@@ -559,12 +543,12 @@ impl AttrDefId {
559543
AttrDefId::FieldId(it) => it.parent.module(db).krate,
560544
AttrDefId::AdtId(it) => it.module(db).krate,
561545
AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
562-
AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db).krate,
546+
AttrDefId::EnumVariantId(it) => it.parent.lookup(db).container.krate,
563547
AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
564548
AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
565-
AttrDefId::TraitId(it) => it.lookup(db).container.module(db).krate,
549+
AttrDefId::TraitId(it) => it.lookup(db).container.krate,
566550
AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
567-
AttrDefId::ImplId(it) => it.lookup(db).container.module(db).krate,
551+
AttrDefId::ImplId(it) => it.lookup(db).container.krate,
568552
AttrDefId::GenericParamId(it) => {
569553
match it {
570554
GenericParamId::TypeParamId(it) => it.parent,

crates/hir_def/src/nameres/collector.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ use crate::{
3737
path::{ImportAlias, ModPath, PathKind},
3838
per_ns::PerNs,
3939
visibility::{RawVisibility, Visibility},
40-
AdtId, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc,
41-
ImplLoc, Intern, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc,
42-
UnionLoc, UnresolvedMacro,
40+
AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
41+
LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
42+
UnresolvedMacro,
4343
};
4444

4545
const GLOB_RECURSION_LIMIT: usize = 100;
@@ -1042,7 +1042,6 @@ impl ModCollector<'_, '_> {
10421042
}
10431043
}
10441044
let module = self.def_collector.def_map.module_id(self.module_id);
1045-
let container = ContainerId::ModuleId(module);
10461045

10471046
let mut def = None;
10481047
match item {
@@ -1109,9 +1108,9 @@ impl ModCollector<'_, '_> {
11091108
}
11101109
ModItem::Impl(imp) => {
11111110
let module = self.def_collector.def_map.module_id(self.module_id);
1112-
let container = ContainerId::ModuleId(module);
1113-
let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) }
1114-
.intern(self.def_collector.db);
1111+
let impl_id =
1112+
ImplLoc { container: module, id: ItemTreeId::new(self.file_id, imp) }
1113+
.intern(self.def_collector.db);
11151114
self.def_collector.def_map.modules[self.module_id].scope.define_impl(impl_id)
11161115
}
11171116
ModItem::Function(id) => {
@@ -1140,7 +1139,7 @@ impl ModCollector<'_, '_> {
11401139
self.collect_derives(&attrs, it.ast_id.upcast());
11411140

11421141
def = Some(DefData {
1143-
id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) }
1142+
id: StructLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
11441143
.intern(self.def_collector.db)
11451144
.into(),
11461145
name: &it.name,
@@ -1157,7 +1156,7 @@ impl ModCollector<'_, '_> {
11571156
self.collect_derives(&attrs, it.ast_id.upcast());
11581157

11591158
def = Some(DefData {
1160-
id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) }
1159+
id: UnionLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
11611160
.intern(self.def_collector.db)
11621161
.into(),
11631162
name: &it.name,
@@ -1174,7 +1173,7 @@ impl ModCollector<'_, '_> {
11741173
self.collect_derives(&attrs, it.ast_id.upcast());
11751174

11761175
def = Some(DefData {
1177-
id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) }
1176+
id: EnumLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
11781177
.intern(self.def_collector.db)
11791178
.into(),
11801179
name: &it.name,
@@ -1203,7 +1202,7 @@ impl ModCollector<'_, '_> {
12031202
let it = &self.item_tree[id];
12041203

12051204
def = Some(DefData {
1206-
id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) }
1205+
id: StaticLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
12071206
.intern(self.def_collector.db)
12081207
.into(),
12091208
name: &it.name,
@@ -1215,7 +1214,7 @@ impl ModCollector<'_, '_> {
12151214
let it = &self.item_tree[id];
12161215

12171216
def = Some(DefData {
1218-
id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) }
1217+
id: TraitLoc { container: module, id: ItemTreeId::new(self.file_id, id) }
12191218
.intern(self.def_collector.db)
12201219
.into(),
12211220
name: &it.name,

0 commit comments

Comments
 (0)