1414#![ feature( iter_intersperse) ]
1515#![ feature( let_chains) ]
1616#![ feature( never_type) ]
17+ #![ feature( rustc_attrs) ]
1718#![ recursion_limit = "256" ]
1819#![ allow( rustdoc:: private_intra_doc_links) ]
1920#![ allow( rustc:: potential_query_instability) ]
@@ -61,7 +62,7 @@ use rustc_span::{Span, DUMMY_SP};
6162use smallvec:: { smallvec, SmallVec } ;
6263use std:: cell:: { Cell , RefCell } ;
6364use std:: collections:: BTreeSet ;
64- use std:: { fmt, ptr } ;
65+ use std:: fmt;
6566
6667use diagnostics:: { ImportSuggestion , LabelSuggestion , Suggestion } ;
6768use imports:: { Import , ImportData , ImportKind , NameResolution } ;
@@ -365,7 +366,7 @@ impl<'a> LexicalScopeBinding<'a> {
365366 }
366367}
367368
368- #[ derive( Copy , Clone , Debug ) ]
369+ #[ derive( Copy , Clone , PartialEq , Debug ) ]
369370enum ModuleOrUniformRoot < ' a > {
370371 /// Regular module.
371372 Module ( Module < ' a > ) ,
@@ -383,23 +384,6 @@ enum ModuleOrUniformRoot<'a> {
383384 CurrentScope ,
384385}
385386
386- impl ModuleOrUniformRoot < ' _ > {
387- fn same_def ( lhs : Self , rhs : Self ) -> bool {
388- match ( lhs, rhs) {
389- ( ModuleOrUniformRoot :: Module ( lhs) , ModuleOrUniformRoot :: Module ( rhs) ) => {
390- ptr:: eq ( lhs, rhs)
391- }
392- (
393- ModuleOrUniformRoot :: CrateRootAndExternPrelude ,
394- ModuleOrUniformRoot :: CrateRootAndExternPrelude ,
395- )
396- | ( ModuleOrUniformRoot :: ExternPrelude , ModuleOrUniformRoot :: ExternPrelude )
397- | ( ModuleOrUniformRoot :: CurrentScope , ModuleOrUniformRoot :: CurrentScope ) => true ,
398- _ => false ,
399- }
400- }
401- }
402-
403387#[ derive( Debug ) ]
404388enum PathResult < ' a > {
405389 Module ( ModuleOrUniformRoot < ' a > ) ,
@@ -530,7 +514,9 @@ struct ModuleData<'a> {
530514 expansion : ExpnId ,
531515}
532516
533- type Module < ' a > = & ' a ModuleData < ' a > ;
517+ #[ derive( Clone , Copy , PartialEq ) ]
518+ #[ rustc_pass_by_value]
519+ struct Module < ' a > ( Interned < ' a , ModuleData < ' a > > ) ;
534520
535521impl < ' a > ModuleData < ' a > {
536522 fn new (
@@ -558,8 +544,10 @@ impl<'a> ModuleData<'a> {
558544 expansion,
559545 }
560546 }
547+ }
561548
562- fn for_each_child < ' tcx , R , F > ( & ' a self , resolver : & mut R , mut f : F )
549+ impl < ' a > Module < ' a > {
550+ fn for_each_child < ' tcx , R , F > ( self , resolver : & mut R , mut f : F )
563551 where
564552 R : AsMut < Resolver < ' a , ' tcx > > ,
565553 F : FnMut ( & mut R , Ident , Namespace , NameBinding < ' a > ) ,
@@ -572,7 +560,7 @@ impl<'a> ModuleData<'a> {
572560 }
573561
574562 /// This modifies `self` in place. The traits will be stored in `self.traits`.
575- fn ensure_traits < ' tcx , R > ( & ' a self , resolver : & mut R )
563+ fn ensure_traits < ' tcx , R > ( self , resolver : & mut R )
576564 where
577565 R : AsMut < Resolver < ' a , ' tcx > > ,
578566 {
@@ -591,35 +579,35 @@ impl<'a> ModuleData<'a> {
591579 }
592580 }
593581
594- fn res ( & self ) -> Option < Res > {
582+ fn res ( self ) -> Option < Res > {
595583 match self . kind {
596584 ModuleKind :: Def ( kind, def_id, _) => Some ( Res :: Def ( kind, def_id) ) ,
597585 _ => None ,
598586 }
599587 }
600588
601589 // Public for rustdoc.
602- fn def_id ( & self ) -> DefId {
590+ fn def_id ( self ) -> DefId {
603591 self . opt_def_id ( ) . expect ( "`ModuleData::def_id` is called on a block module" )
604592 }
605593
606- fn opt_def_id ( & self ) -> Option < DefId > {
594+ fn opt_def_id ( self ) -> Option < DefId > {
607595 match self . kind {
608596 ModuleKind :: Def ( _, def_id, _) => Some ( def_id) ,
609597 _ => None ,
610598 }
611599 }
612600
613601 // `self` resolves to the first module ancestor that `is_normal`.
614- fn is_normal ( & self ) -> bool {
602+ fn is_normal ( self ) -> bool {
615603 matches ! ( self . kind, ModuleKind :: Def ( DefKind :: Mod , _, _) )
616604 }
617605
618- fn is_trait ( & self ) -> bool {
606+ fn is_trait ( self ) -> bool {
619607 matches ! ( self . kind, ModuleKind :: Def ( DefKind :: Trait , _, _) )
620608 }
621609
622- fn nearest_item_scope ( & ' a self ) -> Module < ' a > {
610+ fn nearest_item_scope ( self ) -> Module < ' a > {
623611 match self . kind {
624612 ModuleKind :: Def ( DefKind :: Enum | DefKind :: Trait , ..) => {
625613 self . parent . expect ( "enum or trait module without a parent" )
@@ -630,15 +618,15 @@ impl<'a> ModuleData<'a> {
630618
631619 /// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
632620 /// This may be the crate root.
633- fn nearest_parent_mod ( & self ) -> DefId {
621+ fn nearest_parent_mod ( self ) -> DefId {
634622 match self . kind {
635623 ModuleKind :: Def ( DefKind :: Mod , def_id, _) => def_id,
636624 _ => self . parent . expect ( "non-root module without parent" ) . nearest_parent_mod ( ) ,
637625 }
638626 }
639627
640- fn is_ancestor_of ( & self , mut other : & Self ) -> bool {
641- while !ptr :: eq ( self , other) {
628+ fn is_ancestor_of ( self , mut other : Self ) -> bool {
629+ while self != other {
642630 if let Some ( parent) = other. parent {
643631 other = parent;
644632 } else {
@@ -649,7 +637,15 @@ impl<'a> ModuleData<'a> {
649637 }
650638}
651639
652- impl < ' a > fmt:: Debug for ModuleData < ' a > {
640+ impl < ' a > std:: ops:: Deref for Module < ' a > {
641+ type Target = ModuleData < ' a > ;
642+
643+ fn deref ( & self ) -> & Self :: Target {
644+ & self . 0
645+ }
646+ }
647+
648+ impl < ' a > fmt:: Debug for Module < ' a > {
653649 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
654650 write ! ( f, "{:?}" , self . res( ) )
655651 }
@@ -810,10 +806,9 @@ impl<'a> NameBindingData<'a> {
810806 NameBindingKind :: Import { import, .. } => {
811807 matches ! ( import. kind, ImportKind :: ExternCrate { .. } )
812808 }
813- NameBindingKind :: Module ( & ModuleData {
814- kind : ModuleKind :: Def ( DefKind :: Mod , def_id, _) ,
815- ..
816- } ) => def_id. is_crate_root ( ) ,
809+ NameBindingKind :: Module ( module)
810+ if let ModuleKind :: Def ( DefKind :: Mod , def_id, _) = module. kind
811+ => def_id. is_crate_root ( ) ,
817812 _ => false ,
818813 }
819814 }
@@ -1102,8 +1097,13 @@ impl<'a> ResolverArenas<'a> {
11021097 no_implicit_prelude : bool ,
11031098 module_map : & mut FxHashMap < DefId , Module < ' a > > ,
11041099 ) -> Module < ' a > {
1105- let module =
1106- self . modules . alloc ( ModuleData :: new ( parent, kind, expn_id, span, no_implicit_prelude) ) ;
1100+ let module = Module ( Interned :: new_unchecked ( self . modules . alloc ( ModuleData :: new (
1101+ parent,
1102+ kind,
1103+ expn_id,
1104+ span,
1105+ no_implicit_prelude,
1106+ ) ) ) ) ;
11071107 let def_id = module. opt_def_id ( ) ;
11081108 if def_id. map_or ( true , |def_id| def_id. is_local ( ) ) {
11091109 self . local_modules . borrow_mut ( ) . push ( module) ;
@@ -1647,7 +1647,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16471647 module. populate_on_access . set ( false ) ;
16481648 self . build_reduced_graph_external ( module) ;
16491649 }
1650- & module. lazy_resolutions
1650+ & module. 0 . 0 . lazy_resolutions
16511651 }
16521652
16531653 fn resolution (
@@ -1827,7 +1827,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18271827
18281828 fn set_binding_parent_module ( & mut self , binding : NameBinding < ' a > , module : Module < ' a > ) {
18291829 if let Some ( old_module) = self . binding_parent_modules . insert ( binding, module) {
1830- if !ptr :: eq ( module, old_module) {
1830+ if module != old_module {
18311831 span_bug ! ( binding. span, "parent module is reset for binding" ) ;
18321832 }
18331833 }
@@ -1847,7 +1847,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18471847 ) {
18481848 ( Some ( macro_rules) , Some ( modularized) ) => {
18491849 macro_rules. nearest_parent_mod ( ) == modularized. nearest_parent_mod ( )
1850- && modularized. is_ancestor_of ( macro_rules)
1850+ && modularized. is_ancestor_of ( * macro_rules)
18511851 }
18521852 _ => false ,
18531853 }
0 commit comments