Make super_traits_of return an iterator

This commit is contained in:
Santiago Pastorino 2020-11-25 13:24:05 -03:00
parent 35bf466a27
commit 504d27cb0c
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF

View File

@ -2088,9 +2088,9 @@ 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()
})
}
@ -2098,24 +2098,27 @@ impl<'tcx> TyCtxt<'tcx> {
/// 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