@@ -657,8 +657,6 @@ type Resolutions<'ra> = CmRefCell<FxIndexMap<BindingKey, &'ra CmRefCell<NameReso
657657///
658658/// You can use [`CommonModuleData::kind`] to determine the kind of module this is.
659659struct CommonModuleData < ' ra > {
660- /// The direct parent module (it may not be a `mod`, however).
661- parent : Option < Module < ' ra > > ,
662660 /// What kind of module this is, because this may not be a `mod`.
663661 kind : ModuleKind ,
664662 /// Mapping between names and their (possibly in-progress) resolutions in this module.
@@ -680,6 +678,8 @@ struct CommonModuleData<'ra> {
680678
681679struct LocalModuleData < ' ra > {
682680 common : CommonModuleData < ' ra > ,
681+ /// The direct parent module (it may not be a `mod`, however).
682+ parent : Option < LocalModule < ' ra > > ,
683683 /// Used to disambiguate underscore items (`const _: T = ...`) in the module.
684684 underscore_disambiguator : CmCell < u32 > ,
685685 /// Macro invocations that can expand into items in this module.
@@ -690,6 +690,8 @@ struct LocalModuleData<'ra> {
690690
691691struct ExternModuleData < ' ra > {
692692 common : CommonModuleData < ' ra > ,
693+ /// The direct parent module (it may not be a `mod`, however).
694+ parent : Option < ExternModule < ' ra > > ,
693695 /// True if this is a module from other crate that needs to be populated on access.
694696 populate_on_access : CacheCell < bool > ,
695697}
@@ -715,7 +717,6 @@ struct ExternModule<'ra>(Interned<'ra, ExternModuleData<'ra>>);
715717
716718impl < ' ra > CommonModuleData < ' ra > {
717719 fn new (
718- parent : Option < Module < ' ra > > ,
719720 kind : ModuleKind ,
720721 vis : Visibility < DefId > ,
721722 expansion : ExpnId ,
@@ -731,7 +732,6 @@ impl<'ra> CommonModuleData<'ra> {
731732 ModuleKind :: Block => None ,
732733 } ;
733734 CommonModuleData {
734- parent,
735735 kind,
736736 lazy_resolutions : Default :: default ( ) ,
737737 no_implicit_prelude,
@@ -819,7 +819,7 @@ impl<'ra> Module<'ra> {
819819 fn nearest_item_scope ( self ) -> Module < ' ra > {
820820 match self . kind {
821821 ModuleKind :: Def ( DefKind :: Enum | DefKind :: Trait , ..) => {
822- self . parent . expect ( "enum or trait module without a parent" )
822+ self . parent ( ) . expect ( "enum or trait module without a parent" )
823823 }
824824 _ => self ,
825825 }
@@ -830,7 +830,7 @@ impl<'ra> Module<'ra> {
830830 fn nearest_parent_mod ( self ) -> DefId {
831831 match self . kind {
832832 ModuleKind :: Def ( DefKind :: Mod , def_id, _, _) => def_id,
833- _ => self . parent . expect ( "non-root module without parent" ) . nearest_parent_mod ( ) ,
833+ _ => self . parent ( ) . expect ( "non-root module without parent" ) . nearest_parent_mod ( ) ,
834834 }
835835 }
836836
@@ -839,13 +839,15 @@ impl<'ra> Module<'ra> {
839839 fn nearest_parent_mod_node_id ( self ) -> NodeId {
840840 match self . kind {
841841 ModuleKind :: Def ( DefKind :: Mod , _, node_id, _) => node_id,
842- _ => self . parent . expect ( "non-root module without parent" ) . nearest_parent_mod_node_id ( ) ,
842+ _ => {
843+ self . parent ( ) . expect ( "non-root module without parent" ) . nearest_parent_mod_node_id ( )
844+ }
843845 }
844846 }
845847
846848 fn is_ancestor_of ( self , mut other : Self ) -> bool {
847849 while self != other {
848- if let Some ( parent) = other. parent {
850+ if let Some ( parent) = other. parent ( ) {
849851 other = parent;
850852 } else {
851853 return false ;
@@ -883,11 +885,18 @@ impl<'ra> Module<'ra> {
883885 Module :: Extern ( _) => false ,
884886 }
885887 }
888+
889+ fn parent ( self ) -> Option < Module < ' ra > > {
890+ match self {
891+ Module :: Local ( m) => m. parent . map ( Module :: Local ) ,
892+ Module :: Extern ( m) => m. parent . map ( Module :: Extern ) ,
893+ }
894+ }
886895}
887896
888897impl < ' ra > LocalModule < ' ra > {
889898 fn new (
890- parent : Option < Module < ' ra > > ,
899+ parent : Option < LocalModule < ' ra > > ,
891900 kind : ModuleKind ,
892901 vis : Visibility < DefId > ,
893902 expn_id : ExpnId ,
@@ -896,10 +905,10 @@ impl<'ra> LocalModule<'ra> {
896905 arenas : & ' ra ResolverArenas < ' ra > ,
897906 ) -> LocalModule < ' ra > {
898907 assert ! ( kind. is_local( ) ) ;
899- let common =
900- CommonModuleData :: new ( parent, kind, vis, expn_id, span, no_implicit_prelude, arenas) ;
908+ let common = CommonModuleData :: new ( kind, vis, expn_id, span, no_implicit_prelude, arenas) ;
901909 let data = LocalModuleData {
902910 common,
911+ parent,
903912 underscore_disambiguator : CmCell :: new ( 0 ) ,
904913 unexpanded_invocations : Default :: default ( ) ,
905914 glob_importers : Default :: default ( ) ,
@@ -919,7 +928,7 @@ impl<'ra> LocalModule<'ra> {
919928
920929impl < ' ra > ExternModule < ' ra > {
921930 fn new (
922- parent : Option < Module < ' ra > > ,
931+ parent : Option < ExternModule < ' ra > > ,
923932 kind : ModuleKind ,
924933 vis : Visibility < DefId > ,
925934 expn_id : ExpnId ,
@@ -928,9 +937,8 @@ impl<'ra> ExternModule<'ra> {
928937 arenas : & ' ra ResolverArenas < ' ra > ,
929938 ) -> ExternModule < ' ra > {
930939 assert ! ( !kind. is_local( ) ) ;
931- let common =
932- CommonModuleData :: new ( parent, kind, vis, expn_id, span, no_implicit_prelude, arenas) ;
933- let data = ExternModuleData { common, populate_on_access : CacheCell :: new ( true ) } ;
940+ let common = CommonModuleData :: new ( kind, vis, expn_id, span, no_implicit_prelude, arenas) ;
941+ let data = ExternModuleData { common, parent, populate_on_access : CacheCell :: new ( true ) } ;
934942 ExternModule ( Interned :: new_unchecked ( arenas. extern_modules . alloc ( data) ) )
935943 }
936944
@@ -1904,7 +1912,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19041912 span : Span ,
19051913 no_implicit_prelude : bool ,
19061914 ) -> LocalModule < ' ra > {
1907- let parent = parent. map ( |m| m. to_module ( ) ) ;
19081915 let vis =
19091916 kind. opt_def_id ( ) . map_or ( Visibility :: Public , |def_id| self . tcx . visibility ( def_id) ) ;
19101917 let module =
@@ -1924,7 +1931,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19241931 span : Span ,
19251932 no_implicit_prelude : bool ,
19261933 ) -> ExternModule < ' ra > {
1927- let parent = parent. map ( |m| m. to_module ( ) ) ;
19281934 let vis =
19291935 kind. opt_def_id ( ) . map_or ( Visibility :: Public , |def_id| self . tcx . visibility ( def_id) ) ;
19301936 let module =
@@ -2415,7 +2421,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24152421 fn resolve_self ( & self , ctxt : & mut SyntaxContext , module : Module < ' ra > ) -> Module < ' ra > {
24162422 let mut module = self . expect_module ( module. nearest_parent_mod ( ) ) ;
24172423 while module. span . ctxt ( ) . normalize_to_macros_2_0 ( ) != * ctxt {
2418- let parent = module. parent . unwrap_or_else ( || self . expn_def_scope ( ctxt. remove_mark ( ) ) ) ;
2424+ let parent = module. parent ( ) . unwrap_or_else ( || self . expn_def_scope ( ctxt. remove_mark ( ) ) ) ;
24192425 module = self . expect_module ( parent. nearest_parent_mod ( ) ) ;
24202426 }
24212427 module
@@ -2752,7 +2758,7 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
27522758 let mut names = Vec :: new ( ) ;
27532759 loop {
27542760 if let ModuleKind :: Def ( .., name) = module. kind {
2755- if let Some ( parent) = module. parent {
2761+ if let Some ( parent) = module. parent ( ) {
27562762 // `unwrap` is safe: the presence of a parent means it's not the crate root.
27572763 names. push ( name. unwrap ( ) ) ;
27582764 module = parent
@@ -2761,7 +2767,7 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
27612767 }
27622768 } else {
27632769 names. push ( sym:: opaque_module_name_placeholder) ;
2764- let Some ( parent) = module. parent else {
2770+ let Some ( parent) = module. parent ( ) else {
27652771 return None ;
27662772 } ;
27672773 module = parent;
0 commit comments