@@ -533,7 +533,7 @@ enum ModuleKind {
533533 /// }
534534 /// ```
535535 Block ,
536- /// Any module with a name .
536+ /// A local non-block module .
537537 ///
538538 /// This could be:
539539 ///
@@ -542,13 +542,16 @@ enum ModuleKind {
542542 /// The crate root will have `None` for the symbol.
543543 /// * A trait or an enum (it implicitly contains associated types, methods and variant
544544 /// constructors).
545- Def ( DefKind , DefId , NodeId , Option < Symbol > ) ,
545+ Local ( DefKind , LocalDefId , NodeId , Option < Symbol > ) ,
546+ /// An external module, non-block by definition.
547+ Extern ( DefKind , DefId , Symbol ) ,
546548}
547549
548550impl ModuleKind {
549551 fn opt_def_id ( & self ) -> Option < DefId > {
550552 match self {
551- ModuleKind :: Def ( _, def_id, _, _) => Some ( * def_id) ,
553+ ModuleKind :: Local ( _, def_id, _, _) => Some ( def_id. to_def_id ( ) ) ,
554+ ModuleKind :: Extern ( _, def_id, _) => Some ( * def_id) ,
552555 _ => None ,
553556 }
554557 }
@@ -557,12 +560,17 @@ impl ModuleKind {
557560 self . opt_def_id ( ) . expect ( "`Module::def_id` is called on a block module" )
558561 }
559562
560- fn is_local ( & self ) -> bool {
563+ fn def ( & self ) -> Option < ( DefKind , DefId ) > {
561564 match self {
562- ModuleKind :: Def ( _, def_id, ..) => def_id. is_local ( ) ,
563- ModuleKind :: Block => true ,
565+ ModuleKind :: Local ( def_kind, def_id, _, _) => Some ( ( * def_kind, def_id. to_def_id ( ) ) ) ,
566+ ModuleKind :: Extern ( def_kind, def_id, _) => Some ( ( * def_kind, * def_id) ) ,
567+ ModuleKind :: Block => None ,
564568 }
565569 }
570+
571+ fn is_local ( & self ) -> bool {
572+ matches ! ( self , ModuleKind :: Local ( ..) | ModuleKind :: Block )
573+ }
566574}
567575
568576/// Combination of a symbol and its macros 2.0 normalized hygiene context.
@@ -729,13 +737,10 @@ impl<'ra> ModuleData<'ra> {
729737 arenas : & ' ra ResolverArenas < ' ra > ,
730738 ) -> Self {
731739 let is_foreign = !kind. is_local ( ) ;
732- let self_decl = match kind {
733- ModuleKind :: Def ( def_kind, def_id, ..) => {
734- let expn_id = expansion. as_local ( ) . unwrap_or ( LocalExpnId :: ROOT ) ;
735- Some ( arenas. new_def_decl ( Res :: Def ( def_kind, def_id) , vis, span, expn_id, parent) )
736- }
737- ModuleKind :: Block => None ,
738- } ;
740+ let self_decl = kind. def ( ) . map ( |( def_kind, def_id) | {
741+ let expn_id = expansion. as_local ( ) . unwrap_or ( LocalExpnId :: ROOT ) ;
742+ arenas. new_def_decl ( Res :: Def ( def_kind, def_id) , vis, span, expn_id, parent)
743+ } ) ;
739744 ModuleData {
740745 parent,
741746 kind,
@@ -757,7 +762,8 @@ impl<'ra> ModuleData<'ra> {
757762 fn name ( & self ) -> Option < Symbol > {
758763 match self . kind {
759764 ModuleKind :: Block => None ,
760- ModuleKind :: Def ( .., name) => name,
765+ ModuleKind :: Local ( .., name) => name,
766+ ModuleKind :: Extern ( .., name) => Some ( name) ,
761767 }
762768 }
763769
@@ -769,6 +775,10 @@ impl<'ra> ModuleData<'ra> {
769775 self . kind . def_id ( )
770776 }
771777
778+ fn def ( & self ) -> Option < ( DefKind , DefId ) > {
779+ self . kind . def ( )
780+ }
781+
772782 fn is_local ( & self ) -> bool {
773783 self . kind . is_local ( )
774784 }
@@ -779,14 +789,15 @@ impl<'ra> ModuleData<'ra> {
779789
780790 fn res ( & self ) -> Option < Res > {
781791 match self . kind {
782- ModuleKind :: Def ( kind, def_id, _, _) => Some ( Res :: Def ( kind, def_id) ) ,
792+ ModuleKind :: Local ( kind, def_id, _, _) => Some ( Res :: Def ( kind, def_id. to_def_id ( ) ) ) ,
793+ ModuleKind :: Extern ( kind, def_id, _) => Some ( Res :: Def ( kind, def_id) ) ,
783794 _ => None ,
784795 }
785796 }
786797
787798 fn def_kind ( & self ) -> Option < DefKind > {
788799 match self . kind {
789- ModuleKind :: Def ( def_kind, ..) => Some ( def_kind) ,
800+ ModuleKind :: Local ( def_kind , .. ) | ModuleKind :: Extern ( def_kind, ..) => Some ( def_kind) ,
790801 ModuleKind :: Block => None ,
791802 }
792803 }
@@ -863,7 +874,8 @@ impl<'ra> Module<'ra> {
863874 /// This may be the crate root.
864875 fn nearest_parent_mod ( self ) -> DefId {
865876 match self . kind {
866- ModuleKind :: Def ( DefKind :: Mod , def_id, _, _) => def_id,
877+ ModuleKind :: Local ( DefKind :: Mod , def_id, _, _) => def_id. to_def_id ( ) ,
878+ ModuleKind :: Extern ( DefKind :: Mod , def_id, _) => def_id,
867879 _ => self . parent . expect ( "non-root module without parent" ) . nearest_parent_mod ( ) ,
868880 }
869881 }
@@ -872,7 +884,8 @@ impl<'ra> Module<'ra> {
872884 /// This may be the crate root.
873885 fn nearest_parent_mod_node_id ( self ) -> NodeId {
874886 match self . kind {
875- ModuleKind :: Def ( DefKind :: Mod , _, node_id, _) => node_id,
887+ ModuleKind :: Local ( DefKind :: Mod , _, node_id, _) => node_id,
888+ ModuleKind :: Extern ( ..) => ast:: DUMMY_NODE_ID ,
876889 _ => self . parent . expect ( "non-root module without parent" ) . nearest_parent_mod_node_id ( ) ,
877890 }
878891 }
@@ -891,20 +904,16 @@ impl<'ra> Module<'ra> {
891904 #[ track_caller]
892905 fn expect_local ( self ) -> LocalModule < ' ra > {
893906 match self . kind {
894- ModuleKind :: Def ( _, def_id, _, _) if !def_id. is_local ( ) => {
895- span_bug ! ( self . span, "unexpected extern module: {self:?}" )
896- }
897- ModuleKind :: Def ( ..) | ModuleKind :: Block => LocalModule ( self . 0 ) ,
907+ ModuleKind :: Local ( ..) | ModuleKind :: Block => LocalModule ( self . 0 ) ,
908+ _ => span_bug ! ( self . span, "unexpected extern module: {self:?}" ) ,
898909 }
899910 }
900911
901912 #[ track_caller]
902913 fn expect_extern ( self ) -> ExternModule < ' ra > {
903914 match self . kind {
904- ModuleKind :: Def ( _, def_id, _, _) if !def_id. is_local ( ) => ExternModule ( self . 0 ) ,
905- ModuleKind :: Def ( ..) | ModuleKind :: Block => {
906- span_bug ! ( self . span, "unexpected local module: {self:?}" )
907- }
915+ ModuleKind :: Extern ( ..) => ExternModule ( self . 0 ) ,
916+ _ => span_bug ! ( self . span, "unexpected local module: {self:?}" ) ,
908917 }
909918 }
910919}
@@ -1757,10 +1766,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17571766 current_crate_outer_attr_insert_span : Span ,
17581767 arenas : & ' ra ResolverArenas < ' ra > ,
17591768 ) -> Resolver < ' ra , ' tcx > {
1760- let root_def_id = CRATE_DEF_ID . to_def_id ( ) ;
17611769 let graph_root = LocalModule :: new (
17621770 None ,
1763- ModuleKind :: Def ( DefKind :: Mod , root_def_id , CRATE_NODE_ID , None ) ,
1771+ ModuleKind :: Local ( DefKind :: Mod , CRATE_DEF_ID , CRATE_NODE_ID , None ) ,
17641772 Visibility :: Public ,
17651773 ExpnId :: root ( ) ,
17661774 crate_span,
@@ -1771,7 +1779,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17711779 let local_module_map = FxIndexMap :: from_iter ( [ ( CRATE_DEF_ID , graph_root) ] ) ;
17721780 let empty_module = LocalModule :: new (
17731781 None ,
1774- ModuleKind :: Def ( DefKind :: Mod , root_def_id , CRATE_NODE_ID , None ) ,
1782+ ModuleKind :: Local ( DefKind :: Mod , CRATE_DEF_ID , CRATE_NODE_ID , None ) ,
17751783 Visibility :: Public ,
17761784 ExpnId :: root ( ) ,
17771785 DUMMY_SP ,
0 commit comments