Skip to content

Commit fde2d9b

Browse files
committed
Deduplicate FileId field in ModuleOrigin
1 parent e5b23e3 commit fde2d9b

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

crates/hir-def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl AttrsWithOwner {
431431
.item_tree(db)
432432
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
433433
.clone(),
434-
ModuleOrigin::BlockExpr { id, block } => {
434+
ModuleOrigin::BlockExpr { id, .. } => {
435435
let tree = db.block_item_tree_query(id);
436436
tree.raw_attrs(AttrOwner::TopLevel).clone()
437437
}

crates/hir-def/src/nameres.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mod tests;
6060
use std::{cmp::Ord, ops::Deref};
6161

6262
use base_db::{CrateId, Edition, FileId, ProcMacroKind};
63-
use hir_expand::{name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
63+
use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
6464
use itertools::Itertools;
6565
use la_arena::Arena;
6666
use profile::Count;
@@ -217,13 +217,13 @@ pub enum ModuleOrigin {
217217
/// Note that non-inline modules, by definition, live inside non-macro file.
218218
File {
219219
is_mod_rs: bool,
220-
declaration: AstId<ast::Module>,
220+
declaration: FileAstId<ast::Module>,
221221
declaration_tree_id: ItemTreeId<Mod>,
222222
definition: FileId,
223223
},
224224
Inline {
225225
definition_tree_id: ItemTreeId<Mod>,
226-
definition: AstId<ast::Module>,
226+
definition: FileAstId<ast::Module>,
227227
},
228228
/// Pseudo-module introduced by a block scope (contains only inner items).
229229
BlockExpr {
@@ -235,8 +235,12 @@ pub enum ModuleOrigin {
235235
impl ModuleOrigin {
236236
pub fn declaration(&self) -> Option<AstId<ast::Module>> {
237237
match self {
238-
ModuleOrigin::File { declaration: module, .. }
239-
| ModuleOrigin::Inline { definition: module, .. } => Some(*module),
238+
&ModuleOrigin::File { declaration, declaration_tree_id, .. } => {
239+
Some(AstId::new(declaration_tree_id.file_id(), declaration))
240+
}
241+
&ModuleOrigin::Inline { definition, definition_tree_id } => {
242+
Some(AstId::new(definition_tree_id.file_id(), definition))
243+
}
240244
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::BlockExpr { .. } => None,
241245
}
242246
}
@@ -261,14 +265,15 @@ impl ModuleOrigin {
261265
/// That is, a file or a `mod foo {}` with items.
262266
fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
263267
match self {
264-
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
265-
let file_id = *definition;
266-
let sf = db.parse(file_id).tree();
267-
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
268+
&ModuleOrigin::File { definition, .. } | &ModuleOrigin::CrateRoot { definition } => {
269+
let sf = db.parse(definition).tree();
270+
InFile::new(definition.into(), ModuleSource::SourceFile(sf))
268271
}
269-
ModuleOrigin::Inline { definition, .. } => InFile::new(
270-
definition.file_id,
271-
ModuleSource::Module(definition.to_node(db.upcast())),
272+
&ModuleOrigin::Inline { definition, definition_tree_id } => InFile::new(
273+
definition_tree_id.file_id(),
274+
ModuleSource::Module(
275+
AstId::new(definition_tree_id.file_id(), definition).to_node(db.upcast()),
276+
),
272277
),
273278
ModuleOrigin::BlockExpr { block, .. } => {
274279
InFile::new(block.file_id, ModuleSource::BlockExpr(block.to_node(db.upcast())))
@@ -645,7 +650,7 @@ impl ModuleData {
645650
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
646651
definition.into()
647652
}
648-
ModuleOrigin::Inline { definition, .. } => definition.file_id,
653+
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id.file_id(),
649654
ModuleOrigin::BlockExpr { block, .. } => block.file_id,
650655
}
651656
}

crates/hir-def/src/nameres/collector.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,7 @@ impl ModCollector<'_, '_> {
18431843
ModKind::Inline { items } => {
18441844
let module_id = self.push_child_module(
18451845
module.name.clone(),
1846-
AstId::new(self.file_id(), module.ast_id),
1846+
module.ast_id,
18471847
None,
18481848
&self.item_tree[module.visibility],
18491849
module_id,
@@ -1881,7 +1881,7 @@ impl ModCollector<'_, '_> {
18811881
if is_enabled {
18821882
let module_id = self.push_child_module(
18831883
module.name.clone(),
1884-
ast_id,
1884+
ast_id.value,
18851885
Some((file_id, is_mod_rs)),
18861886
&self.item_tree[module.visibility],
18871887
module_id,
@@ -1908,7 +1908,7 @@ impl ModCollector<'_, '_> {
19081908
Err(candidates) => {
19091909
self.push_child_module(
19101910
module.name.clone(),
1911-
ast_id,
1911+
ast_id.value,
19121912
None,
19131913
&self.item_tree[module.visibility],
19141914
module_id,
@@ -1925,7 +1925,7 @@ impl ModCollector<'_, '_> {
19251925
fn push_child_module(
19261926
&mut self,
19271927
name: Name,
1928-
declaration: AstId<ast::Module>,
1928+
declaration: FileAstId<ast::Module>,
19291929
definition: Option<(FileId, bool)>,
19301930
visibility: &crate::visibility::RawVisibility,
19311931
mod_tree_id: FileItemTreeId<Mod>,

0 commit comments

Comments
 (0)