@@ -11,6 +11,7 @@ use hir_expand::{
1111 ExpandError , HirFileId , InFile ,
1212} ;
1313use la_arena:: Arena ;
14+ use once_cell:: unsync:: OnceCell ;
1415use profile:: Count ;
1516use rustc_hash:: FxHashMap ;
1617use syntax:: {
@@ -41,38 +42,42 @@ use crate::{
4142pub 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
4848impl < ' 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
6665 pub ( crate ) fn file_id ( & self ) -> HirFileId {
67- self . file_id . unwrap ( )
66+ self . ast_id_map . as_ref ( ) . unwrap ( ) . 0
6867 }
6968
7069 pub ( crate ) fn lower_path ( & self , ast : ast:: Path ) -> Option < Path > {
7170 Path :: from_src ( ast, self )
7271 }
7372
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) )
73+ pub ( crate ) fn ast_id < N : AstNode > (
74+ & self ,
75+ db : & dyn DefDatabase ,
76+ item : & N ,
77+ ) -> Option < FileAstId < N > > {
78+ let ( file_id, ast_id_map) = self . ast_id_map . as_ref ( ) ?;
79+ let ast_id_map = ast_id_map. get_or_init ( || db. ast_id_map ( * file_id) ) ;
80+ Some ( ast_id_map. ast_id ( item) )
7681 }
7782}
7883
@@ -675,7 +680,7 @@ impl ExprCollector<'_> {
675680 }
676681
677682 fn collect_block ( & mut self , block : ast:: BlockExpr ) -> ExprId {
678- let ast_id = self . expander . ast_id ( & block) ;
683+ let ast_id = self . expander . ast_id ( self . db , & block) ;
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) ;
0 commit comments