@@ -60,7 +60,7 @@ mod tests;
6060use std:: { cmp:: Ord , ops:: Deref } ;
6161
6262use base_db:: { CrateId , Edition , FileId , ProcMacroKind } ;
63- use hir_expand:: { name:: Name , HirFileId , InFile , MacroCallId , MacroDefId } ;
63+ use hir_expand:: { ast_id_map :: FileAstId , name:: Name , HirFileId , InFile , MacroCallId , MacroDefId } ;
6464use itertools:: Itertools ;
6565use la_arena:: Arena ;
6666use profile:: Count ;
@@ -217,25 +217,30 @@ pub enum ModuleOrigin {
217217 /// Note that non-inline modules, by definition, live inside non-macro file.
218218 File {
219219 is_mod_rs : bool ,
220- declaration : AstId < ast:: Module > ,
220+ declaration : FileAstId < ast:: Module > ,
221221 declaration_tree_id : ItemTreeId < Mod > ,
222222 definition : FileId ,
223223 } ,
224224 Inline {
225225 definition_tree_id : ItemTreeId < Mod > ,
226- definition : AstId < ast:: Module > ,
226+ definition : FileAstId < ast:: Module > ,
227227 } ,
228228 /// Pseudo-module introduced by a block scope (contains only inner items).
229229 BlockExpr {
230+ id : BlockId ,
230231 block : AstId < ast:: BlockExpr > ,
231232 } ,
232233}
233234
234235impl ModuleOrigin {
235236 pub fn declaration ( & self ) -> Option < AstId < ast:: Module > > {
236237 match self {
237- ModuleOrigin :: File { declaration : module, .. }
238- | ModuleOrigin :: Inline { definition : module, .. } => Some ( * module) ,
238+ & ModuleOrigin :: File { declaration, declaration_tree_id, .. } => {
239+ Some ( AstId :: new ( declaration_tree_id. file_id ( ) , declaration) )
240+ }
241+ & ModuleOrigin :: Inline { definition, definition_tree_id } => {
242+ Some ( AstId :: new ( definition_tree_id. file_id ( ) , definition) )
243+ }
239244 ModuleOrigin :: CrateRoot { .. } | ModuleOrigin :: BlockExpr { .. } => None ,
240245 }
241246 }
@@ -260,16 +265,17 @@ impl ModuleOrigin {
260265 /// That is, a file or a `mod foo {}` with items.
261266 fn definition_source ( & self , db : & dyn DefDatabase ) -> InFile < ModuleSource > {
262267 match self {
263- ModuleOrigin :: File { definition, .. } | ModuleOrigin :: CrateRoot { definition } => {
264- let file_id = * definition;
265- let sf = db. parse ( file_id) . tree ( ) ;
266- InFile :: new ( file_id. into ( ) , ModuleSource :: SourceFile ( sf) )
268+ & ModuleOrigin :: File { definition, .. } | & ModuleOrigin :: CrateRoot { definition } => {
269+ let sf = db. parse ( definition) . tree ( ) ;
270+ InFile :: new ( definition. into ( ) , ModuleSource :: SourceFile ( sf) )
267271 }
268- ModuleOrigin :: Inline { definition, .. } => InFile :: new (
269- definition. file_id ,
270- ModuleSource :: Module ( definition. to_node ( db. upcast ( ) ) ) ,
272+ & ModuleOrigin :: Inline { definition, definition_tree_id } => InFile :: new (
273+ definition_tree_id. file_id ( ) ,
274+ ModuleSource :: Module (
275+ AstId :: new ( definition_tree_id. file_id ( ) , definition) . to_node ( db. upcast ( ) ) ,
276+ ) ,
271277 ) ,
272- ModuleOrigin :: BlockExpr { block } => {
278+ ModuleOrigin :: BlockExpr { block, .. } => {
273279 InFile :: new ( block. file_id , ModuleSource :: BlockExpr ( block. to_node ( db. upcast ( ) ) ) )
274280 }
275281 }
@@ -314,9 +320,7 @@ impl DefMap {
314320 }
315321
316322 pub ( crate ) fn block_def_map_query ( db : & dyn DefDatabase , block_id : BlockId ) -> Arc < DefMap > {
317- let block: BlockLoc = db. lookup_intern_block ( block_id) ;
318-
319- let tree_id = TreeId :: new ( block. ast_id . file_id , Some ( block_id) ) ;
323+ let block: BlockLoc = block_id. lookup ( db) ;
320324
321325 let parent_map = block. module . def_map ( db) ;
322326 let krate = block. module . krate ;
@@ -325,8 +329,10 @@ impl DefMap {
325329 // modules declared by blocks with items. At the moment, we don't use
326330 // this visibility for anything outside IDE, so that's probably OK.
327331 let visibility = Visibility :: Module ( ModuleId { krate, local_id, block : None } ) ;
328- let module_data =
329- ModuleData :: new ( ModuleOrigin :: BlockExpr { block : block. ast_id } , visibility) ;
332+ let module_data = ModuleData :: new (
333+ ModuleOrigin :: BlockExpr { block : block. ast_id , id : block_id } ,
334+ visibility,
335+ ) ;
330336
331337 let mut def_map = DefMap :: empty ( krate, parent_map. data . edition , module_data) ;
332338 def_map. data = parent_map. data . clone ( ) ;
@@ -338,7 +344,8 @@ impl DefMap {
338344 } ,
339345 } ) ;
340346
341- let def_map = collector:: collect_defs ( db, def_map, tree_id) ;
347+ let def_map =
348+ collector:: collect_defs ( db, def_map, TreeId :: new ( block. ast_id . file_id , Some ( block_id) ) ) ;
342349 Arc :: new ( def_map)
343350 }
344351
@@ -642,8 +649,8 @@ impl ModuleData {
642649 ModuleOrigin :: File { definition, .. } | ModuleOrigin :: CrateRoot { definition } => {
643650 definition. into ( )
644651 }
645- ModuleOrigin :: Inline { definition , .. } => definition . file_id ,
646- ModuleOrigin :: BlockExpr { block } => block. file_id ,
652+ ModuleOrigin :: Inline { definition_tree_id , .. } => definition_tree_id . file_id ( ) ,
653+ ModuleOrigin :: BlockExpr { block, .. } => block. file_id ,
647654 }
648655 }
649656
0 commit comments