Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9ed91f3

Browse files
committed
resolve: Add public entrypoint traits_in_module
- Consider the implicit prelude as well
1 parent 0647a48 commit 9ed91f3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/librustc_resolve/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,6 +3125,31 @@ impl<'a> Resolver<'a> {
31253125
})
31263126
}
31273127

3128+
pub fn traits_in_scope(&mut self, module_id: DefId) -> Vec<TraitCandidate> {
3129+
let module = self.get_module(module_id);
3130+
module.ensure_traits(self);
3131+
let traits = module.traits.borrow();
3132+
let to_candidate =
3133+
|this: &mut Self, &(trait_name, binding): &(Ident, &NameBinding<'_>)| TraitCandidate {
3134+
def_id: binding.res().def_id(),
3135+
import_ids: this.find_transitive_imports(&binding.kind, trait_name),
3136+
};
3137+
3138+
let mut candidates: Vec<_> =
3139+
traits.as_ref().unwrap().iter().map(|x| to_candidate(self, x)).collect();
3140+
3141+
if let Some(prelude) = self.prelude {
3142+
if !module.no_implicit_prelude {
3143+
prelude.ensure_traits(self);
3144+
candidates.extend(
3145+
prelude.traits.borrow().as_ref().unwrap().iter().map(|x| to_candidate(self, x)),
3146+
);
3147+
}
3148+
}
3149+
3150+
candidates
3151+
}
3152+
31283153
/// Rustdoc uses this to resolve things in a recoverable way. `ResolutionError<'a>`
31293154
/// isn't something that can be returned because it can't be made to live that long,
31303155
/// and also it's a private type. Fortunately rustdoc doesn't need to know the error,

0 commit comments

Comments
 (0)