Skip to content

Commit c815e03

Browse files
authored
Rollup merge of #108424 - megakorre:elaborator_refactor, r=compiler-errors
rustc_infer: Consolidate obligation elaboration de-duplication # Explanation The obligations `Elaborator` is doing de-duplication of obligations in 3 different locations. 1 off which has a comment. This PR consolidates the functionality and comment to a single function.
2 parents 786a75a + 3d34538 commit c815e03

File tree

1 file changed

+15
-16
lines changed
  • compiler/rustc_infer/src/traits

1 file changed

+15
-16
lines changed

compiler/rustc_infer/src/traits/util.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ pub fn elaborate_predicates_with_span<'tcx>(
116116

117117
pub fn elaborate_obligations<'tcx>(
118118
tcx: TyCtxt<'tcx>,
119-
mut obligations: Vec<PredicateObligation<'tcx>>,
119+
obligations: Vec<PredicateObligation<'tcx>>,
120120
) -> Elaborator<'tcx> {
121-
let mut visited = PredicateSet::new(tcx);
122-
obligations.retain(|obligation| visited.insert(obligation.predicate));
123-
Elaborator { stack: obligations, visited }
121+
let mut elaborator = Elaborator { stack: Vec::new(), visited: PredicateSet::new(tcx) };
122+
elaborator.extend_deduped(obligations);
123+
elaborator
124124
}
125125

126126
fn predicate_obligation<'tcx>(
@@ -132,6 +132,15 @@ fn predicate_obligation<'tcx>(
132132
}
133133

134134
impl<'tcx> Elaborator<'tcx> {
135+
fn extend_deduped(&mut self, obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>) {
136+
// Only keep those bounds that we haven't already seen.
137+
// This is necessary to prevent infinite recursion in some
138+
// cases. One common case is when people define
139+
// `trait Sized: Sized { }` rather than `trait Sized { }`.
140+
// let visited = &mut self.visited;
141+
self.stack.extend(obligations.into_iter().filter(|o| self.visited.insert(o.predicate)));
142+
}
143+
135144
pub fn filter_to_traits(self) -> FilterToTraits<Self> {
136145
FilterToTraits::new(self)
137146
}
@@ -172,15 +181,7 @@ impl<'tcx> Elaborator<'tcx> {
172181
)
173182
});
174183
debug!(?data, ?obligations, "super_predicates");
175-
176-
// Only keep those bounds that we haven't already seen.
177-
// This is necessary to prevent infinite recursion in some
178-
// cases. One common case is when people define
179-
// `trait Sized: Sized { }` rather than `trait Sized { }`.
180-
let visited = &mut self.visited;
181-
let obligations = obligations.filter(|o| visited.insert(o.predicate));
182-
183-
self.stack.extend(obligations);
184+
self.extend_deduped(obligations);
184185
}
185186
ty::PredicateKind::WellFormed(..) => {
186187
// Currently, we do not elaborate WF predicates,
@@ -237,10 +238,9 @@ impl<'tcx> Elaborator<'tcx> {
237238
return;
238239
}
239240

240-
let visited = &mut self.visited;
241241
let mut components = smallvec![];
242242
push_outlives_components(tcx, ty_max, &mut components);
243-
self.stack.extend(
243+
self.extend_deduped(
244244
components
245245
.into_iter()
246246
.filter_map(|component| match component {
@@ -280,7 +280,6 @@ impl<'tcx> Elaborator<'tcx> {
280280
.map(|predicate_kind| {
281281
bound_predicate.rebind(predicate_kind).to_predicate(tcx)
282282
})
283-
.filter(|&predicate| visited.insert(predicate))
284283
.map(|predicate| {
285284
predicate_obligation(
286285
predicate,

0 commit comments

Comments
 (0)