@@ -11,6 +11,7 @@ use hir_expand::{
11
11
ExpandError , HirFileId , InFile ,
12
12
} ;
13
13
use la_arena:: Arena ;
14
+ use once_cell:: unsync:: OnceCell ;
14
15
use profile:: Count ;
15
16
use rustc_hash:: FxHashMap ;
16
17
use syntax:: {
@@ -41,38 +42,42 @@ use crate::{
41
42
pub struct LowerCtx < ' a > {
42
43
pub db : & ' a dyn DefDatabase ,
43
44
hygiene : Hygiene ,
44
- file_id : Option < HirFileId > ,
45
- source_ast_id_map : Option < Arc < AstIdMap > > ,
45
+ ast_id_map : Option < ( HirFileId , OnceCell < Arc < AstIdMap > > ) > ,
46
46
}
47
47
48
48
impl < ' a > LowerCtx < ' a > {
49
49
pub fn new ( db : & ' a dyn DefDatabase , file_id : HirFileId ) -> Self {
50
50
LowerCtx {
51
51
db,
52
52
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 ( ) ) ) ,
55
54
}
56
55
}
57
56
58
57
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 }
60
59
}
61
60
62
61
pub ( crate ) fn hygiene ( & self ) -> & Hygiene {
63
62
& self . hygiene
64
63
}
65
64
66
65
pub ( crate ) fn file_id ( & self ) -> HirFileId {
67
- self . file_id . unwrap ( )
66
+ self . ast_id_map . as_ref ( ) . unwrap ( ) . 0
68
67
}
69
68
70
69
pub ( crate ) fn lower_path ( & self , ast : ast:: Path ) -> Option < Path > {
71
70
Path :: from_src ( ast, self )
72
71
}
73
72
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) )
76
81
}
77
82
}
78
83
@@ -675,7 +680,7 @@ impl ExprCollector<'_> {
675
680
}
676
681
677
682
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) ;
679
684
let block_loc =
680
685
BlockLoc { ast_id, module : self . expander . def_map . module_id ( self . expander . module ) } ;
681
686
let block_id = self . db . intern_block ( block_loc) ;
0 commit comments