@@ -5,12 +5,13 @@ use std::{mem, sync::Arc};
55
66use either:: Either ;
77use 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} ;
1313use la_arena:: Arena ;
14+ use once_cell:: unsync:: OnceCell ;
1415use profile:: Count ;
1516use rustc_hash:: FxHashMap ;
1617use syntax:: {
@@ -41,38 +42,34 @@ 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
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(
105103struct 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) ;
0 commit comments