Deduplicate FileId field in ModuleOrigin

This commit is contained in:
Lukas Wirth 2023-08-10 19:04:46 +02:00
parent e5b23e3bc1
commit fde2d9b47c
3 changed files with 23 additions and 18 deletions

View File

@ -431,7 +431,7 @@ pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs {
.item_tree(db)
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
.clone(),
ModuleOrigin::BlockExpr { id, block } => {
ModuleOrigin::BlockExpr { id, .. } => {
let tree = db.block_item_tree_query(id);
tree.raw_attrs(AttrOwner::TopLevel).clone()
}

View File

@ -60,7 +60,7 @@
use std::{cmp::Ord, ops::Deref};
use base_db::{CrateId, Edition, FileId, ProcMacroKind};
use hir_expand::{name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
use itertools::Itertools;
use la_arena::Arena;
use profile::Count;
@ -217,13 +217,13 @@ pub enum ModuleOrigin {
/// Note that non-inline modules, by definition, live inside non-macro file.
File {
is_mod_rs: bool,
declaration: AstId<ast::Module>,
declaration: FileAstId<ast::Module>,
declaration_tree_id: ItemTreeId<Mod>,
definition: FileId,
},
Inline {
definition_tree_id: ItemTreeId<Mod>,
definition: AstId<ast::Module>,
definition: FileAstId<ast::Module>,
},
/// Pseudo-module introduced by a block scope (contains only inner items).
BlockExpr {
@ -235,8 +235,12 @@ pub enum ModuleOrigin {
impl ModuleOrigin {
pub fn declaration(&self) -> Option<AstId<ast::Module>> {
match self {
ModuleOrigin::File { declaration: module, .. }
| ModuleOrigin::Inline { definition: module, .. } => Some(*module),
&ModuleOrigin::File { declaration, declaration_tree_id, .. } => {
Some(AstId::new(declaration_tree_id.file_id(), declaration))
}
&ModuleOrigin::Inline { definition, definition_tree_id } => {
Some(AstId::new(definition_tree_id.file_id(), definition))
}
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::BlockExpr { .. } => None,
}
}
@ -261,14 +265,15 @@ pub fn is_inline(&self) -> bool {
/// That is, a file or a `mod foo {}` with items.
fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
match self {
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
let file_id = *definition;
let sf = db.parse(file_id).tree();
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
&ModuleOrigin::File { definition, .. } | &ModuleOrigin::CrateRoot { definition } => {
let sf = db.parse(definition).tree();
InFile::new(definition.into(), ModuleSource::SourceFile(sf))
}
ModuleOrigin::Inline { definition, .. } => InFile::new(
definition.file_id,
ModuleSource::Module(definition.to_node(db.upcast())),
&ModuleOrigin::Inline { definition, definition_tree_id } => InFile::new(
definition_tree_id.file_id(),
ModuleSource::Module(
AstId::new(definition_tree_id.file_id(), definition).to_node(db.upcast()),
),
),
ModuleOrigin::BlockExpr { block, .. } => {
InFile::new(block.file_id, ModuleSource::BlockExpr(block.to_node(db.upcast())))
@ -645,7 +650,7 @@ pub fn definition_source_file_id(&self) -> HirFileId {
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
definition.into()
}
ModuleOrigin::Inline { definition, .. } => definition.file_id,
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id.file_id(),
ModuleOrigin::BlockExpr { block, .. } => block.file_id,
}
}

View File

@ -1843,7 +1843,7 @@ fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
ModKind::Inline { items } => {
let module_id = self.push_child_module(
module.name.clone(),
AstId::new(self.file_id(), module.ast_id),
module.ast_id,
None,
&self.item_tree[module.visibility],
module_id,
@ -1881,7 +1881,7 @@ fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
if is_enabled {
let module_id = self.push_child_module(
module.name.clone(),
ast_id,
ast_id.value,
Some((file_id, is_mod_rs)),
&self.item_tree[module.visibility],
module_id,
@ -1908,7 +1908,7 @@ fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
Err(candidates) => {
self.push_child_module(
module.name.clone(),
ast_id,
ast_id.value,
None,
&self.item_tree[module.visibility],
module_id,
@ -1925,7 +1925,7 @@ fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
fn push_child_module(
&mut self,
name: Name,
declaration: AstId<ast::Module>,
declaration: FileAstId<ast::Module>,
definition: Option<(FileId, bool)>,
visibility: &crate::visibility::RawVisibility,
mod_tree_id: FileItemTreeId<Mod>,