Skip to content

Commit 9b835f3

Browse files
committed
Auto merge of rust-lang#14594 - Veykril:Simplify, r=Veykril
internal: Move Expander and LowerCtx into separate modules
2 parents b92b7c0 + bca8029 commit 9b835f3

25 files changed

+487
-402
lines changed

crates/hir-def/src/attr.rs

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use crate::{
2828
lang_item::LangItem,
2929
nameres::{ModuleOrigin, ModuleSource},
3030
src::{HasChildSource, HasSource},
31-
AdtId, AttrDefId, EnumId, GenericParamId, LocalEnumVariantId, LocalFieldId, Lookup, MacroId,
32-
VariantId,
31+
AdtId, AssocItemLoc, AttrDefId, EnumId, GenericParamId, ItemLoc, LocalEnumVariantId,
32+
LocalFieldId, Lookup, MacroId, VariantId,
3333
};
3434

3535
/// Holds documentation
@@ -421,23 +421,24 @@ impl AttrsWithOwner {
421421
AttrDefId::EnumVariantId(it) => {
422422
return db.variants_attrs(it.parent)[it.local_id].clone();
423423
}
424+
// FIXME: DRY this up
424425
AttrDefId::AdtId(it) => match it {
425-
AdtId::StructId(it) => attrs_from_item_tree(it.lookup(db).id, db),
426-
AdtId::EnumId(it) => attrs_from_item_tree(it.lookup(db).id, db),
427-
AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
426+
AdtId::StructId(it) => attrs_from_item_tree_loc(db, it),
427+
AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it),
428+
AdtId::UnionId(it) => attrs_from_item_tree_loc(db, it),
428429
},
429-
AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
430-
AttrDefId::TraitAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
430+
AttrDefId::TraitId(it) => attrs_from_item_tree_loc(db, it),
431+
AttrDefId::TraitAliasId(it) => attrs_from_item_tree_loc(db, it),
431432
AttrDefId::MacroId(it) => match it {
432-
MacroId::Macro2Id(it) => attrs_from_item_tree(it.lookup(db).id, db),
433-
MacroId::MacroRulesId(it) => attrs_from_item_tree(it.lookup(db).id, db),
434-
MacroId::ProcMacroId(it) => attrs_from_item_tree(it.lookup(db).id, db),
433+
MacroId::Macro2Id(it) => attrs_from_item_tree(db, it.lookup(db).id),
434+
MacroId::MacroRulesId(it) => attrs_from_item_tree(db, it.lookup(db).id),
435+
MacroId::ProcMacroId(it) => attrs_from_item_tree(db, it.lookup(db).id),
435436
},
436-
AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
437-
AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
438-
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
439-
AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
440-
AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
437+
AttrDefId::ImplId(it) => attrs_from_item_tree_loc(db, it),
438+
AttrDefId::ConstId(it) => attrs_from_item_tree_assoc(db, it),
439+
AttrDefId::StaticId(it) => attrs_from_item_tree_assoc(db, it),
440+
AttrDefId::FunctionId(it) => attrs_from_item_tree_assoc(db, it),
441+
AttrDefId::TypeAliasId(it) => attrs_from_item_tree_assoc(db, it),
441442
AttrDefId::GenericParamId(it) => match it {
442443
GenericParamId::ConstParamId(it) => {
443444
let src = it.parent().child_source(db);
@@ -458,7 +459,7 @@ impl AttrsWithOwner {
458459
RawAttrs::from_attrs_owner(db.upcast(), src.with_value(&src.value[it.local_id]))
459460
}
460461
},
461-
AttrDefId::ExternBlockId(it) => attrs_from_item_tree(it.lookup(db).id, db),
462+
AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it),
462463
};
463464

