Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/expander.rs')
| -rw-r--r-- | crates/hir-def/src/expander.rs | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/crates/hir-def/src/expander.rs b/crates/hir-def/src/expander.rs index b99df1ed59..b0872fcdc0 100644 --- a/crates/hir-def/src/expander.rs +++ b/crates/hir-def/src/expander.rs @@ -1,5 +1,7 @@ //! Macro expansion utilities. +use std::cell::OnceCell; + use base_db::CrateId; use cfg::CfgOptions; use drop_bomb::DropBomb; @@ -18,7 +20,7 @@ use crate::{ #[derive(Debug)] pub struct Expander { cfg_options: CfgOptions, - span_map: SpanMap, + span_map: OnceCell<SpanMap>, krate: CrateId, current_file_id: HirFileId, pub(crate) module: ModuleId, @@ -42,7 +44,7 @@ impl Expander { recursion_depth: 0, recursion_limit, cfg_options: db.crate_graph()[module.krate].cfg_options.clone(), - span_map: db.span_map(current_file_id), + span_map: OnceCell::new(), krate: module.krate, } } @@ -100,7 +102,7 @@ impl Expander { } pub fn ctx<'a>(&self, db: &'a dyn DefDatabase) -> LowerCtx<'a> { - LowerCtx::new(db, self.span_map.clone(), self.current_file_id) + LowerCtx::with_span_map_cell(db, self.current_file_id, self.span_map.clone()) } pub(crate) fn in_file<T>(&self, value: T) -> InFile<T> { @@ -108,7 +110,15 @@ impl Expander { } pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs { - Attrs::filter(db, self.krate, RawAttrs::new(db.upcast(), owner, self.span_map.as_ref())) + Attrs::filter( + db, + self.krate, + RawAttrs::new( + db.upcast(), + owner, + self.span_map.get_or_init(|| db.span_map(self.current_file_id)).as_ref(), + ), + ) } pub(crate) fn cfg_options(&self) -> &CfgOptions { @@ -120,7 +130,7 @@ impl Expander { } pub(crate) fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> { - let ctx = LowerCtx::new(db, self.span_map.clone(), self.current_file_id); + let ctx = LowerCtx::with_span_map_cell(db, self.current_file_id, self.span_map.clone()); Path::from_src(&ctx, path) } @@ -165,10 +175,11 @@ impl Expander { let parse = res.value.0.cast::<T>()?; self.recursion_depth += 1; - let old_span_map = std::mem::replace( - &mut self.span_map, - SpanMap::ExpansionSpanMap(res.value.1), - ); + let old_span_map = OnceCell::new(); + if let Some(prev) = self.span_map.take() { + _ = old_span_map.set(prev); + }; + _ = self.span_map.set(SpanMap::ExpansionSpanMap(res.value.1)); let old_file_id = std::mem::replace(&mut self.current_file_id, macro_file.into()); let mark = Mark { @@ -187,6 +198,6 @@ impl Expander { #[derive(Debug)] pub struct Mark { file_id: HirFileId, - span_map: SpanMap, + span_map: OnceCell<SpanMap>, bomb: DropBomb, } |