Skip to content

Commit 2f6c390

Browse files
committed
Auto merge of rust-lang#12827 - Veykril:be-lazy, r=Veykril
internal: Construct fewer `AstIdMap`s in lowering
2 parents fa883cb + 7bd2e30 commit 2f6c390

File tree

4 files changed

+37
-49
lines changed

4 files changed

+37
-49
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: Arc<AstIdMap>,
5450
module: LocalModuleId,
5551
recursion_limit: usize,
5652
}
@@ -80,12 +76,10 @@ impl Expander {
8076
pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
8177
let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
8278
let def_map = module.def_map(db);
83-
let ast_id_map = db.ast_id_map(current_file_id);
8479
Expander {
8580
cfg_expander,
8681
def_map,
8782
current_file_id,
88-
ast_id_map,
8983
module: module.local_id,
9084
recursion_limit: 0,
9185
}
@@ -168,22 +162,17 @@ impl Expander {
168162
tracing::debug!("macro expansion {:#?}", node.syntax());
169163

170164
self.recursion_limit += 1;
171-
let mark = Mark {
172-
file_id: self.current_file_id,
173-
ast_id_map: mem::take(&mut self.ast_id_map),
174-
bomb: DropBomb::new("expansion mark dropped"),
175-
};
165+
let mark =
166+
Mark { file_id: self.current_file_id, bomb: DropBomb::new("expansion mark dropped") };
176167
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
177168
self.current_file_id = file_id;
178-
self.ast_id_map = db.ast_id_map(file_id);
179169

180170
ExpandResult { value: Some((mark, node)), err }
181171
}
182172

183173
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
184174
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
185175
self.current_file_id = mark.file_id;
186-
self.ast_id_map = mem::take(&mut mark.ast_id_map);
187176
self.recursion_limit -= 1;
188177
mark.bomb.defuse();
189178
}
@@ -213,11 +202,6 @@ impl Expander {
213202
self.def_map.resolve_path(db, self.module, path, BuiltinShadowMode::Other).0.take_macros()
214203
}
215204

216-
fn ast_id<N: AstNode>(&self, item: &N) -> AstId<N> {
217-
let file_local_id = self.ast_id_map.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: Arc<AstIdMap>,
237220
bomb: DropBomb,
238221
}
239222

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

+19-14
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use std::{mem, sync::Arc};
55

66
use either::Either;
77
use hir_expand::{
8-
ast_id_map::{AstIdMap, FileAstId},
8+
ast_id_map::AstIdMap,
99
hygiene::Hygiene,
1010
name::{name, AsName, Name},
11-
ExpandError, HirFileId, InFile,
11+
AstId, ExpandError, HirFileId, InFile,
1212
};
1313
use la_arena::Arena;
14+
use once_cell::unsync::OnceCell;
1415
use profile::Count;
1516
use rustc_hash::FxHashMap;
1617
use syntax::{
@@ -41,38 +42,34 @@ use crate::{
4142
pub struct LowerCtx<'a> {
4243
pub db: &'a dyn DefDatabase,
4344
hygiene: Hygiene,
44-
file_id: Option<HirFileId>,
45-
source_ast_id_map: Option<Arc<AstIdMap>>,
45+
ast_id_map: Option<(HirFileId, OnceCell<Arc<AstIdMap>>)>,
4646
}
4747

4848
impl<'a> LowerCtx<'a> {
4949
pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
5050
LowerCtx {
5151
db,
5252
hygiene: Hygiene::new(db.upcast(), file_id),
53-
file_id: Some(file_id),
54-
source_ast_id_map: Some(db.ast_id_map(file_id)),
53+
ast_id_map: Some((file_id, OnceCell::new())),
5554
}
5655
}
5756

5857
pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: &Hygiene) -> Self {
59-
LowerCtx { db, hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None }
58+
LowerCtx { db, hygiene: hygiene.clone(), ast_id_map: None }
6059
}
6160

6261
pub(crate) fn hygiene(&self) -> &Hygiene {
6362
&self.hygiene
6463
}
6564

66-
pub(crate) fn file_id(&self) -> HirFileId {
67-
self.file_id.unwrap()
68-
}
69-
7065
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
7166
Path::from_src(ast, self)
7267
}
7368

