Skip to content

Commit

Permalink
Make super_traits_of return an iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Nov 27, 2020
1 parent 35bf466 commit 504d27c
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -2088,34 +2088,37 @@ impl<'tcx> TyCtxt<'tcx> {
/// Given the def_id of a Trait `trait_def_id` and the name of an associated item `assoc_name`
/// returns true if the `trait_def_id` defines an associated item of name `assoc_name`.
pub fn trait_may_define_assoc_type(self, trait_def_id: DefId, assoc_name: Ident) -> bool {
self.super_traits_of(trait_def_id).iter().any(|trait_did| {
self.associated_items(*trait_did)
.find_by_name_and_kind(self, assoc_name, ty::AssocKind::Type, *trait_did)
self.super_traits_of(trait_def_id).any(|trait_did| {
self.associated_items(trait_did)
.find_by_name_and_kind(self, assoc_name, ty::AssocKind::Type, trait_did)
.is_some()
})
}

/// Computes the def-ids of the transitive super-traits of `trait_def_id`. This (intentionally)
/// does not compute the full elaborated super-predicates but just the set of def-ids. It is used
/// to identify which traits may define a given associated type to help avoid cycle errors.
/// Returns `Lrc<FxHashSet<DefId>>` so that cloning is cheaper.
fn super_traits_of(self, trait_def_id: DefId) -> Lrc<FxHashSet<DefId>> {
/// Returns a `DefId` iterator.
fn super_traits_of(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
let mut set = FxHashSet::default();
let mut stack = vec![trait_def_id];
while let Some(trait_did) = stack.pop() {
if !set.insert(trait_did) {
continue;
}

set.insert(trait_def_id);

iter::from_fn(move || -> Option<DefId> {
let trait_did = stack.pop()?;
let generic_predicates = self.super_predicates_of(trait_did);

for (predicate, _) in generic_predicates.predicates {
if let ty::PredicateAtom::Trait(data, _) = predicate.skip_binders() {
stack.push(data.def_id());
if set.insert(data.def_id()) {
stack.push(data.def_id());
}
}
}
}

Lrc::new(set)
Some(trait_did)
})
}

/// Given a closure signature, returns an equivalent fn signature. Detuples
Expand Down

0 comments on commit 504d27c

Please sign in to comment.