Skip to content

Commit c83f14a

Browse files
committed
Remove AstIdMap from Expander as it is seldom needed
1 parent e507807 commit c83f14a

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

crates/hir-def/src/body.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ mod lower;
55
mod tests;
66
pub mod scope;
77

8-
use std::{mem, ops::Index, sync::Arc};
8+
use std::{ops::Index, sync::Arc};
99

1010
use base_db::CrateId;
1111
use cfg::{CfgExpr, CfgOptions};
1212
use drop_bomb::DropBomb;
1313
use either::Either;
14-
use hir_expand::{
15-
ast_id_map::AstIdMap, hygiene::Hygiene, AstId, ExpandError, ExpandResult, HirFileId, InFile,
16-
MacroCallId,
17-
};
14+
use hir_expand::{hygiene::Hygiene, ExpandError, ExpandResult, HirFileId, InFile, MacroCallId};
1815
use la_arena::{Arena, ArenaMap};
1916
use limit::Limit;
2017
use profile::Count;
2118
use rustc_hash::FxHashMap;
22-
use syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
19+
use syntax::{ast, AstPtr, SyntaxNodePtr};
2320

2421
use crate::{
2522
attr::{Attrs, RawAttrs},
@@ -50,7 +47,6 @@ pub struct Expander {
5047
cfg_expander: CfgExpander,
5148
def_map: Arc<DefMap>,
5249
current_file_id: HirFileId,
53-
ast_id_map: Option<Arc<AstIdMap>>,
5450
module: LocalModuleId,
5551
recursion_limit: usize,
5652
}
@@ -84,7 +80,6 @@ impl Expander {
8480
cfg_expander,
8581
def_map,
8682
current_file_id,
87-
ast_id_map: None,
8883
module: module.local_id,
8984
recursion_limit: 0,
9085
}
@@ -167,22 +162,17 @@ impl Expander {
167162
tracing::debug!("macro expansion {:#?}", node.syntax());
168163

169164
self.recursion_limit += 1;
170-
let mark = Mark {
171-
file_id: self.current_file_id,
172-
ast_id_map: mem::take(&mut self.ast_id_map),
173-
bomb: DropBomb::new("expansion mark dropped"),
174-
};
165+
let mark =
166+
Mark { file_id: self.current_file_id, bomb: DropBomb::new("expansion mark dropped") };
175167
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
176168
self.current_file_id = file_id;
177-
self.ast_id_map = None;
178169

179170
ExpandResult { value: Some((mark, node)), err }
180171
}
181172

182173
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
183174
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
184175
self.current_file_id = mark.file_id;
185-
self.ast_id_map = mem::take(&mut mark.ast_id_map);
186176
self.recursion_limit -= 1;
187177
mark.bomb.defuse();
188178
}
@@ -212,12 +202,6 @@ impl Expander {
212202
self.def_map.resolve_path(db, self.module, path, BuiltinShadowMode::Other).0.take_macros()
213203
}
214204

215-
fn ast_id<N: AstNode>(&mut self, db: &dyn DefDatabase, item: &N) -> AstId<N> {
216-
let file_local_id =
217-
self.ast_id_map.get_or_insert_with(|| db.ast_id_map(self.current_file_id)).ast_id(item);
218-
AstId::new(self.current_file_id, file_local_id)
219-
}
220-
221205
fn recursion_limit(&self, db: &dyn DefDatabase) -> Limit {
222206
let limit = db.crate_limits(self.cfg_expander.krate).recursion_limit as _;
223207

@@ -233,7 +217,6 @@ impl Expander {
233217
#[derive(Debug)]
234218
pub struct Mark {
235219
file_id: HirFileId,
236-
ast_id_map: Option<Arc<AstIdMap>>,
237220
bomb: DropBomb,
238221
}
239222

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir_expand::{
88
ast_id_map::{AstIdMap, FileAstId},
99
hygiene::Hygiene,
1010
name::{name, AsName, Name},
11-
ExpandError, HirFileId, InFile,
11+
AstId, ExpandError, HirFileId, InFile,
1212
};
1313
use la_arena::Arena;
1414
use once_cell::unsync::OnceCell;
@@ -90,6 +90,7 @@ pub(super) fn lower(
9090
ExprCollector {
9191
db,
9292
source_map: BodySourceMap::default(),
93+
ast_id_map: db.ast_id_map(expander.current_file_id),
9394
body: Body {
9495
exprs: Arena::default(),
9596
pats: Arena::default(),
@@ -110,6 +111,7 @@ pub(super) fn lower(
110111
struct ExprCollector<'a> {
111112
db: &'a dyn DefDatabase,
112113
expander: Expander,
114+
ast_id_map: Arc<AstIdMap>,
113115
body: Body,
114116
source_map: BodySourceMap,
115117
// a poor-mans union-find?
@@ -591,8 +593,13 @@ impl ExprCollector<'_> {
591593
match res.value {
592594
Some((mark, expansion)) => {
593595
self.source_map.expansions.insert(macro_call_ptr, self.expander.current_file_id);
596+
let prev_ast_id_map = mem::replace(
597+
&mut self.ast_id_map,
598+
self.db.ast_id_map(self.expander.current_file_id),
599+
);
594600

595601
let id = collector(self, Some(expansion));
602+
self.ast_id_map = prev_ast_id_map;
596603
self.expander.exit(self.db, mark);
597604
id
598605
}
@@ -680,7 +687,8 @@ impl ExprCollector<'_> {
680687
}
681688

682689
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
683-
let ast_id = self.expander.ast_id(self.db, &block);
690+
let file_local_id = self.ast_id_map.ast_id(&block);
691+
let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
684692
let block_loc =
685693
BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
686694
let block_id = self.db.intern_block(block_loc);

0 commit comments

Comments
 (0)