From 759f8c83e53bb72804cb4c578a01b09fc2dcf4fa Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 13 Mar 2021 00:34:01 +0100 Subject: Make `ItemTreeId` its own type --- crates/hir/src/semantics.rs | 2 +- crates/hir_def/src/adt.rs | 6 ++--- crates/hir_def/src/attr.rs | 2 +- crates/hir_def/src/data.rs | 20 ++++++++-------- crates/hir_def/src/generics.rs | 14 +++++------ crates/hir_def/src/item_tree.rs | 42 ++++++++++++++++++++++++++++++++- crates/hir_def/src/nameres/collector.rs | 12 +++++----- crates/hir_def/src/src.rs | 16 ++++++------- 8 files changed, 77 insertions(+), 37 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index e0eb2a66d..15651bb22 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -574,7 +574,7 @@ impl<'db> SemanticsImpl<'db> { } fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { - let file_id = self.db.lookup_intern_trait(def.id).id.file_id; + let file_id = self.db.lookup_intern_trait(def.id).id.file_id(); let resolver = def.id.resolver(self.db.upcast()); SemanticsScope { db: self.db, file_id, resolver } } diff --git a/crates/hir_def/src/adt.rs b/crates/hir_def/src/adt.rs index 1b9bb8235..58e35353b 100644 --- a/crates/hir_def/src/adt.rs +++ b/crates/hir_def/src/adt.rs @@ -94,7 +94,7 @@ impl StructData { pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc { let loc = id.lookup(db); let krate = loc.container.krate; - let item_tree = db.item_tree(loc.id.file_id); + let item_tree = loc.id.item_tree(db); let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into()); let cfg_options = db.crate_graph()[loc.container.krate].cfg_options.clone(); @@ -110,7 +110,7 @@ impl StructData { pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc { let loc = id.lookup(db); let krate = loc.container.krate; - let item_tree = db.item_tree(loc.id.file_id); + let item_tree = loc.id.item_tree(db); let repr = repr_from_value(db, krate, &item_tree, ModItem::from(loc.id.value).into()); let cfg_options = db.crate_graph()[loc.container.krate].cfg_options.clone(); @@ -130,7 +130,7 @@ impl EnumData { pub(crate) fn enum_data_query(db: &dyn DefDatabase, e: EnumId) -> Arc { let loc = e.lookup(db); let krate = loc.container.krate; - let item_tree = db.item_tree(loc.id.file_id); + let item_tree = loc.id.item_tree(db); let cfg_options = db.crate_graph()[krate].cfg_options.clone(); let enum_ = &item_tree[loc.id.value]; diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index b7353d868..e4c84afbf 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -530,7 +530,7 @@ where } fn attrs_from_item_tree(id: ItemTreeId, db: &dyn DefDatabase) -> RawAttrs { - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let mod_item = N::id_to_mod_item(id.value); tree.raw_attrs(mod_item.into()).clone() } diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index fefc31129..f1772ea3d 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs @@ -38,7 +38,7 @@ impl FunctionData { let krate = loc.container.module(db).krate; let crate_graph = db.crate_graph(); let cfg_options = &crate_graph[krate].cfg_options; - let item_tree = db.item_tree(loc.id.file_id); + let item_tree = loc.id.item_tree(db); let func = &item_tree[loc.id.value]; let enabled_params = func @@ -89,7 +89,7 @@ impl TypeAliasData { typ: TypeAliasId, ) -> Arc { let loc = typ.lookup(db); - let item_tree = db.item_tree(loc.id.file_id); + let item_tree = loc.id.item_tree(db); let typ = &item_tree[loc.id.value]; Arc::new(TypeAliasData { @@ -115,23 +115,23 @@ pub struct TraitData { impl TraitData { pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc { let tr_loc = tr.lookup(db); - let item_tree = db.item_tree(tr_loc.id.file_id); + let item_tree = tr_loc.id.item_tree(db); let tr_def = &item_tree[tr_loc.id.value]; let name = tr_def.name.clone(); let is_auto = tr_def.is_auto; let is_unsafe = tr_def.is_unsafe; let module_id = tr_loc.container; let container = AssocContainerId::TraitId(tr); - let mut expander = Expander::new(db, tr_loc.id.file_id, module_id); let visibility = item_tree[tr_def.visibility].clone(); let bounds = tr_def.bounds.clone(); + let mut expander = Expander::new(db, tr_loc.id.file_id(), module_id); let items = collect_items( db, module_id, &mut expander, tr_def.items.iter().copied(), - tr_loc.id.file_id, + tr_loc.id.file_id(), container, 100, ); @@ -167,21 +167,21 @@ impl ImplData { let _p = profile::span("impl_data_query"); let impl_loc = id.lookup(db); - let item_tree = db.item_tree(impl_loc.id.file_id); + let item_tree = impl_loc.id.item_tree(db); let impl_def = &item_tree[impl_loc.id.value]; 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; let container = AssocContainerId::ImplId(id); - let mut expander = Expander::new(db, impl_loc.id.file_id, module_id); + let mut expander = Expander::new(db, impl_loc.id.file_id(), module_id); let items = collect_items( db, module_id, &mut expander, impl_def.items.iter().copied(), - impl_loc.id.file_id, + impl_loc.id.file_id(), container, 100, ); @@ -202,7 +202,7 @@ pub struct ConstData { impl ConstData { pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc { let loc = konst.lookup(db); - let item_tree = db.item_tree(loc.id.file_id); + let item_tree = loc.id.item_tree(db); let konst = &item_tree[loc.id.value]; Arc::new(ConstData { @@ -225,7 +225,7 @@ pub struct StaticData { impl StaticData { pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc { let node = konst.lookup(db); - let item_tree = db.item_tree(node.id.file_id); + let item_tree = node.id.item_tree(db); let statik = &item_tree[node.id.value]; Arc::new(StaticData { diff --git a/crates/hir_def/src/generics.rs b/crates/hir_def/src/generics.rs index a056ab797..7c6cbff11 100644 --- a/crates/hir_def/src/generics.rs +++ b/crates/hir_def/src/generics.rs @@ -97,43 +97,43 @@ impl GenericParams { let generics = match def { GenericDefId::FunctionId(id) => { let id = id.lookup(db).id; - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let item = &tree[id.value]; tree[item.generic_params].clone() } GenericDefId::AdtId(AdtId::StructId(id)) => { let id = id.lookup(db).id; - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let item = &tree[id.value]; tree[item.generic_params].clone() } GenericDefId::AdtId(AdtId::EnumId(id)) => { let id = id.lookup(db).id; - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let item = &tree[id.value]; tree[item.generic_params].clone() } GenericDefId::AdtId(AdtId::UnionId(id)) => { let id = id.lookup(db).id; - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let item = &tree[id.value]; tree[item.generic_params].clone() } GenericDefId::TraitId(id) => { let id = id.lookup(db).id; - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let item = &tree[id.value]; tree[item.generic_params].clone() } GenericDefId::TypeAliasId(id) => { let id = id.lookup(db).id; - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let item = &tree[id.value]; tree[item.generic_params].clone() } GenericDefId::ImplId(id) => { let id = id.lookup(db).id; - let tree = db.item_tree(id.file_id); + let tree = id.item_tree(db); let item = &tree[id.value]; tree[item.generic_params].clone() } diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 90df3d929..8383780e0 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -401,7 +401,47 @@ impl fmt::Debug for FileItemTreeId { } } -pub type ItemTreeId = InFile>; +#[derive(Debug)] +pub struct ItemTreeId { + file: HirFileId, + pub value: FileItemTreeId, +} + +impl ItemTreeId { + pub fn new(file: HirFileId, idx: FileItemTreeId) -> Self { + Self { file, value: idx } + } + + pub fn file_id(self) -> HirFileId { + self.file + } + + pub fn item_tree(self, db: &dyn DefDatabase) -> Arc { + db.item_tree(self.file) + } +} + +impl Copy for ItemTreeId {} +impl Clone for ItemTreeId { + fn clone(&self) -> Self { + *self + } +} + +impl PartialEq for ItemTreeId { + fn eq(&self, other: &Self) -> bool { + self.file == other.file && self.value == other.value + } +} + +impl Eq for ItemTreeId {} + +impl Hash for ItemTreeId { + fn hash(&self, state: &mut H) { + self.file.hash(state); + self.value.hash(state); + } +} macro_rules! mod_items { ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => { diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index b5ae5a9e4..7ca4299b9 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -951,21 +951,21 @@ impl DefCollector<'_> { let mut diagnosed_extern_crates = FxHashSet::default(); for directive in &self.unresolved_imports { if let ImportSource::ExternCrate(krate) = directive.import.source { - let item_tree = self.db.item_tree(krate.file_id); + let item_tree = krate.item_tree(self.db); let extern_crate = &item_tree[krate.value]; diagnosed_extern_crates.insert(extern_crate.name.clone()); self.def_map.diagnostics.push(DefDiagnostic::unresolved_extern_crate( directive.module_id, - InFile::new(krate.file_id, extern_crate.ast_id), + InFile::new(krate.file_id(), extern_crate.ast_id), )); } } for directive in &self.unresolved_imports { if let ImportSource::Import(import) = &directive.import.source { - let item_tree = self.db.item_tree(import.file_id); + let item_tree = import.item_tree(self.db); let import_data = &item_tree[import.value]; match (import_data.path.segments().first(), &import_data.path.kind) { @@ -979,7 +979,7 @@ impl DefCollector<'_> { self.def_map.diagnostics.push(DefDiagnostic::unresolved_import( directive.module_id, - InFile::new(import.file_id, import_data.ast_id), + InFile::new(import.file_id(), import_data.ast_id), import_data.index, )); } @@ -1055,7 +1055,7 @@ impl ModCollector<'_, '_> { self.def_collector.db, krate, &self.item_tree, - InFile::new(self.file_id, import_id), + ItemTreeId::new(self.file_id, import_id), ), status: PartialResolvedImport::Unresolved, }) @@ -1067,7 +1067,7 @@ impl ModCollector<'_, '_> { self.def_collector.db, krate, &self.item_tree, - InFile::new(self.file_id, import_id), + ItemTreeId::new(self.file_id, import_id), ), status: PartialResolvedImport::Unresolved, }) diff --git a/crates/hir_def/src/src.rs b/crates/hir_def/src/src.rs index 751d4c052..24e57b469 100644 --- a/crates/hir_def/src/src.rs +++ b/crates/hir_def/src/src.rs @@ -14,12 +14,12 @@ impl HasSource for AssocItemLoc { type Value = N::Source; fn source(&self, db: &dyn DefDatabase) -> InFile { - let tree = db.item_tree(self.id.file_id); - let ast_id_map = db.ast_id_map(self.id.file_id); - let root = db.parse_or_expand(self.id.file_id).unwrap(); + let tree = self.id.item_tree(db); + let ast_id_map = db.ast_id_map(self.id.file_id()); + let root = db.parse_or_expand(self.id.file_id()).unwrap(); let node = &tree[self.id.value]; - InFile::new(self.id.file_id, ast_id_map.get(node.ast_id()).to_node(&root)) + InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root)) } } @@ -27,12 +27,12 @@ impl HasSource for ItemLoc { type Value = N::Source; fn source(&self, db: &dyn DefDatabase) -> InFile { - let tree = db.item_tree(self.id.file_id); - let ast_id_map = db.ast_id_map(self.id.file_id); - let root = db.parse_or_expand(self.id.file_id).unwrap(); + let tree = self.id.item_tree(db); + let ast_id_map = db.ast_id_map(self.id.file_id()); + let root = db.parse_or_expand(self.id.file_id()).unwrap(); let node = &tree[self.id.value]; - InFile::new(self.id.file_id, ast_id_map.get(node.ast_id()).to_node(&root)) + InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root)) } } -- cgit v1.2.3