@@ -48,7 +48,7 @@ pub mod find_path;
4848pub mod import_map;
4949pub mod visibility;
5050
51- use intern:: Interned ;
51+ use intern:: { Interned , sym } ;
5252use rustc_abi:: ExternAbi ;
5353use thin_vec:: ThinVec ;
5454
@@ -472,6 +472,11 @@ pub struct ModuleIdLt<'db> {
472472 /// `BlockId` of that block expression. If `None`, this module is part of the crate-level
473473 /// `DefMap` of `krate`.
474474 pub block : Option < BlockId > ,
475+ /// The parent module of this module, or `None` if this is the root module inside the def
476+ /// map (including for block def maps).
477+ pub containing_module_inside_def_map : Option < ModuleIdLt < ' db > > ,
478+ /// The name of this module, or [`sym::__empty`] for the root module.
479+ name_or_empty : Name ,
475480}
476481pub type ModuleId = ModuleIdLt < ' static > ;
477482
@@ -517,21 +522,23 @@ impl ModuleId {
517522 }
518523
519524 pub fn name ( self , db : & dyn DefDatabase ) -> Option < Name > {
520- let def_map = self . def_map ( db) ;
521- let parent = def_map[ self ] . parent ?;
522- def_map[ parent] . children . iter ( ) . find_map ( |( name, module_id) | {
523- if * module_id == self { Some ( name. clone ( ) ) } else { None }
524- } )
525+ let name = self . name_or_empty ( db) ;
526+ if * name. symbol ( ) == sym:: __empty { None } else { Some ( name) }
525527 }
526528
527529 /// Returns the module containing `self`, either the parent `mod`, or the module (or block) containing
528530 /// the block, if `self` corresponds to a block expression.
529531 pub fn containing_module ( self , db : & dyn DefDatabase ) -> Option < ModuleId > {
530- self . def_map ( db) . containing_module ( self )
532+ self . containing_module_inside_def_map ( db)
533+ . or_else ( || self . block ( db) . map ( |block| block. loc ( db) . module ) )
534+ . map ( |module| {
535+ // SAFETY: Not sure.
536+ unsafe { module. to_static ( ) }
537+ } )
531538 }
532539
533540 pub fn is_block_module ( self , db : & dyn DefDatabase ) -> bool {
534- self . block ( db) . is_some ( ) && self . def_map ( db) . root_module_id ( ) == self
541+ self . block ( db) . is_some ( ) && self . containing_module_inside_def_map ( db) . is_none ( )
535542 }
536543}
537544
0 commit comments