Skip to content

Commit 0383344

Browse files
committed
Detect duplicate glob imports arising from glob cycles
1 parent 470ca1c commit 0383344

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/librustc_resolve/resolve_imports.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ impl<'a> ::ModuleS<'a> {
275275
// Define the name or return the existing binding if there is a collision.
276276
pub fn try_define_child(&self, name: Name, ns: Namespace, binding: NameBinding<'a>)
277277
-> Result<(), &'a NameBinding<'a>> {
278-
if self.resolutions.borrow_state() != ::std::cell::BorrowState::Unused { return Ok(()); }
279278
self.update_resolution(name, ns, |resolution| {
280279
resolution.try_define(self.arenas.alloc_name_binding(binding))
281280
})
@@ -318,15 +317,20 @@ impl<'a> ::ModuleS<'a> {
318317
fn update_resolution<T, F>(&self, name: Name, ns: Namespace, update: F) -> T
319318
where F: FnOnce(&mut NameResolution<'a>) -> T
320319
{
321-
let mut resolution = &mut *self.resolution(name, ns).borrow_mut();
322-
let was_known = resolution.binding().is_some();
320+
let (new_binding, t) = {
321+
let mut resolution = &mut *self.resolution(name, ns).borrow_mut();
322+
let was_known = resolution.binding().is_some();
323323

324-
let t = update(resolution);
325-
if !was_known {
326-
if let Some(binding) = resolution.binding() {
327-
self.define_in_glob_importers(name, ns, binding);
324+
let t = update(resolution);
325+
326+
if was_known { return t; }
327+
match resolution.binding() {
328+
Some(binding) => (binding, t),
329+
None => return t,
328330
}
329-
}
331+
};
332+
333+
self.define_in_glob_importers(name, ns, new_binding);
330334
t
331335
}
332336

@@ -646,11 +650,12 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
646650
// Add to target_module's glob_importers
647651
target_module.glob_importers.borrow_mut().push((module_, directive));
648652

649-
for (&(name, ns), resolution) in target_module.resolutions.borrow().iter() {
650-
if let Some(binding) = resolution.borrow().binding() {
651-
if binding.defined_with(DefModifiers::IMPORTABLE | DefModifiers::PUBLIC) {
652-
let _ = module_.try_define_child(name, ns, directive.import(binding, None));
653-
}
653+
let bindings = target_module.resolutions.borrow().iter().filter_map(|(name, resolution)| {
654+
resolution.borrow().binding().map(|binding| (*name, binding))
655+
}).collect::<Vec<_>>();
656+
for ((name, ns), binding) in bindings {
657+
if binding.defined_with(DefModifiers::IMPORTABLE | DefModifiers::PUBLIC) {
658+
let _ = module_.try_define_child(name, ns, directive.import(binding, None));
654659
}
655660
}
656661

0 commit comments

Comments
 (0)