Skip to content

Commit 180e9b2

Browse files
committed
Cleanup
1 parent 1669344 commit 180e9b2

File tree

17 files changed

+175
-168
lines changed

17 files changed

+175
-168
lines changed

crates/hir-def/src/attr.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use triomphe::Arc;
2424

2525
use crate::{
2626
db::DefDatabase,
27-
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeNode},
27+
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeModItemNode},
2828
lang_item::LangItem,
2929
nameres::{ModuleOrigin, ModuleSource},
3030
src::{HasChildSource, HasSource},
@@ -82,7 +82,7 @@ impl Attrs {
8282
let (fields, item_tree, krate) = match v {
8383
VariantId::EnumVariantId(it) => {
8484
let loc = it.lookup(db);
85-
let krate = loc.container.krate;
85+
let krate = loc.parent.lookup(db).container.krate;
8686
let item_tree = loc.id.item_tree(db);
8787
let variant = &item_tree[loc.id.value];
8888
(variant.fields.clone(), item_tree, krate)
@@ -606,21 +606,24 @@ fn any_has_attrs<'db>(
606606
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
607607
}
608608

609-
fn attrs_from_item_tree<N: ItemTreeNode>(db: &dyn DefDatabase, id: ItemTreeId<N>) -> RawAttrs {
609+
fn attrs_from_item_tree<N: ItemTreeModItemNode>(
610+
db: &dyn DefDatabase,
611+
id: ItemTreeId<N>,
612+
) -> RawAttrs {
610613
let tree = id.item_tree(db);
611614
let mod_item = N::id_to_mod_item(id.value);
612615
tree.raw_attrs(mod_item.into()).clone()
613616
}
614617

615-
fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
618+
fn attrs_from_item_tree_loc<'db, N: ItemTreeModItemNode>(
616619
db: &(dyn DefDatabase + 'db),
617620
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>>,
618621
) -> RawAttrs {
619622
let id = lookup.lookup(db).id;
620623
attrs_from_item_tree(db, id)
621624
}
622625

623-
fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>(
626+
fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>(
624627
db: &(dyn DefDatabase + 'db),
625628
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>>,
626629
) -> RawAttrs {

crates/hir-def/src/body/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::*;
1818
pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBodyId) -> String {
1919
let header = match owner {
2020
DefWithBodyId::FunctionId(it) => {
21-
it.lookup(db).id.resolved(db, |it| format!("fn {} = ", it.name.display(db.upcast())))
21+
it.lookup(db).id.resolved(db, |it| format!("fn {}", it.name.display(db.upcast())))
2222
}
2323
DefWithBodyId::StaticId(it) => it
2424
.lookup(db)

crates/hir-def/src/data/adt.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ impl EnumVariantData {
334334
e: EnumVariantId,
335335
) -> (Arc<EnumVariantData>, DefDiagnostics) {
336336
let loc = e.lookup(db);
337-
let krate = loc.container.krate;
337+
let container = loc.parent.lookup(db).container;
338+
let krate = container.krate;
338339
let item_tree = loc.id.item_tree(db);
339340
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
340341
let variant = &item_tree[loc.id.value];
@@ -343,7 +344,7 @@ impl EnumVariantData {
343344
db,
344345
krate,
345346
loc.id.file_id(),
346-
loc.container.local_id,
347+
container.local_id,
347348
&item_tree,
348349
&cfg_options,
349350
&variant.fields,
@@ -395,7 +396,7 @@ impl HasChildSource<LocalFieldId> for VariantId {
395396
(
396397
lookup.source(db).map(|it| it.kind()),
397398
&item_tree[lookup.id.value].fields,
398-
lookup.container,
399+
lookup.parent.lookup(db).container,
399400
)
400401
}
401402
VariantId::StructId(it) => {
@@ -411,11 +412,7 @@ impl HasChildSource<LocalFieldId> for VariantId {
411412
let lookup = it.lookup(db);
412413
item_tree = lookup.id.item_tree(db);
413414
(
414-
lookup.source(db).map(|it| {
415-
it.record_field_list()
416-
.map(ast::StructKind::Record)
417-
.unwrap_or(ast::StructKind::Unit)
418-
}),
415+
lookup.source(db).map(|it| it.kind()),
419416
&item_tree[lookup.id.value].fields,
420417
lookup.container,
421418
)

crates/hir-def/src/item_tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ from_attrs!(
337337
);
338338

339339
/// Trait implemented by all item nodes in the item tree.
340-
pub trait ItemTreeNode: Clone {
340+
pub trait ItemTreeModItemNode: Clone {
341341
type Source: AstIdNode + Into<ast::Item>;
342342

343343
fn ast_id(&self) -> FileAstId<Self::Source>;
@@ -494,7 +494,7 @@ macro_rules! mod_items {
494494
)+
495495

496496
$(
497-
impl ItemTreeNode for $typ {
497+
impl ItemTreeModItemNode for $typ {
498498
type Source = $ast;
499499

500500
fn ast_id(&self) -> FileAstId<Self::Source> {
@@ -577,7 +577,7 @@ impl Index<RawVisibilityId> for ItemTree {
577577
}
578578
}
579579

580-
impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
580+
impl<N: ItemTreeModItemNode> Index<FileItemTreeId<N>> for ItemTree {
581581
type Output = N;
582582
fn index(&self, id: FileItemTreeId<N>) -> &N {
583583
N::lookup(self, id.index())

crates/hir-def/src/item_tree/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313

1414
use super::*;
1515

16-
fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
16+
fn id<N: ItemTreeModItemNode>(index: Idx<N>) -> FileItemTreeId<N> {
1717
FileItemTreeId(index)
1818
}
1919

crates/hir-def/src/lib.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ use crate::{
9999
data::adt::VariantData,
100100
db::DefDatabase,
101101
item_tree::{
102-
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, Macro2, MacroRules,
103-
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
102+
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeModItemNode, Macro2,
103+
MacroRules, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
104104
},
105105
};
106106

@@ -213,57 +213,57 @@ impl ModuleId {
213213
pub type LocalModuleId = Idx<nameres::ModuleData>;
214214

215215
#[derive(Debug)]
216-
pub struct ItemLoc<N: ItemTreeNode> {
216+
pub struct ItemLoc<N: ItemTreeModItemNode> {
217217
pub container: ModuleId,
218218
pub id: ItemTreeId<N>,
219219
}
220220

221-
impl<N: ItemTreeNode> Clone for ItemLoc<N> {
221+
impl<N: ItemTreeModItemNode> Clone for ItemLoc<N> {
222222
fn clone(&self) -> Self {
223223
Self { container: self.container, id: self.id }
224224
}
225225
}
226226

227-
impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
227+
impl<N: ItemTreeModItemNode> Copy for ItemLoc<N> {}
228228

229-
impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
229+
impl<N: ItemTreeModItemNode> PartialEq for ItemLoc<N> {
230230
fn eq(&self, other: &Self) -> bool {
231231
self.container == other.container && self.id == other.id
232232
}
233233
}
234234

235-
impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
235+
impl<N: ItemTreeModItemNode> Eq for ItemLoc<N> {}
236236

237-
impl<N: ItemTreeNode> Hash for ItemLoc<N> {
237+
impl<N: ItemTreeModItemNode> Hash for ItemLoc<N> {
238238
fn hash<H: Hasher>(&self, state: &mut H) {
239239
self.container.hash(state);
240240
self.id.hash(state);
241241
}
242242
}
243243

244244
#[derive(Debug)]
245-
pub struct AssocItemLoc<N: ItemTreeNode> {
245+
pub struct AssocItemLoc<N: ItemTreeModItemNode> {
246246
pub container: ItemContainerId,
247247
pub id: ItemTreeId<N>,
248248
}
249249

250-
impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
250+
impl<N: ItemTreeModItemNode> Clone for AssocItemLoc<N> {
251251
fn clone(&self) -> Self {
252252
Self { container: self.container, id: self.id }
253253
}
254254
}
255255

256-
impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
256+
impl<N: ItemTreeModItemNode> Copy for AssocItemLoc<N> {}
257257

258-
impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
258+
impl<N: ItemTreeModItemNode> PartialEq for AssocItemLoc<N> {
259259
fn eq(&self, other: &Self) -> bool {
260260
self.container == other.container && self.id == other.id
261261
}
262262
}
263263

264-
impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
264+
impl<N: ItemTreeModItemNode> Eq for AssocItemLoc<N> {}
265265

266-
impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
266+
impl<N: ItemTreeModItemNode> Hash for AssocItemLoc<N> {
267267
fn hash<H: Hasher>(&self, state: &mut H) {
268268
self.container.hash(state);
269269
self.id.hash(state);
@@ -297,15 +297,14 @@ pub struct EnumId(salsa::InternId);
297297
pub type EnumLoc = ItemLoc<Enum>;
298298
impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
299299

300-
// FIXME: rename to `VariantId`, only enums can ave variants
301300
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
302301
pub struct EnumVariantId(salsa::InternId);
303302

304303
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
305304
pub struct EnumVariantLoc {
306-
pub container: ModuleId,
307305
pub id: ItemTreeId<Variant>,
308306
pub parent: EnumId,
307+
pub index: u32,
309308
}
310309
impl_intern!(EnumVariantId, EnumVariantLoc, intern_enum_variant, lookup_intern_enum_variant);
311310

@@ -992,7 +991,8 @@ impl HasModule for ItemContainerId {
992991
}
993992
}
994993

995-
impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
994+
impl<N: ItemTreeModItemNode> HasModule for AssocItemLoc<N> {
995+
#[inline]
996996
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
997997
self.container.module(db)
998998
}
@@ -1008,7 +1008,22 @@ impl HasModule for AdtId {
10081008
}
10091009
}
10101010

1011+
impl HasModule for EnumId {
1012+
#[inline]
1013+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
1014+
self.lookup(db).container
1015+
}
1016+
}
1017+
1018+
impl HasModule for EnumVariantId {
1019+
#[inline]
1020+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
1021+
self.lookup(db).parent.module(db)
1022+
}
1023+
}
1024+
10111025
impl HasModule for ExternCrateId {
1026+
#[inline]
10121027
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10131028
self.lookup(db).container
10141029
}
@@ -1017,7 +1032,7 @@ impl HasModule for ExternCrateId {
10171032
impl HasModule for VariantId {
10181033
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10191034
match self {
1020-
VariantId::EnumVariantId(it) => it.lookup(db).container,
1035+
VariantId::EnumVariantId(it) => it.lookup(db).parent.module(db),
10211036
VariantId::StructId(it) => it.lookup(db).container,
10221037
VariantId::UnionId(it) => it.lookup(db).container,
10231038
}
@@ -1046,7 +1061,7 @@ impl HasModule for TypeOwnerId {
10461061
TypeOwnerId::TraitAliasId(it) => it.lookup(db).container,
10471062
TypeOwnerId::TypeAliasId(it) => it.lookup(db).module(db),
10481063
TypeOwnerId::ImplId(it) => it.lookup(db).container,
1049-
TypeOwnerId::EnumVariantId(it) => it.lookup(db).container,
1064+
TypeOwnerId::EnumVariantId(it) => it.lookup(db).parent.module(db),
10501065
}
10511066
}
10521067
}
@@ -1057,7 +1072,7 @@ impl HasModule for DefWithBodyId {
10571072
DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
10581073
DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
10591074
DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
1060-
DefWithBodyId::VariantId(it) => it.lookup(db).container,
1075+
DefWithBodyId::VariantId(it) => it.lookup(db).parent.module(db),
10611076
DefWithBodyId::InTypeConstId(it) => it.lookup(db).owner.module(db),
10621077
}
10631078
}
@@ -1072,19 +1087,21 @@ impl HasModule for GenericDefId {
10721087
GenericDefId::TraitAliasId(it) => it.lookup(db).container,
10731088
GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
10741089
GenericDefId::ImplId(it) => it.lookup(db).container,
1075-
GenericDefId::EnumVariantId(it) => it.lookup(db).container,
1090+
GenericDefId::EnumVariantId(it) => it.lookup(db).parent.lookup(db).container,
10761091
GenericDefId::ConstId(it) => it.lookup(db).module(db),
10771092
}
10781093
}
10791094
}
10801095

10811096
impl HasModule for TypeAliasId {
1097+
#[inline]
10821098
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10831099
self.lookup(db).module(db)
10841100
}
10851101
}
10861102

10871103
impl HasModule for TraitId {
1104+
#[inline]
10881105
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10891106
self.lookup(db).container
10901107
}
@@ -1099,7 +1116,7 @@ impl ModuleDefId {
10991116
ModuleDefId::ModuleId(id) => *id,
11001117
ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
11011118
ModuleDefId::AdtId(id) => id.module(db),
1102-
ModuleDefId::EnumVariantId(id) => id.lookup(db).container,
1119+
ModuleDefId::EnumVariantId(id) => id.lookup(db).parent.module(db),
11031120
ModuleDefId::ConstId(id) => id.lookup(db).container.module(db),
11041121
ModuleDefId::StaticId(id) => id.lookup(db).module(db),
11051122
ModuleDefId::TraitId(id) => id.lookup(db).container,
@@ -1118,7 +1135,7 @@ impl AttrDefId {
11181135
AttrDefId::FieldId(it) => it.parent.module(db).krate,
11191136
AttrDefId::AdtId(it) => it.module(db).krate,
11201137
AttrDefId::FunctionId(it) => it.lookup(db).module(db).krate,
1121-
AttrDefId::EnumVariantId(it) => it.lookup(db).container.krate,
1138+
AttrDefId::EnumVariantId(it) => it.lookup(db).parent.module(db).krate,
11221139
AttrDefId::StaticId(it) => it.lookup(db).module(db).krate,
11231140
AttrDefId::ConstId(it) => it.lookup(db).module(db).krate,
11241141
AttrDefId::TraitId(it) => it.lookup(db).container.krate,

0 commit comments

Comments
 (0)