@@ -42,19 +42,19 @@ type Res = def::Res<NodeId>;
42
42
impl < ' ra , ' tcx > Resolver < ' ra , ' tcx > {
43
43
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
44
44
/// otherwise, reports an error.
45
- pub ( crate ) fn define_binding (
45
+ pub ( crate ) fn define_binding_local (
46
46
& mut self ,
47
47
parent : Module < ' ra > ,
48
48
ident : Ident ,
49
49
ns : Namespace ,
50
50
binding : NameBinding < ' ra > ,
51
51
) {
52
- if let Err ( old_binding) = self . try_define ( parent, ident , ns , binding, false ) {
52
+ if let Err ( old_binding) = self . try_define_local ( parent, key , binding, false ) {
53
53
self . report_conflict ( parent, ident, ns, old_binding, binding) ;
54
54
}
55
55
}
56
56
57
- fn define (
57
+ fn define_local (
58
58
& mut self ,
59
59
parent : Module < ' ra > ,
60
60
ident : Ident ,
@@ -65,7 +65,29 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
65
65
expn_id : LocalExpnId ,
66
66
) {
67
67
let binding = self . arenas . new_res_binding ( res, vis. to_def_id ( ) , span, expn_id) ;
68
- self . define_binding ( parent, ident, ns, binding)
68
+ self . define_binding_local ( parent, ident, ns, binding)
69
+ }
70
+
71
+ // Panics when a binding already exists.
72
+ fn define_extern (
73
+ & self ,
74
+ parent : Module < ' ra > ,
75
+ ident : Ident ,
76
+ ns : Namespace ,
77
+ res : Res ,
78
+ vis : Visibility < impl Into < DefId > > ,
79
+ span : Span ,
80
+ expn_id : LocalExpnId ,
81
+ ) {
82
+ let binding = self . arenas . new_res_binding ( res, vis. to_def_id ( ) , span, expn_id) ;
83
+ let key = self . new_disambiguated_key ( ident, ns) ;
84
+ self . check_reserved_macro_name ( key. ident , binding. res ( ) ) ;
85
+ let resolution = & mut * self . resolution ( parent, key) . borrow_mut ( ) ;
86
+ if resolution. binding . is_some ( ) {
87
+ panic ! ( "An external binding was already defined" ) ;
88
+ }
89
+ // FIXME: maybe some handling of glob-importers
90
+ resolution. binding = Some ( binding) ;
69
91
}
70
92
71
93
/// Walks up the tree of definitions starting at `def_id`,
@@ -188,7 +210,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
188
210
visitor. parent_scope . macro_rules
189
211
}
190
212
191
- pub ( crate ) fn build_reduced_graph_external ( & mut self , module : Module < ' ra > ) {
213
+ pub ( crate ) fn build_reduced_graph_external ( & self , module : Module < ' ra > ) {
192
214
for child in self . tcx . module_children ( module. def_id ( ) ) {
193
215
let parent_scope = ParentScope :: module ( module, self ) ;
194
216
self . build_reduced_graph_for_external_crate_res ( child, parent_scope)
@@ -197,7 +219,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
197
219
198
220
/// Builds the reduced graph for a single item in an external crate.
199
221
fn build_reduced_graph_for_external_crate_res (
200
- & mut self ,
222
+ & self ,
201
223
child : & ModChild ,
202
224
parent_scope : ParentScope < ' ra > ,
203
225
) {
@@ -228,7 +250,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
228
250
_,
229
251
)
230
252
| Res :: PrimTy ( ..)
231
- | Res :: ToolMod => self . define ( parent, ident, TypeNS , res, vis, span, expansion) ,
253
+ | Res :: ToolMod => self . define_extern ( parent, ident, TypeNS , res, vis, span, expansion) ,
232
254
Res :: Def (
233
255
DefKind :: Fn
234
256
| DefKind :: AssocFn
@@ -237,9 +259,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
237
259
| DefKind :: AssocConst
238
260
| DefKind :: Ctor ( ..) ,
239
261
_,
240
- ) => self . define ( parent, ident, ValueNS , res, vis, span, expansion) ,
262
+ ) => self . define_extern ( parent, ident, ValueNS , res, vis, span, expansion) ,
241
263
Res :: Def ( DefKind :: Macro ( ..) , _) | Res :: NonMacroAttr ( ..) => {
242
- self . define ( parent, ident, MacroNS , res, vis, span, expansion)
264
+ self . define_extern ( parent, ident, MacroNS , res, vis, span, expansion)
243
265
}
244
266
Res :: Def (
245
267
DefKind :: TyParam
@@ -705,7 +727,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
705
727
let expansion = parent_scope. expansion ;
706
728
707
729
// Define a name in the type namespace if it is not anonymous.
708
- self . r . define ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
730
+ self . r . define_local ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
709
731
self . r . feed_visibility ( feed, adt_vis) ;
710
732
let def_id = feed. key ( ) ;
711
733
@@ -757,7 +779,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
757
779
}
758
780
759
781
ItemKind :: Mod ( _, ident, ref mod_kind) => {
760
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
782
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
761
783
762
784
if let ast:: ModKind :: Loaded ( _, _, _, Err ( _) ) = mod_kind {
763
785
self . r . mods_with_parse_errors . insert ( def_id) ;
@@ -776,10 +798,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
776
798
ItemKind :: Const ( box ConstItem { ident, .. } )
777
799
| ItemKind :: Delegation ( box Delegation { ident, .. } )
778
800
| ItemKind :: Static ( box StaticItem { ident, .. } ) => {
779
- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
801
+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
780
802
}
781
803
ItemKind :: Fn ( box Fn { ident, .. } ) => {
782
- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
804
+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
783
805
784
806
// Functions introducing procedural macros reserve a slot
785
807
// in the macro namespace as well (see #52225).
@@ -788,11 +810,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
788
810
789
811
// These items live in the type namespace.
790
812
ItemKind :: TyAlias ( box TyAlias { ident, .. } ) | ItemKind :: TraitAlias ( ident, ..) => {
791
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
813
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
792
814
}
793
815
794
816
ItemKind :: Enum ( ident, _, _) | ItemKind :: Trait ( box ast:: Trait { ident, .. } ) => {
795
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
817
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
796
818
797
819
self . parent_scope . module = self . r . new_local_module (
798
820
Some ( parent) ,
@@ -844,7 +866,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
844
866
let feed = self . r . feed ( ctor_node_id) ;
845
867
let ctor_def_id = feed. key ( ) ;
846
868
let ctor_res = self . res ( ctor_def_id) ;
847
- self . r . define ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
869
+ self . r . define_local ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
848
870
self . r . feed_visibility ( feed, ctor_vis) ;
849
871
// We need the field visibility spans also for the constructor for E0603.
850
872
self . insert_field_visibilities_local ( ctor_def_id. to_def_id ( ) , vdata. fields ( ) ) ;
@@ -965,7 +987,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
965
987
) ;
966
988
}
967
989
}
968
- self . r . define_binding ( parent, ident, TypeNS , imported_binding) ;
990
+ self . r . define_binding_local ( parent, ident, TypeNS , imported_binding) ;
969
991
}
970
992
971
993
/// Constructs the reduced graph for one foreign item.
@@ -982,7 +1004,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
982
1004
let parent = self . parent_scope . module ;
983
1005
let expansion = self . parent_scope . expansion ;
984
1006
let vis = self . resolve_visibility ( & item. vis ) ;
985
- self . r . define ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1007
+ self . r . define_local ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
986
1008
self . r . feed_visibility ( feed, vis) ;
987
1009
}
988
1010
@@ -1241,7 +1263,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1241
1263
} ) ;
1242
1264
self . r . import_use_map . insert ( import, Used :: Other ) ;
1243
1265
let import_binding = self . r . import ( binding, import) ;
1244
- self . r . define_binding ( self . r . graph_root , ident, MacroNS , import_binding) ;
1266
+ self . r . define_binding_local ( self . r . graph_root , ident, MacroNS , import_binding) ;
1245
1267
} else {
1246
1268
self . r . check_reserved_macro_name ( ident, res) ;
1247
1269
self . insert_unused_macro ( ident, def_id, item. id ) ;
@@ -1269,7 +1291,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1269
1291
if !vis. is_public ( ) {
1270
1292
self . insert_unused_macro ( ident, def_id, item. id ) ;
1271
1293
}
1272
- self . r . define ( module, ident, MacroNS , res, vis, span, expansion) ;
1294
+ self . r . define_local ( module, ident, MacroNS , res, vis, span, expansion) ;
1273
1295
self . r . feed_visibility ( feed, vis) ;
1274
1296
self . parent_scope . macro_rules
1275
1297
}
@@ -1405,7 +1427,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1405
1427
if ctxt == AssocCtxt :: Trait {
1406
1428
let parent = self . parent_scope . module ;
1407
1429
let expansion = self . parent_scope . expansion ;
1408
- self . r . define ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1430
+ self . r . define_local ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1409
1431
} else if !matches ! ( & item. kind, AssocItemKind :: Delegation ( deleg) if deleg. from_glob)
1410
1432
&& ident. name != kw:: Underscore
1411
1433
{
@@ -1493,7 +1515,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1493
1515
let feed = self . r . feed ( variant. id ) ;
1494
1516
let def_id = feed. key ( ) ;
1495
1517
let vis = self . resolve_visibility ( & variant. vis ) ;
1496
- self . r . define ( parent, ident, TypeNS , self . res ( def_id) , vis, variant. span , expn_id) ;
1518
+ self . r . define_local ( parent, ident, TypeNS , self . res ( def_id) , vis, variant. span , expn_id) ;
1497
1519
self . r . feed_visibility ( feed, vis) ;
1498
1520
1499
1521
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1509,7 +1531,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1509
1531
let feed = self . r . feed ( ctor_node_id) ;
1510
1532
let ctor_def_id = feed. key ( ) ;
1511
1533
let ctor_res = self . res ( ctor_def_id) ;
1512
- self . r . define ( parent, ident, ValueNS , ctor_res, ctor_vis, variant. span , expn_id) ;
1534
+ self . r . define_local ( parent, ident, ValueNS , ctor_res, ctor_vis, variant. span , expn_id) ;
1513
1535
self . r . feed_visibility ( feed, ctor_vis) ;
1514
1536
}
1515
1537
0 commit comments