Skip to content

Commit 504d27c

Browse files
committed
Make super_traits_of return an iterator
1 parent 35bf466 commit 504d27c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

compiler/rustc_middle/src/ty/context.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -2088,34 +2088,37 @@ impl<'tcx> TyCtxt<'tcx> {
20882088
/// Given the def_id of a Trait `trait_def_id` and the name of an associated item `assoc_name`
20892089
/// returns true if the `trait_def_id` defines an associated item of name `assoc_name`.
20902090
pub fn trait_may_define_assoc_type(self, trait_def_id: DefId, assoc_name: Ident) -> bool {
2091-
self.super_traits_of(trait_def_id).iter().any(|trait_did| {
2092-
self.associated_items(*trait_did)
2093-
.find_by_name_and_kind(self, assoc_name, ty::AssocKind::Type, *trait_did)
2091+
self.super_traits_of(trait_def_id).any(|trait_did| {
2092+
self.associated_items(trait_did)
2093+
.find_by_name_and_kind(self, assoc_name, ty::AssocKind::Type, trait_did)
20942094
.is_some()
20952095
})
20962096
}
20972097

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

2106+
set.insert(trait_def_id);
2107+
2108+
iter::from_fn(move || -> Option<DefId> {
2109+
let trait_did = stack.pop()?;
21102110
let generic_predicates = self.super_predicates_of(trait_did);
2111+
21112112
for (predicate, _) in generic_predicates.predicates {
21122113
if let ty::PredicateAtom::Trait(data, _) = predicate.skip_binders() {
2113-
stack.push(data.def_id());
2114+
if set.insert(data.def_id()) {
2115+
stack.push(data.def_id());
2116+
}
21142117
}
21152118
}
2116-
}
21172119

2118-
Lrc::new(set)
2120+
Some(trait_did)
2121+
})
21192122
}
21202123

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

0 commit comments

Comments
 (0)