@@ -85,7 +85,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
8585 /// Reachable macros with block module parents exist due to `#[macro_export] macro_rules!`,
8686 /// but they cannot use def-site hygiene, so the assumption holds
8787 /// (<https://github.com/rust-lang/rust/pull/77984#issuecomment-712445508>).
88- pub ( crate ) fn get_nearest_non_block_module ( & mut self , mut def_id : DefId ) -> Module < ' ra > {
88+ pub ( crate ) fn get_nearest_non_block_module ( & self , mut def_id : DefId ) -> Module < ' ra > {
8989 loop {
9090 match self . get_module ( def_id) {
9191 Some ( module) => return module,
@@ -94,44 +94,47 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
9494 }
9595 }
9696
97- pub ( crate ) fn expect_module ( & mut self , def_id : DefId ) -> Module < ' ra > {
97+ pub ( crate ) fn expect_module ( & self , def_id : DefId ) -> Module < ' ra > {
9898 self . get_module ( def_id) . expect ( "argument `DefId` is not a module" )
9999 }
100100
101101 /// If `def_id` refers to a module (in resolver's sense, i.e. a module item, crate root, enum,
102102 /// or trait), then this function returns that module's resolver representation, otherwise it
103103 /// returns `None`.
104- pub ( crate ) fn get_module ( & mut self , def_id : DefId ) -> Option < Module < ' ra > > {
105- if let module @ Some ( ..) = self . module_map . get ( & def_id) {
106- return module. copied ( ) ;
107- }
104+ pub ( crate ) fn get_module ( & self , def_id : DefId ) -> Option < Module < ' ra > > {
105+ match def_id. as_local ( ) {
106+ Some ( local_def_id) => self . local_module_map . get ( & local_def_id) . copied ( ) ,
107+ None => {
108+ if let module @ Some ( ..) = self . extern_module_map . borrow ( ) . get ( & def_id) {
109+ return module. copied ( ) ;
110+ }
108111
109- if !def_id. is_local ( ) {
110- // Query `def_kind` is not used because query system overhead is too expensive here.
111- let def_kind = self . cstore ( ) . def_kind_untracked ( def_id) ;
112- if def_kind. is_module_like ( ) {
113- let parent = self
114- . tcx
115- . opt_parent ( def_id)
116- . map ( |parent_id| self . get_nearest_non_block_module ( parent_id) ) ;
117- // Query `expn_that_defined` is not used because
118- // hashing spans in its result is expensive.
119- let expn_id = self . cstore ( ) . expn_that_defined_untracked ( def_id, self . tcx . sess ) ;
120- return Some ( self . new_module (
121- parent,
122- ModuleKind :: Def ( def_kind, def_id, Some ( self . tcx . item_name ( def_id) ) ) ,
123- expn_id,
124- self . def_span ( def_id) ,
125- // FIXME: Account for `#[no_implicit_prelude]` attributes.
126- parent. is_some_and ( |module| module. no_implicit_prelude ) ,
127- ) ) ;
112+ // Query `def_kind` is not used because query system overhead is too expensive here.
113+ let def_kind = self . cstore ( ) . def_kind_untracked ( def_id) ;
114+ if def_kind. is_module_like ( ) {
115+ let parent = self
116+ . tcx
117+ . opt_parent ( def_id)
118+ . map ( |parent_id| self . get_nearest_non_block_module ( parent_id) ) ;
119+ // Query `expn_that_defined` is not used because
120+ // hashing spans in its result is expensive.
121+ let expn_id = self . cstore ( ) . expn_that_defined_untracked ( def_id, self . tcx . sess ) ;
122+ return Some ( self . new_extern_module (
123+ parent,
124+ ModuleKind :: Def ( def_kind, def_id, Some ( self . tcx . item_name ( def_id) ) ) ,
125+ expn_id,
126+ self . def_span ( def_id) ,
127+ // FIXME: Account for `#[no_implicit_prelude]` attributes.
128+ parent. is_some_and ( |module| module. no_implicit_prelude ) ,
129+ ) ) ;
130+ }
131+
132+ None
128133 }
129134 }
130-
131- None
132135 }
133136
134- pub ( crate ) fn expn_def_scope ( & mut self , expn_id : ExpnId ) -> Module < ' ra > {
137+ pub ( crate ) fn expn_def_scope ( & self , expn_id : ExpnId ) -> Module < ' ra > {
135138 match expn_id. expn_data ( ) . macro_def_id {
136139 Some ( def_id) => self . macro_def_scope ( def_id) ,
137140 None => expn_id
@@ -141,7 +144,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
141144 }
142145 }
143146
144- pub ( crate ) fn macro_def_scope ( & mut self , def_id : DefId ) -> Module < ' ra > {
147+ pub ( crate ) fn macro_def_scope ( & self , def_id : DefId ) -> Module < ' ra > {
145148 if let Some ( id) = def_id. as_local ( ) {
146149 self . local_macro_def_scopes [ & id]
147150 } else {
@@ -403,7 +406,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
403406 self . r . field_visibility_spans . insert ( def_id, field_vis) ;
404407 }
405408
406- fn block_needs_anonymous_module ( & mut self , block : & Block ) -> bool {
409+ fn block_needs_anonymous_module ( & self , block : & Block ) -> bool {
407410 // If any statements are items, we need to create an anonymous module
408411 block
409412 . stmts
@@ -758,7 +761,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
758761 if let ast:: ModKind :: Loaded ( _, _, _, Err ( _) ) = mod_kind {
759762 self . r . mods_with_parse_errors . insert ( def_id) ;
760763 }
761- self . parent_scope . module = self . r . new_module (
764+ self . parent_scope . module = self . r . new_local_module (
762765 Some ( parent) ,
763766 ModuleKind :: Def ( def_kind, def_id, Some ( ident. name ) ) ,
764767 expansion. to_expn_id ( ) ,
@@ -790,7 +793,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
790793 ItemKind :: Enum ( ident, _, _) | ItemKind :: Trait ( box ast:: Trait { ident, .. } ) => {
791794 self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
792795
793- self . parent_scope . module = self . r . new_module (
796+ self . parent_scope . module = self . r . new_local_module (
794797 Some ( parent) ,
795798 ModuleKind :: Def ( def_kind, def_id, Some ( ident. name ) ) ,
796799 expansion. to_expn_id ( ) ,
@@ -986,7 +989,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
986989 let parent = self . parent_scope . module ;
987990 let expansion = self . parent_scope . expansion ;
988991 if self . block_needs_anonymous_module ( block) {
989- let module = self . r . new_module (
992+ let module = self . r . new_local_module (
990993 Some ( parent) ,
991994 ModuleKind :: Block ,
992995 expansion. to_expn_id ( ) ,
@@ -1118,7 +1121,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11181121 }
11191122
11201123 /// Returns `true` if this attribute list contains `macro_use`.
1121- fn contains_macro_use ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
1124+ fn contains_macro_use ( & self , attrs : & [ ast:: Attribute ] ) -> bool {
11221125 for attr in attrs {
11231126 if attr. has_name ( sym:: macro_escape) {
11241127 let inner_attribute = matches ! ( attr. style, ast:: AttrStyle :: Inner ) ;
0 commit comments