@@ -49,7 +49,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
49
49
ns : Namespace ,
50
50
binding : NameBinding < ' ra > ,
51
51
) {
52
- if let Err ( old_binding) = self . try_define_local ( parent, key, binding, false ) {
52
+ assert ! ( parent. is_local( ) ) ;
53
+ if let Err ( old_binding) = self . try_define_local ( parent, ident, ns, binding, false ) {
53
54
self . report_conflict ( parent, ident, ns, old_binding, binding) ;
54
55
}
55
56
}
@@ -60,17 +61,43 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
60
61
ident : Ident ,
61
62
ns : Namespace ,
62
63
res : Res ,
63
- vis : Visibility ,
64
+ vis : Visibility , // Implictly local
64
65
span : Span ,
65
66
expn_id : LocalExpnId ,
66
67
) {
67
68
assert ! ( parent. is_local( ) ) ;
68
69
assert ! ( res. opt_def_id( ) . is_none_or( |def_id| def_id. is_local( ) ) ) ;
69
70
let binding = self . arenas . new_res_binding ( res, vis. to_def_id ( ) , span, expn_id) ;
70
- self . define_binding_local ( parent, ident, ns, binding)
71
+ self . define_binding_local ( parent, ident, ns, binding) ;
72
+ }
73
+
74
+ /// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
75
+ /// otherwise, panic.
76
+ pub ( crate ) fn define_binding_extern (
77
+ & self ,
78
+ parent : Module < ' ra > ,
79
+ ident : Ident ,
80
+ ns : Namespace ,
81
+ binding : NameBinding < ' ra > ,
82
+ ) {
83
+ assert ! ( !parent. is_local( ) ) ;
84
+ // Even if underscore names cannot be looked up, we still need to add them to modules,
85
+ // because they can be fetched by glob imports from those modules, and bring traits
86
+ // into scope both directly and through glob imports.
87
+ let key = BindingKey :: new_disambiguated ( ident, ns, || {
88
+ ( parent. 0 . 0 . lazy_resolutions . borrow ( ) . len ( ) + 1 ) . try_into ( ) . unwrap ( )
89
+ } ) ;
90
+ let resolution = & mut * self . resolution ( parent, key) . borrow_mut ( ) ;
91
+ let resolution_binding = if binding. is_glob_import ( ) {
92
+ & mut resolution. glob_binding
93
+ } else {
94
+ & mut resolution. non_glob_binding
95
+ } ;
96
+ if resolution_binding. replace ( binding) . is_some ( ) {
97
+ panic ! ( "An external binding was already defined" ) ;
98
+ }
71
99
}
72
100
73
- // Panics when a binding already exists.
74
101
fn define_extern (
75
102
& self ,
76
103
parent : Module < ' ra > ,
@@ -88,12 +115,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
88
115
def_id
89
116
} ) ;
90
117
let binding = self . arenas . new_res_binding ( res, vis, span, expn_id) ;
91
- let key = self . new_disambiguated_key ( ident, ns) ;
92
- let resolution = & mut * self . resolution ( parent, key) . borrow_mut ( ) ;
93
- if resolution. binding . is_some ( ) {
94
- panic ! ( "An external binding was already defined" ) ;
95
- }
96
- resolution. binding = Some ( binding) ;
118
+ self . define_binding_extern ( parent, ident, ns, binding) ;
97
119
}
98
120
99
121
/// Walks up the tree of definitions starting at `def_id`,
0 commit comments