464465
let attrs = raw_attrs.filter(db.upcast(), def.krate(db));
@@ -506,28 +507,28 @@ impl AttrsWithOwner {
506507
InFile::new(file_id, owner)
507508
}
508509
AttrDefId::AdtId(adt) => match adt {
509-
AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
510-
AdtId::UnionId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
511-
AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
510+
AdtId::StructId(id) => any_has_attrs(db, id),
511+
AdtId::UnionId(id) => any_has_attrs(db, id),
512+
AdtId::EnumId(id) => any_has_attrs(db, id),
512513
},
513-
AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
514+
AttrDefId::FunctionId(id) => any_has_attrs(db, id),
514515
AttrDefId::EnumVariantId(id) => {
515516
let map = db.variants_attrs_source_map(id.parent);
516517
let file_id = id.parent.lookup(db).id.file_id();
517518
let root = db.parse_or_expand(file_id);
518519
InFile::new(file_id, ast::AnyHasAttrs::new(map[id.local_id].to_node(&root)))
519520
}
520-
AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
521-
AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
522-
AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
523-
AttrDefId::TraitAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
524-
AttrDefId::TypeAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
521+
AttrDefId::StaticId(id) => any_has_attrs(db, id),
522+
AttrDefId::ConstId(id) => any_has_attrs(db, id),
523+
AttrDefId::TraitId(id) => any_has_attrs(db, id),
524+
AttrDefId::TraitAliasId(id) => any_has_attrs(db, id),
525+
AttrDefId::TypeAliasId(id) => any_has_attrs(db, id),
525526
AttrDefId::MacroId(id) => match id {
526-
MacroId::Macro2Id(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
527-
MacroId::MacroRulesId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
528-
MacroId::ProcMacroId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
527+
MacroId::Macro2Id(id) => any_has_attrs(db, id),
528+
MacroId::MacroRulesId(id) => any_has_attrs(db, id),
529+
MacroId::ProcMacroId(id) => any_has_attrs(db, id),
529530
},
530-
AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
531+
AttrDefId::ImplId(id) => any_has_attrs(db, id),
531532
AttrDefId::GenericParamId(id) => match id {
532533
GenericParamId::ConstParamId(id) => id
533534
.parent()
@@ -542,7 +543,7 @@ impl AttrsWithOwner {
542543
.child_source(db)
543544
.map(|source| ast::AnyHasAttrs::new(source[id.local_id].clone())),
544545
},
545-
AttrDefId::ExternBlockId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
546+
AttrDefId::ExternBlockId(id) => any_has_attrs(db, id),
546547
};
547548

548549
AttrSourceMap::new(owner.as_ref().map(|node| node as &dyn HasAttrs))
@@ -769,12 +770,35 @@ impl<'attr> AttrQuery<'attr> {
769770
}
770771
}
771772

772-
fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase) -> RawAttrs {
773+
fn any_has_attrs(
774+
db: &dyn DefDatabase,
775+
id: impl Lookup<Data = impl HasSource<Value = impl ast::HasAttrs>>,
776+
) -> InFile<ast::AnyHasAttrs> {
777+
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
778+
}
779+
780+
fn attrs_from_item_tree<N: ItemTreeNode>(db: &dyn DefDatabase, id: ItemTreeId<N>) -> RawAttrs {
773781
let tree = id.item_tree(db);
774782
let mod_item = N::id_to_mod_item(id.value);
775783
tree.raw_attrs(mod_item.into()).clone()
776784
}
777785

786+
fn attrs_from_item_tree_loc<N: ItemTreeNode>(
787+
db: &dyn DefDatabase,
788+
lookup: impl Lookup<Data = ItemLoc<N>>,
789+
) -> RawAttrs {
790+
let id = lookup.lookup(db).id;
791+
attrs_from_item_tree(db, id)
792+
}
793+
794+
fn attrs_from_item_tree_assoc<N: ItemTreeNode>(
795+
db: &dyn DefDatabase,
796+
lookup: impl Lookup<Data = AssocItemLoc<N>>,
797+
) -> RawAttrs {
798+
let id = lookup.lookup(db).id;
799+
attrs_from_item_tree(db, id)
800+
}
801+
778802
pub(crate) fn variants_attrs_source_map(
779803
db: &dyn DefDatabase,
780804
def: EnumId,

0 commit comments

Comments
 (0)