Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/body/lower.rs')
-rw-r--r--crates/hir-def/src/body/lower.rs33
1 files changed, 19 insertions, 14 deletions
diff --git a/crates/hir-def/src/body/lower.rs b/crates/hir-def/src/body/lower.rs
index 049afa8227..c3f2611227 100644
--- a/crates/hir-def/src/body/lower.rs
+++ b/crates/hir-def/src/body/lower.rs
@@ -5,12 +5,13 @@ use std::{mem, sync::Arc};
use either::Either;
use hir_expand::{
- ast_id_map::{AstIdMap, FileAstId},
+ ast_id_map::AstIdMap,
hygiene::Hygiene,
name::{name, AsName, Name},
- ExpandError, HirFileId, InFile,
+ AstId, ExpandError, HirFileId, InFile,
};
use la_arena::Arena;
+use once_cell::unsync::OnceCell;
use profile::Count;
use rustc_hash::FxHashMap;
use syntax::{
@@ -41,8 +42,7 @@ use crate::{
pub struct LowerCtx<'a> {
pub db: &'a dyn DefDatabase,
hygiene: Hygiene,
- file_id: Option<HirFileId>,
- source_ast_id_map: Option<Arc<AstIdMap>>,
+ ast_id_map: Option<(HirFileId, OnceCell<Arc<AstIdMap>>)>,
}
impl<'a> LowerCtx<'a> {
@@ -50,29 +50,26 @@ impl<'a> LowerCtx<'a> {
LowerCtx {
db,
hygiene: Hygiene::new(db.upcast(), file_id),
- file_id: Some(file_id),
- source_ast_id_map: Some(db.ast_id_map(file_id)),
+ ast_id_map: Some((file_id, OnceCell::new())),
}
}
pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: &Hygiene) -> Self {
- LowerCtx { db, hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None }
+ LowerCtx { db, hygiene: hygiene.clone(), ast_id_map: None }
}
pub(crate) fn hygiene(&self) -> &Hygiene {
&self.hygiene
}
- pub(crate) fn file_id(&self) -> HirFileId {
- self.file_id.unwrap()
- }
-
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
Path::from_src(ast, self)
}
- pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> Option<FileAstId<N>> {
- self.source_ast_id_map.as_ref().map(|ast_id_map| ast_id_map.ast_id(item))
+ pub(crate) fn ast_id<N: AstNode>(&self, db: &dyn DefDatabase, item: &N) -> Option<AstId<N>> {
+ let &(file_id, ref ast_id_map) = self.ast_id_map.as_ref()?;
+ let ast_id_map = ast_id_map.get_or_init(|| db.ast_id_map(file_id));
+ Some(InFile::new(file_id, ast_id_map.ast_id(item)))
}
}
@@ -85,6 +82,7 @@ pub(super) fn lower(
ExprCollector {
db,
source_map: BodySourceMap::default(),
+ ast_id_map: db.ast_id_map(expander.current_file_id),
body: Body {
exprs: Arena::default(),
pats: Arena::default(),
@@ -105,6 +103,7 @@ pub(super) fn lower(
struct ExprCollector<'a> {
db: &'a dyn DefDatabase,
expander: Expander,
+ ast_id_map: Arc<AstIdMap>,
body: Body,
source_map: BodySourceMap,
// a poor-mans union-find?
@@ -586,8 +585,13 @@ impl ExprCollector<'_> {
match res.value {
Some((mark, expansion)) => {
self.source_map.expansions.insert(macro_call_ptr, self.expander.current_file_id);
+ let prev_ast_id_map = mem::replace(
+ &mut self.ast_id_map,
+ self.db.ast_id_map(self.expander.current_file_id),
+ );
let id = collector(self, Some(expansion));
+ self.ast_id_map = prev_ast_id_map;
self.expander.exit(self.db, mark);
id
}
@@ -675,7 +679,8 @@ impl ExprCollector<'_> {
}
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
- let ast_id = self.expander.ast_id(&block);
+ let file_local_id = self.ast_id_map.ast_id(&block);
+ let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
let block_loc =
BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
let block_id = self.db.intern_block(block_loc);