74-
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> Option<FileAstId<N>> {
75-
self.source_ast_id_map.as_ref().map(|ast_id_map| ast_id_map.ast_id(item))
69+
pub(crate) fn ast_id<N: AstNode>(&self, db: &dyn DefDatabase, item: &N) -> Option<AstId<N>> {
70+
let &(file_id, ref ast_id_map) = self.ast_id_map.as_ref()?;
71+
let ast_id_map = ast_id_map.get_or_init(|| db.ast_id_map(file_id));
72+
Some(InFile::new(file_id, ast_id_map.ast_id(item)))
7673
}
7774
}
7875

@@ -85,6 +82,7 @@ pub(super) fn lower(
8582
ExprCollector {
8683
db,
8784
source_map: BodySourceMap::default(),
85+
ast_id_map: db.ast_id_map(expander.current_file_id),
8886
body: Body {
8987
exprs: Arena::default(),
9088
pats: Arena::default(),
@@ -105,6 +103,7 @@ pub(super) fn lower(
105103
struct ExprCollector<'a> {
106104
db: &'a dyn DefDatabase,
107105
expander: Expander,
106+
ast_id_map: Arc<AstIdMap>,
108107
body: Body,
109108
source_map: BodySourceMap,
110109
// a poor-mans union-find?
@@ -586,8 +585,13 @@ impl ExprCollector<'_> {
586585
match res.value {
587586
Some((mark, expansion)) => {
588587
self.source_map.expansions.insert(macro_call_ptr, self.expander.current_file_id);
588+
let prev_ast_id_map = mem::replace(
589+
&mut self.ast_id_map,
590+
self.db.ast_id_map(self.expander.current_file_id),
591+
);
589592

590593
let id = collector(self, Some(expansion));
594+
self.ast_id_map = prev_ast_id_map;
591595
self.expander.exit(self.db, mark);
592596
id
593597
}
@@ -675,7 +679,8 @@ impl ExprCollector<'_> {
675679
}
676680

677681
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
678-
let ast_id = self.expander.ast_id(&block);
682+
let file_local_id = self.ast_id_map.ast_id(&block);
683+
let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
679684
let block_loc =
680685
BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
681686
let block_id = self.db.intern_block(block_loc);

crates/hir-def/src/data.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,17 @@ impl<'a> AssocItemCollector<'a> {
550550
AssocItem::MacroCall(call) => {
551551
let call = &item_tree[call];
552552
let ast_id_map = self.db.ast_id_map(self.expander.current_file_id());
553-
let root = self.db.parse_or_expand(self.expander.current_file_id()).unwrap();
554-
let call = ast_id_map.get(call.ast_id).to_node(&root);
555-
let _cx =
556-
stdx::panic_context::enter(format!("collect_items MacroCall: {}", call));
557-
let res = self.expander.enter_expand(self.db, call);
558-
559-
if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
560-
self.collect_macro_items(mark, mac);
553+
if let Some(root) = self.db.parse_or_expand(self.expander.current_file_id()) {
554+
let call = ast_id_map.get(call.ast_id).to_node(&root);
555+
let _cx = stdx::panic_context::enter(format!(
556+
"collect_items MacroCall: {}",
557+
call
558+
));
559+
let res = self.expander.enter_expand(self.db, call);
560+
561+
if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
562+
self.collect_macro_items(mark, mac);
563+
}
561564
}
562565
}
563566
}

crates/hir-def/src/type_ref.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt::Write;
55

66
use hir_expand::{
77
name::{AsName, Name},
8-
AstId, InFile,
8+
AstId,
99
};
1010
use syntax::ast::{self, HasName};
1111

@@ -236,10 +236,7 @@ impl TypeRef {
236236
TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
237237
}
238238
ast::Type::MacroType(mt) => match mt.macro_call() {
239-
Some(mc) => ctx
240-
.ast_id(&mc)
241-
.map(|mc| TypeRef::Macro(InFile::new(ctx.file_id(), mc)))
242-
.unwrap_or(TypeRef::Error),
239+
Some(mc) => ctx.ast_id(ctx.db, &mc).map(TypeRef::Macro).unwrap_or(TypeRef::Error),
243240
None => TypeRef::Error,
244241
},
245242
}

0 commit comments

Comments
 (0)