@@ -42,20 +42,20 @@ 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
52
let key = self . new_disambiguated_key ( ident, ns) ;
53
- if let Err ( old_binding) = self . try_define ( parent, key, binding, false ) {
53
+ if let Err ( old_binding) = self . try_define_local ( parent, key, binding, false ) {
54
54
self . report_conflict ( parent, ident, ns, old_binding, binding) ;
55
55
}
56
56
}
57
57
58
- fn define (
58
+ fn define_local (
59
59
& mut self ,
60
60
parent : Module < ' ra > ,
61
61
ident : Ident ,
@@ -66,7 +66,29 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
66
66
expn_id : LocalExpnId ,
67
67
) {
68
68
let binding = self . arenas . new_res_binding ( res, vis. to_def_id ( ) , span, expn_id) ;
69
- self . define_binding ( parent, ident, ns, binding)
69
+ self . define_binding_local ( parent, ident, ns, binding)
70
+ }
71
+
72
+ // Panics when a binding already exists.
73
+ fn define_extern (
74
+ & self ,
75
+ parent : Module < ' ra > ,
76
+ ident : Ident ,
77
+ ns : Namespace ,
78
+ res : Res ,
79
+ vis : Visibility < impl Into < DefId > > ,
80
+ span : Span ,
81
+ expn_id : LocalExpnId ,
82
+ ) {
83
+ let binding = self . arenas . new_res_binding ( res, vis. to_def_id ( ) , span, expn_id) ;
84
+ let key = self . new_disambiguated_key ( ident, ns) ;
85
+ self . check_reserved_macro_name ( key. ident , binding. res ( ) ) ;
86
+ let resolution = & mut * self . resolution ( parent, key) . borrow_mut ( ) ;
87
+ if resolution. binding . is_some ( ) {
88
+ panic ! ( "An external binding was already defined" ) ;
89
+ }
90
+ // FIXME: maybe some handling of glob-importers
91
+ resolution. binding = Some ( binding) ;
70
92
}
71
93
72
94
/// Walks up the tree of definitions starting at `def_id`,
@@ -189,7 +211,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
189
211
visitor. parent_scope . macro_rules
190
212
}
191
213
192
- pub ( crate ) fn build_reduced_graph_external ( & mut self , module : Module < ' ra > ) {
214
+ pub ( crate ) fn build_reduced_graph_external ( & self , module : Module < ' ra > ) {
193
215
for child in self . tcx . module_children ( module. def_id ( ) ) {
194
216
let parent_scope = ParentScope :: module ( module, self ) ;
195
217
self . build_reduced_graph_for_external_crate_res ( child, parent_scope)
@@ -198,7 +220,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
198
220
199
221
/// Builds the reduced graph for a single item in an external crate.
200
222
fn build_reduced_graph_for_external_crate_res (
201
- & mut self ,
223
+ & self ,
202
224
child : & ModChild ,
203
225
parent_scope : ParentScope < ' ra > ,
204
226
) {
@@ -229,7 +251,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
229
251
_,
230
252
)
231
253
| Res :: PrimTy ( ..)
232
- | Res :: ToolMod => self . define ( parent, ident, TypeNS , res, vis, span, expansion) ,
254
+ | Res :: ToolMod => self . define_extern ( parent, ident, TypeNS , res, vis, span, expansion) ,
233
255
Res :: Def (
234
256
DefKind :: Fn
235
257
| DefKind :: AssocFn
@@ -238,9 +260,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
238
260
| DefKind :: AssocConst
239
261
| DefKind :: Ctor ( ..) ,
240
262
_,
241
- ) => self . define ( parent, ident, ValueNS , res, vis, span, expansion) ,
263
+ ) => self . define_extern ( parent, ident, ValueNS , res, vis, span, expansion) ,
242
264
Res :: Def ( DefKind :: Macro ( ..) , _) | Res :: NonMacroAttr ( ..) => {
243
- self . define ( parent, ident, MacroNS , res, vis, span, expansion)
265
+ self . define_extern ( parent, ident, MacroNS , res, vis, span, expansion)
244
266
}
245
267
Res :: Def (
246
268
DefKind :: TyParam
@@ -713,7 +735,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
713
735
let expansion = parent_scope. expansion ;
714
736
715
737
// Define a name in the type namespace if it is not anonymous.
716
- self . r . define ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
738
+ self . r . define_local ( parent, ident, TypeNS , adt_res, adt_vis, adt_span, expansion) ;
717
739
self . r . feed_visibility ( feed, adt_vis) ;
718
740
let def_id = feed. key ( ) ;
719
741
@@ -765,7 +787,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
765
787
}
766
788
767
789
ItemKind :: Mod ( _, ident, ref mod_kind) => {
768
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
790
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
769
791
770
792
if let ast:: ModKind :: Loaded ( _, _, _, Err ( _) ) = mod_kind {
771
793
self . r . mods_with_parse_errors . insert ( def_id) ;
@@ -784,10 +806,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
784
806
ItemKind :: Const ( box ConstItem { ident, .. } )
785
807
| ItemKind :: Delegation ( box Delegation { ident, .. } )
786
808
| ItemKind :: Static ( box StaticItem { ident, .. } ) => {
787
- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
809
+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
788
810
}
789
811
ItemKind :: Fn ( box Fn { ident, .. } ) => {
790
- self . r . define ( parent, ident, ValueNS , res, vis, sp, expansion) ;
812
+ self . r . define_local ( parent, ident, ValueNS , res, vis, sp, expansion) ;
791
813
792
814
// Functions introducing procedural macros reserve a slot
793
815
// in the macro namespace as well (see #52225).
@@ -796,11 +818,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
796
818
797
819
// These items live in the type namespace.
798
820
ItemKind :: TyAlias ( box TyAlias { ident, .. } ) | ItemKind :: TraitAlias ( ident, ..) => {
799
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
821
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
800
822
}
801
823
802
824
ItemKind :: Enum ( ident, _, _) | ItemKind :: Trait ( box ast:: Trait { ident, .. } ) => {
803
- self . r . define ( parent, ident, TypeNS , res, vis, sp, expansion) ;
825
+ self . r . define_local ( parent, ident, TypeNS , res, vis, sp, expansion) ;
804
826
805
827
self . parent_scope . module = self . r . new_local_module (
806
828
Some ( parent) ,
@@ -852,7 +874,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
852
874
let feed = self . r . feed ( ctor_node_id) ;
853
875
let ctor_def_id = feed. key ( ) ;
854
876
let ctor_res = self . res ( ctor_def_id) ;
855
- self . r . define ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
877
+ self . r . define_local ( parent, ident, ValueNS , ctor_res, ctor_vis, sp, expansion) ;
856
878
self . r . feed_visibility ( feed, ctor_vis) ;
857
879
// We need the field visibility spans also for the constructor for E0603.
858
880
self . insert_field_visibilities_local ( ctor_def_id. to_def_id ( ) , vdata. fields ( ) ) ;
@@ -973,7 +995,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
973
995
) ;
974
996
}
975
997
}
976
- self . r . define_binding ( parent, ident, TypeNS , imported_binding) ;
998
+ self . r . define_binding_local ( parent, ident, TypeNS , imported_binding) ;
977
999
}
978
1000
979
1001
/// Constructs the reduced graph for one foreign item.
@@ -990,7 +1012,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
990
1012
let parent = self . parent_scope . module ;
991
1013
let expansion = self . parent_scope . expansion ;
992
1014
let vis = self . resolve_visibility ( & item. vis ) ;
993
- 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) ;
994
1016
self . r . feed_visibility ( feed, vis) ;
995
1017
}
996
1018
@@ -1249,7 +1271,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1249
1271
} ) ;
1250
1272
self . r . import_use_map . insert ( import, Used :: Other ) ;
1251
1273
let import_binding = self . r . import ( binding, import) ;
1252
- self . r . define_binding ( self . r . graph_root , ident, MacroNS , import_binding) ;
1274
+ self . r . define_binding_local ( self . r . graph_root , ident, MacroNS , import_binding) ;
1253
1275
} else {
1254
1276
self . r . check_reserved_macro_name ( ident, res) ;
1255
1277
self . insert_unused_macro ( ident, def_id, item. id ) ;
@@ -1277,7 +1299,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1277
1299
if !vis. is_public ( ) {
1278
1300
self . insert_unused_macro ( ident, def_id, item. id ) ;
1279
1301
}
1280
- self . r . define ( module, ident, MacroNS , res, vis, span, expansion) ;
1302
+ self . r . define_local ( module, ident, MacroNS , res, vis, span, expansion) ;
1281
1303
self . r . feed_visibility ( feed, vis) ;
1282
1304
self . parent_scope . macro_rules
1283
1305
}
@@ -1413,7 +1435,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1413
1435
if ctxt == AssocCtxt :: Trait {
1414
1436
let parent = self . parent_scope . module ;
1415
1437
let expansion = self . parent_scope . expansion ;
1416
- self . r . define ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1438
+ self . r . define_local ( parent, ident, ns, self . res ( def_id) , vis, item. span , expansion) ;
1417
1439
} else if !matches ! ( & item. kind, AssocItemKind :: Delegation ( deleg) if deleg. from_glob) {
1418
1440
let impl_def_id = self . r . tcx . local_parent ( local_def_id) ;
1419
1441
let key = BindingKey :: new ( ident. normalize_to_macros_2_0 ( ) , ns) ;
@@ -1498,7 +1520,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1498
1520
let feed = self . r . feed ( variant. id ) ;
1499
1521
let def_id = feed. key ( ) ;
1500
1522
let vis = self . resolve_visibility ( & variant. vis ) ;
1501
- 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) ;
1502
1524
self . r . feed_visibility ( feed, vis) ;
1503
1525
1504
1526
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1514,7 +1536,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1514
1536
let feed = self . r . feed ( ctor_node_id) ;
1515
1537
let ctor_def_id = feed. key ( ) ;
1516
1538
let ctor_res = self . res ( ctor_def_id) ;
1517
- 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) ;
1518
1540
self . r . feed_visibility ( feed, ctor_vis) ;
1519
1541
}
1520
1542
0 commit comments