Skip to content

Commit 3c62d90

Browse files
committed
Reallow methods from traits that are shadowed by non-import items
1 parent d7734ae commit 3c62d90

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/librustc_resolve/lib.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ pub struct ModuleS<'a> {
818818
// entry block for `f`.
819819
anonymous_children: RefCell<NodeMap<Module<'a>>>,
820820

821+
shadowed_traits: RefCell<Vec<&'a NameBinding<'a>>>,
822+
821823
// The number of unresolved globs that this module exports.
822824
glob_count: Cell<usize>,
823825

@@ -848,6 +850,7 @@ impl<'a> ModuleS<'a> {
848850
children: RefCell::new(HashMap::new()),
849851
imports: RefCell::new(Vec::new()),
850852
anonymous_children: RefCell::new(NodeMap()),
853+
shadowed_traits: RefCell::new(Vec::new()),
851854
glob_count: Cell::new(0),
852855
pub_count: Cell::new(0),
853856
pub_glob_count: Cell::new(0),
@@ -871,8 +874,19 @@ impl<'a> ModuleS<'a> {
871874
// Define the name or return the existing binding if there is a collision.
872875
fn try_define_child(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>)
873876
-> Result<(), &'a NameBinding<'a>> {
874-
self.children.borrow_mut().entry((name, ns)).or_insert_with(Default::default)
875-
.try_define(binding)
877+
let mut children = self.children.borrow_mut();
878+
let resolution = children.entry((name, ns)).or_insert_with(Default::default);
879+
880+
// FIXME #31379: We can use methods from imported traits shadowed by non-import items
881+
if let Some(old_binding) = resolution.binding {
882+
if !old_binding.is_import() && binding.is_import() {
883+
if let Some(Def::Trait(_)) = binding.def() {
884+
self.shadowed_traits.borrow_mut().push(binding);
885+
}
886+
}
887+
}
888+
889+
resolution.try_define(binding)
876890
}
877891

878892
fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) {
@@ -3466,6 +3480,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34663480
}
34673481
});
34683482

3483+
// Look for shadowed traits.
3484+
for binding in search_module.shadowed_traits.borrow().iter() {
3485+
let did = binding.def().unwrap().def_id();
3486+
if self.trait_item_map.contains_key(&(name, did)) {
3487+
add_trait_info(&mut found_traits, did, name);
3488+
let trait_name = self.get_trait_name(did);
3489+
self.record_use(trait_name, TypeNS, binding);
3490+
}
3491+
}
3492+
34693493
match search_module.parent_link {
34703494
NoParentLink | ModuleParentLink(..) => break,
34713495
BlockParentLink(parent_module, _) => {

0 commit comments

Comments
 (0)