Skip to content

Commit aa0c369

Browse files
account for (non)_glob_binding change
1 parent e941e0c commit aa0c369

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4949
ns: Namespace,
5050
binding: NameBinding<'ra>,
5151
) {
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) {
5354
self.report_conflict(parent, ident, ns, old_binding, binding);
5455
}
5556
}
@@ -60,17 +61,43 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
6061
ident: Ident,
6162
ns: Namespace,
6263
res: Res,
63-
vis: Visibility,
64+
vis: Visibility, // Implictly local
6465
span: Span,
6566
expn_id: LocalExpnId,
6667
) {
6768
assert!(parent.is_local());
6869
assert!(res.opt_def_id().is_none_or(|def_id| def_id.is_local()));
6970
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+
}
7199
}
72100

73-
// Panics when a binding already exists.
74101
fn define_extern(
75102
&self,
76103
parent: Module<'ra>,
@@ -88,12 +115,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
88115
def_id
89116
});
90117
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);
97119
}
98120

99121
/// Walks up the tree of definitions starting at `def_id`,

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ impl<'ra> Module<'ra> {
670670
F: FnMut(&R, Ident, Namespace, NameBinding<'ra>),
671671
{
672672
for (key, name_resolution) in resolver.as_ref().resolutions(self).borrow().iter() {
673-
if let Some(binding) = name_resolution.borrow().binding {
673+
if let Some(binding) = name_resolution.borrow().best_binding() {
674674
f(resolver, key.ident, key.ns, binding);
675675
}
676676
}

0 commit comments

Comments
 (0)