@@ -23,9 +23,9 @@ use rustc_hir::def::{self, *};
2323use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LocalDefId } ;
2424use rustc_index:: bit_set:: DenseBitSet ;
2525use rustc_metadata:: creader:: LoadedMacro ;
26- use rustc_middle:: bug;
2726use rustc_middle:: metadata:: ModChild ;
2827use rustc_middle:: ty:: { Feed , Visibility } ;
28+ use rustc_middle:: { bug, span_bug} ;
2929use rustc_span:: hygiene:: { ExpnId , LocalExpnId , MacroKind } ;
3030use rustc_span:: { Ident , Span , Symbol , kw, sym} ;
3131use thin_vec:: ThinVec ;
@@ -46,30 +46,59 @@ type Res = def::Res<NodeId>;
4646impl < ' ra , ' tcx > Resolver < ' ra , ' tcx > {
4747 /// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
4848 /// otherwise, reports an error.
49- pub ( crate ) fn define_binding (
49+ pub ( crate ) fn define_binding_local (
5050 & mut self ,
5151 parent : Module < ' ra > ,
5252 ident : Ident ,
5353 ns : Namespace ,
5454 binding : NameBinding < ' ra > ,
5555 ) {
56- if let Err ( old_binding) = self . try_define ( parent, ident, ns, binding, false ) {
56+ if let Err ( old_binding) = self . try_define_local ( parent, ident, ns, binding, false ) {
5757 self . report_conflict ( parent, ident, ns, old_binding, binding) ;
5858 }
5959 }
6060
61- fn define (
61+ fn define_local (
6262 & mut self ,
6363 parent : Module < ' ra > ,
6464 ident : Ident ,
6565 ns : Namespace ,
6666 res : Res ,
67- vis : Visibility < impl Into < DefId > > ,
67+ vis : Visibility ,
6868 span : Span ,
6969 expn_id : LocalExpnId ,
7070 ) {
7171 let binding = self . arenas . new_res_binding ( res, vis. to_def_id ( ) , span, expn_id) ;
72- self . define_binding ( parent, ident, ns, binding)
72+ self . define_binding_local ( parent, ident, ns, binding) ;
73+ }
74+
75+ fn define_extern (
76+ & self ,
77+ parent : Module < ' ra > ,
78+ ident : Ident ,
79+ ns : Namespace ,
80+ res : Res ,
81+ vis : Visibility < DefId > ,
82+ span : Span ,
83+ expn_id : LocalExpnId ,
84+ ) {
85+ let binding = self . arenas . new_res_binding ( res, vis, span, expn_id) ;
86+ // Even if underscore names cannot be looked up, we still need to add them to modules,
87+ // because they can be fetched by glob imports from those modules, and bring traits
88+ // into scope both directly and through glob imports.
89+ let key = BindingKey :: new_disambiguated ( ident, ns, || {
90+ parent. underscore_disambiguator . update ( |d| d + 1 ) ;
91+ parent. underscore_disambiguator . get ( )
92+ } ) ;
93+ if self
94+ . resolution_or_default ( parent, key)
95+ . borrow_mut ( )
96+ . non_glob_binding
97+ . replace ( binding)
98+ . is_some ( )
99+ {
100+ span_bug ! ( span, "an external binding was already defined" ) ;
101+ }
73102 }
74103
75104 /// Walks up the tree of definitions starting at `def_id`,
@@ -192,7 +221,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
192221 visitor. parent_scope . macro_rules
193222 }
194223
195- pub ( crate ) fn build_reduced_graph_external ( & mut self , module : Module < ' ra > ) {
224+ pub ( crate ) fn build_reduced_graph_external ( & self , module : Module < ' ra > ) {
196225 for child in self . tcx . module_children ( module. def_id ( ) ) {
197226 let parent_scope = ParentScope :: module ( module, self ) ;
198227 self . build_reduced_graph_for_external_crate_res ( child, parent_scope)
@@ -201,7 +230,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
201230
202231 /// Builds the reduced graph for a single item in an external crate.
203232 fn build_reduced_graph_for_external_crate_res (
204- & mut self ,
233+ & self ,
205234 child : & ModChild ,
206235 parent_scope : ParentScope < ' ra > ,
207236 ) {
@@ -232,7 +261,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
232261 _,
233262 )
234263 | Res :: PrimTy ( ..)
235- | Res :: ToolMod => self . define ( parent, ident, TypeNS , res, vis, span, expansion) ,
264+ | Res :: ToolMod => self . define_extern ( parent, ident, TypeNS , res, vis, span, expansion) ,
236265 Res :: Def (
237266 DefKind :: Fn
238267 | DefKind :: AssocFn
@@ -241,9 +270,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
241270 | DefKind :: AssocConst
242271 | DefKind :: Ctor ( ..) ,
243272 _,
244- ) => self . define ( parent, ident, ValueNS , res, vis, span, expansion) ,
273+ ) => self . define_extern ( parent, ident, ValueNS , res, vis, span, expansion) ,
245274 Res :: Def ( DefKind :: Macro ( ..) , _) | Res :: NonMacroAttr ( ..) => {
246- self . define ( parent, ident, MacroNS , res, vis, span, expansion)
275+ self . define_extern ( parent, ident, MacroNS , res, vis, span, expansion)
247276 }
248277 Res :: Def (
249278 DefKind :: TyParam
@@ -711,7 +740,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
711740 let expansion = parent_scope. expansion ;
712741
713742 // Define a name in the type namespace if it is not anonymous.
714- self . r . define ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
743+ self . r . define_local ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
715744 self . r . feed_visibility ( feed, adt_vis) ;
716745 let def_id = feed. key ( ) ;
717746
@@ -763,7 +792,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
763792 }
764793
765794 ItemKind :: Mod ( _, ident, ref mod_kind) => {
766- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
795+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
767796
768797 if let ast:: ModKind :: Loaded ( _, _, _, Err ( _) ) = mod_kind {
769798 self . r . mods_with_parse_errors . insert ( def_id) ;
@@ -782,10 +811,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
782811 ItemKind :: Const ( box ConstItem { ident, .. } )
783812 | ItemKind :: Delegation ( box Delegation { ident, .. } )
784813 | ItemKind :: Static ( box StaticItem { ident, .. } ) => {
785- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
814+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
786815 }
787816 ItemKind :: Fn ( box Fn { ident, .. } ) => {
788- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
817+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
789818
790819 // Functions introducing procedural macros reserve a slot
791820 // in the macro namespace as well (see #52225).
@@ -794,11 +823,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
794823
795824 // These items live in the type namespace.
796825 ItemKind :: TyAlias ( box TyAlias { ident, .. } ) | ItemKind :: TraitAlias ( ident, ..) => {
797- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
826+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
798827 }
799828
800829 ItemKind :: Enum ( ident, _, _) | ItemKind :: Trait ( box ast:: Trait { ident, .. } ) => {
801- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
830+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
802831
803832 self . parent_scope . module = self . r . new_local_module (
804833 Some ( parent) ,
@@ -850,7 +879,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
850879 let feed = self . r . feed ( ctor_node_id) ;
851880 let ctor_def_id = feed. key ( ) ;
852881 let ctor_res = self . res ( ctor_def_id) ;
853- self . r . define ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
882+ self . r . define_local ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
854883 self . r . feed_visibility ( feed, ctor_vis) ;
855884 // We need the field visibility spans also for the constructor for E0603.
856885 self . insert_field_visibilities_local ( ctor_def_id. to_def_id ( ) , vdata. fields ( ) ) ;
@@ -974,7 +1003,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9741003 ) ;
9751004 }
9761005 }
977- self . r . define_binding ( parent, ident, TypeNS , imported_binding) ;
1006+ self . r . define_binding_local ( parent, ident, TypeNS , imported_binding) ;
9781007 }
9791008
9801009 /// Constructs the reduced graph for one foreign item.
@@ -991,7 +1020,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9911020 let parent = self . parent_scope . module ;
9921021 let expansion = self . parent_scope . expansion ;
9931022 let vis = self . resolve_visibility ( & item. vis ) ;
994- self . r . define ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1023+ self . r . define_local ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
9951024 self . r . feed_visibility ( feed, vis) ;
9961025 }
9971026
@@ -1074,7 +1103,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10741103 if let Some ( span) = import_all {
10751104 let import = macro_use_import ( self , span, false ) ;
10761105 self . r . potentially_unused_imports . push ( import) ;
1077- module. for_each_child ( self , |this, ident, ns, binding| {
1106+ module. for_each_child_mut ( self , |this, ident, ns, binding| {
10781107 if ns == MacroNS {
10791108 let import = if this. r . is_accessible_from ( binding. vis , this. parent_scope . module )
10801109 {
@@ -1239,7 +1268,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12391268 } ) ;
12401269 self . r . import_use_map . insert ( import, Used :: Other ) ;
12411270 let import_binding = self . r . import ( binding, import) ;
1242- self . r . define_binding ( self . r . graph_root , ident, MacroNS , import_binding) ;
1271+ self . r . define_binding_local ( self . r . graph_root , ident, MacroNS , import_binding) ;
12431272 } else {
12441273 self . r . check_reserved_macro_name ( ident, res) ;
12451274 self . insert_unused_macro ( ident, def_id, item. id ) ;
@@ -1267,7 +1296,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12671296 if !vis. is_public ( ) {
12681297 self . insert_unused_macro ( ident, def_id, item. id ) ;
12691298 }
1270- self . r . define ( module, ident, MacroNS , res, vis, span, expansion) ;
1299+ self . r . define_local ( module, ident, MacroNS , res, vis, span, expansion) ;
12711300 self . r . feed_visibility ( feed, vis) ;
12721301 self . parent_scope . macro_rules
12731302 }
@@ -1403,7 +1432,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14031432 if ctxt == AssocCtxt :: Trait {
14041433 let parent = self . parent_scope . module ;
14051434 let expansion = self . parent_scope . expansion ;
1406- self . r . define ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1435+ self . r . define_local ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
14071436 } else if !matches ! ( & item. kind, AssocItemKind :: Delegation ( deleg) if deleg. from_glob)
14081437 && ident. name != kw:: Underscore
14091438 {
@@ -1491,7 +1520,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14911520 let feed = self . r . feed ( variant. id ) ;
14921521 let def_id = feed. key ( ) ;
14931522 let vis = self . resolve_visibility ( & variant. vis ) ;
1494- self . r . define ( parent, ident, TypeNS , self . res ( def_id) , vis, variant. span , expn_id) ;
1523+ self . r . define_local ( parent, ident, TypeNS , self . res ( def_id) , vis, variant. span , expn_id) ;
14951524 self . r . feed_visibility ( feed, vis) ;
14961525
14971526 // If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1507,7 +1536,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15071536 let feed = self . r . feed ( ctor_node_id) ;
15081537 let ctor_def_id = feed. key ( ) ;
15091538 let ctor_res = self . res ( ctor_def_id) ;
1510- self . r . define ( parent, ident, ValueNS , ctor_res, ctor_vis, variant. span , expn_id) ;
1539+ self . r . define_local ( parent, ident, ValueNS , ctor_res, ctor_vis, variant. span , expn_id) ;
15111540 self . r . feed_visibility ( feed, ctor_vis) ;
15121541 }
15131542
0 commit comments