@@ -13,11 +13,12 @@ use cfg::{CfgExpr, CfgOptions};
1313use drop_bomb:: DropBomb ;
1414use either:: Either ;
1515use hir_expand:: {
16- attrs:: RawAttrs , hygiene:: Hygiene , name:: Name , ExpandError , ExpandResult , HirFileId , InFile ,
17- MacroCallId ,
16+ ast_id_map :: AstIdMap , attrs:: RawAttrs , hygiene:: Hygiene , name:: Name , AstId , ExpandError ,
17+ ExpandResult , HirFileId , InFile , MacroCallId ,
1818} ;
1919use la_arena:: { Arena , ArenaMap } ;
2020use limit:: Limit ;
21+ use once_cell:: unsync:: OnceCell ;
2122use profile:: Count ;
2223use rustc_hash:: FxHashMap ;
2324use syntax:: { ast, AstPtr , SyntaxNode , SyntaxNodePtr } ;
@@ -37,7 +38,43 @@ use crate::{
3738 UnresolvedMacro ,
3839} ;
3940
40- pub use lower:: LowerCtx ;
41+ pub struct LowerCtx < ' a > {
42+ pub db : & ' a dyn DefDatabase ,
43+ hygiene : Hygiene ,
44+ ast_id_map : Option < ( HirFileId , OnceCell < Arc < AstIdMap > > ) > ,
45+ }
46+
47+ impl < ' a > LowerCtx < ' a > {
48+ pub fn new ( db : & ' a dyn DefDatabase , hygiene : & Hygiene , file_id : HirFileId ) -> Self {
49+ LowerCtx { db, hygiene : hygiene. clone ( ) , ast_id_map : Some ( ( file_id, OnceCell :: new ( ) ) ) }
50+ }
51+
52+ pub fn with_file_id ( db : & ' a dyn DefDatabase , file_id : HirFileId ) -> Self {
53+ LowerCtx {
54+ db,
55+ hygiene : Hygiene :: new ( db. upcast ( ) , file_id) ,
56+ ast_id_map : Some ( ( file_id, OnceCell :: new ( ) ) ) ,
57+ }
58+ }
59+
60+ pub fn with_hygiene ( db : & ' a dyn DefDatabase , hygiene : & Hygiene ) -> Self {
61+ LowerCtx { db, hygiene : hygiene. clone ( ) , ast_id_map : None }
62+ }
63+
64+ pub ( crate ) fn hygiene ( & self ) -> & Hygiene {
65+ & self . hygiene
66+ }
67+
68+ pub ( crate ) fn lower_path ( & self , ast : ast:: Path ) -> Option < Path > {
69+ Path :: from_src ( ast, self )
70+ }
71+
72+ pub ( crate ) fn ast_id < N : syntax:: AstNode > ( & self , item : & N ) -> Option < AstId < N > > {
73+ let & ( file_id, ref ast_id_map) = self . ast_id_map . as_ref ( ) ?;
74+ let ast_id_map = ast_id_map. get_or_init ( || self . db . ast_id_map ( file_id) ) ;
75+ Some ( InFile :: new ( file_id, ast_id_map. ast_id ( item) ) )
76+ }
77+ }
4178
4279/// A subset of Expander that only deals with cfg attributes. We only need it to
4380/// avoid cyclic queries in crate def map during enum processing.
@@ -241,7 +278,7 @@ impl Expander {
241278 // The overflow error should have been reported when it occurred (see the next branch),
242279 // so don't return overflow error here to avoid diagnostics duplication.
243280 cov_mark:: hit!( overflow_but_not_me) ;
244- return ExpandResult :: only_err ( ExpandError :: RecursionOverflowPosioned ) ;
281+ return ExpandResult :: only_err ( ExpandError :: RecursionOverflowPoisoned ) ;
245282 } else if self . recursion_limit ( db) . check ( self . recursion_depth + 1 ) . is_err ( ) {
246283 self . recursion_depth = usize:: MAX ;
247284 cov_mark:: hit!( your_stack_belongs_to_me) ;
0 commit comments