@@ -23,9 +23,9 @@ use rustc_hir::def::{self, *};
23
23
use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LocalDefId } ;
24
24
use rustc_index:: bit_set:: DenseBitSet ;
25
25
use rustc_metadata:: creader:: LoadedMacro ;
26
- use rustc_middle:: bug;
27
26
use rustc_middle:: metadata:: ModChild ;
28
27
use rustc_middle:: ty:: { Feed , Visibility } ;
28
+ use rustc_middle:: { bug, span_bug} ;
29
29
use rustc_span:: hygiene:: { ExpnId , LocalExpnId , MacroKind } ;
30
30
use rustc_span:: { Ident , Span , Symbol , kw, sym} ;
31
31
use thin_vec:: ThinVec ;
@@ -46,30 +46,53 @@ type Res = def::Res<NodeId>;
46
46
impl < ' ra , ' tcx > Resolver < ' ra , ' tcx > {
47
47
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
48
48
/// otherwise, reports an error.
49
- pub ( crate ) fn define_binding (
49
+ pub ( crate ) fn define_binding_local (
50
50
& mut self ,
51
51
parent : Module < ' ra > ,
52
52
ident : Ident ,
53
53
ns : Namespace ,
54
54
binding : NameBinding < ' ra > ,
55
55
) {
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 ) {
57
57
self . report_conflict ( parent, ident, ns, old_binding, binding) ;
58
58
}
59
59
}
60
60
61
- fn define (
61
+ fn define_local (
62
62
& mut self ,
63
63
parent : Module < ' ra > ,
64
64
ident : Ident ,
65
65
ns : Namespace ,
66
66
res : Res ,
67
- vis : Visibility < impl Into < DefId > > ,
67
+ vis : Visibility ,
68
68
span : Span ,
69
69
expn_id : LocalExpnId ,
70
70
) {
71
71
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 . resolution ( parent, key) . borrow_mut ( ) . non_glob_binding . replace ( binding) . is_some ( ) {
94
+ span_bug ! ( span, "an external binding was already defined" ) ;
95
+ }
73
96
}
74
97
75
98
/// Walks up the tree of definitions starting at `def_id`,
@@ -192,7 +215,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
192
215
visitor. parent_scope . macro_rules
193
216
}
194
217
195
- pub ( crate ) fn build_reduced_graph_external ( & mut self , module : Module < ' ra > ) {
218
+ pub ( crate ) fn build_reduced_graph_external ( & self , module : Module < ' ra > ) {
196
219
for child in self . tcx . module_children ( module. def_id ( ) ) {
197
220
let parent_scope = ParentScope :: module ( module, self ) ;
198
221
self . build_reduced_graph_for_external_crate_res ( child, parent_scope)
@@ -201,7 +224,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
201
224
202
225
/// Builds the reduced graph for a single item in an external crate.
203
226
fn build_reduced_graph_for_external_crate_res (
204
- & mut self ,
227
+ & self ,
205
228
child : & ModChild ,
206
229
parent_scope : ParentScope < ' ra > ,
207
230
) {
@@ -232,7 +255,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
232
255
_,
233
256
)
234
257
| Res :: PrimTy ( ..)
235
- | Res :: ToolMod => self . define ( parent, ident, TypeNS , res, vis, span, expansion) ,
258
+ | Res :: ToolMod => self . define_extern ( parent, ident, TypeNS , res, vis, span, expansion) ,
236
259
Res :: Def (
237
260
DefKind :: Fn
238
261
| DefKind :: AssocFn
@@ -241,9 +264,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
241
264
| DefKind :: AssocConst
242
265
| DefKind :: Ctor ( ..) ,
243
266
_,
244
- ) => self . define ( parent, ident, ValueNS , res, vis, span, expansion) ,
267
+ ) => self . define_extern ( parent, ident, ValueNS , res, vis, span, expansion) ,
245
268
Res :: Def ( DefKind :: Macro ( ..) , _) | Res :: NonMacroAttr ( ..) => {
246
- self . define ( parent, ident, MacroNS , res, vis, span, expansion)
269
+ self . define_extern ( parent, ident, MacroNS , res, vis, span, expansion)
247
270
}
248
271
Res :: Def (
249
272
DefKind :: TyParam
@@ -709,7 +732,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
709
732
let expansion = parent_scope. expansion ;
710
733
711
734
// Define a name in the type namespace if it is not anonymous.
712
- self . r . define ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
735
+ self . r . define_local ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
713
736
self . r . feed_visibility ( feed, adt_vis) ;
714
737
let def_id = feed. key ( ) ;
715
738
@@ -761,7 +784,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
761
784
}
762
785
763
786
ItemKind :: Mod ( _, ident, ref mod_kind) => {
764
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
787
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
765
788
766
789
if let ast:: ModKind :: Loaded ( _, _, _, Err ( _) ) = mod_kind {
767
790
self . r . mods_with_parse_errors . insert ( def_id) ;
@@ -780,10 +803,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
780
803
ItemKind :: Const ( box ConstItem { ident, .. } )
781
804
| ItemKind :: Delegation ( box Delegation { ident, .. } )
782
805
| ItemKind :: Static ( box StaticItem { ident, .. } ) => {
783
- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
806
+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
784
807
}
785
808
ItemKind :: Fn ( box Fn { ident, .. } ) => {
786
- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
809
+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
787
810
788
811
// Functions introducing procedural macros reserve a slot
789
812
// in the macro namespace as well (see #52225).
@@ -792,11 +815,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
792
815
793
816
// These items live in the type namespace.
794
817
ItemKind :: TyAlias ( box TyAlias { ident, .. } ) | ItemKind :: TraitAlias ( ident, ..) => {
795
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
818
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
796
819
}
797
820
798
821
ItemKind :: Enum ( ident, _, _) | ItemKind :: Trait ( box ast:: Trait { ident, .. } ) => {
799
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
822
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
800
823
801
824
self . parent_scope . module = self . r . new_local_module (
802
825
Some ( parent) ,
@@ -848,7 +871,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
848
871
let feed = self . r . feed ( ctor_node_id) ;
849
872
let ctor_def_id = feed. key ( ) ;
850
873
let ctor_res = self . res ( ctor_def_id) ;
851
- self . r . define ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
874
+ self . r . define_local ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
852
875
self . r . feed_visibility ( feed, ctor_vis) ;
853
876
// We need the field visibility spans also for the constructor for E0603.
854
877
self . insert_field_visibilities_local ( ctor_def_id. to_def_id ( ) , vdata. fields ( ) ) ;
@@ -972,7 +995,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
972
995
) ;
973
996
}
974
997
}
975
- self . r . define_binding ( parent, ident, TypeNS , imported_binding) ;
998
+ self . r . define_binding_local ( parent, ident, TypeNS , imported_binding) ;
976
999
}
977
1000
978
1001
/// Constructs the reduced graph for one foreign item.
@@ -989,7 +1012,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
989
1012
let parent = self . parent_scope . module ;
990
1013
let expansion = self . parent_scope . expansion ;
991
1014
let vis = self . resolve_visibility ( & item. vis ) ;
992
- self . r . define ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1015
+ self . r . define_local ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
993
1016
self . r . feed_visibility ( feed, vis) ;
994
1017
}
995
1018
@@ -1072,7 +1095,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1072
1095
if let Some ( span) = import_all {
1073
1096
let import = macro_use_import ( self , span, false ) ;
1074
1097
self . r . potentially_unused_imports . push ( import) ;
1075
- module. for_each_child ( self , |this, ident, ns, binding| {
1098
+ module. for_each_child_mut ( self , |this, ident, ns, binding| {
1076
1099
if ns == MacroNS {
1077
1100
let import = if this. r . is_accessible_from ( binding. vis , this. parent_scope . module )
1078
1101
{
@@ -1237,7 +1260,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1237
1260
} ) ;
1238
1261
self . r . import_use_map . insert ( import, Used :: Other ) ;
1239
1262
let import_binding = self . r . import ( binding, import) ;
1240
- self . r . define_binding ( self . r . graph_root , ident, MacroNS , import_binding) ;
1263
+ self . r . define_binding_local ( self . r . graph_root , ident, MacroNS , import_binding) ;
1241
1264
} else {
1242
1265
self . r . check_reserved_macro_name ( ident, res) ;
1243
1266
self . insert_unused_macro ( ident, def_id, item. id ) ;
@@ -1265,7 +1288,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1265
1288
if !vis. is_public ( ) {
1266
1289
self . insert_unused_macro ( ident, def_id, item. id ) ;
1267
1290
}
1268
- self . r . define ( module, ident, MacroNS , res, vis, span, expansion) ;
1291
+ self . r . define_local ( module, ident, MacroNS , res, vis, span, expansion) ;
1269
1292
self . r . feed_visibility ( feed, vis) ;
1270
1293
self . parent_scope . macro_rules
1271
1294
}
@@ -1401,7 +1424,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1401
1424
if ctxt == AssocCtxt :: Trait {
1402
1425
let parent = self . parent_scope . module ;
1403
1426
let expansion = self . parent_scope . expansion ;
1404
- self . r . define ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1427
+ self . r . define_local ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1405
1428
} else if !matches ! ( & item. kind, AssocItemKind :: Delegation ( deleg) if deleg. from_glob)
1406
1429
&& ident. name != kw:: Underscore
1407
1430
{
@@ -1489,7 +1512,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1489
1512
let feed = self . r . feed ( variant. id ) ;
1490
1513
let def_id = feed. key ( ) ;
1491
1514
let vis = self . resolve_visibility ( & variant. vis ) ;
1492
- self . r . define ( parent, ident, TypeNS , self . res ( def_id) , vis, variant. span , expn_id) ;
1515
+ self . r . define_local ( parent, ident, TypeNS , self . res ( def_id) , vis, variant. span , expn_id) ;
1493
1516
self . r . feed_visibility ( feed, vis) ;
1494
1517
1495
1518
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1505,7 +1528,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1505
1528
let feed = self . r . feed ( ctor_node_id) ;
1506
1529
let ctor_def_id = feed. key ( ) ;
1507
1530
let ctor_res = self . res ( ctor_def_id) ;
1508
- self . r . define ( parent, ident, ValueNS , ctor_res, ctor_vis, variant. span , expn_id) ;
1531
+ self . r . define_local ( parent, ident, ValueNS , ctor_res, ctor_vis, variant. span , expn_id) ;
1509
1532
self . r . feed_visibility ( feed, ctor_vis) ;
1510
1533
}
1511
1534
0 commit comments