Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/lower.rs')
-rw-r--r--crates/hir-def/src/lower.rs37
1 files changed, 17 insertions, 20 deletions
diff --git a/crates/hir-def/src/lower.rs b/crates/hir-def/src/lower.rs
index 2fa6acdf17..d574d80a8e 100644
--- a/crates/hir-def/src/lower.rs
+++ b/crates/hir-def/src/lower.rs
@@ -13,39 +13,36 @@ use crate::{db::DefDatabase, path::Path};
pub struct LowerCtx<'a> {
pub db: &'a dyn DefDatabase,
- span_map: SpanMap,
- // FIXME: This optimization is probably pointless, ast id map should pretty much always exist anyways.
- ast_id_map: Option<(HirFileId, OnceCell<Arc<AstIdMap>>)>,
+ file_id: HirFileId,
+ span_map: OnceCell<SpanMap>,
+ ast_id_map: OnceCell<Arc<AstIdMap>>,
}
impl<'a> LowerCtx<'a> {
- pub fn new(db: &'a dyn DefDatabase, span_map: SpanMap, file_id: HirFileId) -> Self {
- LowerCtx { db, span_map, ast_id_map: Some((file_id, OnceCell::new())) }
+ pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
+ LowerCtx { db, file_id, span_map: OnceCell::new(), ast_id_map: OnceCell::new() }
}
- pub fn with_file_id(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
- LowerCtx {
- db,
- span_map: db.span_map(file_id),
- ast_id_map: Some((file_id, OnceCell::new())),
- }
- }
-
- pub fn with_span_map(db: &'a dyn DefDatabase, span_map: SpanMap) -> Self {
- LowerCtx { db, span_map, ast_id_map: None }
+ pub fn with_span_map_cell(
+ db: &'a dyn DefDatabase,
+ file_id: HirFileId,
+ span_map: OnceCell<SpanMap>,
+ ) -> Self {
+ LowerCtx { db, file_id, span_map, ast_id_map: OnceCell::new() }
}
pub(crate) fn span_map(&self) -> SpanMapRef<'_> {
- self.span_map.as_ref()
+ self.span_map.get_or_init(|| self.db.span_map(self.file_id)).as_ref()
}
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
Path::from_src(self, ast)
}
- pub(crate) fn ast_id<N: AstIdNode>(&self, 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(|| self.db.ast_id_map(file_id));
- Some(InFile::new(file_id, ast_id_map.ast_id(item)))
+ pub(crate) fn ast_id<N: AstIdNode>(&self, item: &N) -> AstId<N> {
+ InFile::new(
+ self.file_id,
+ self.ast_id_map.get_or_init(|| self.db.ast_id_map(self.file_id)).ast_id(item),
+ )
}
}