Skip to content

Commit eeb8c8d

Browse files
committed
cleanup deduce_expectations_from_obligations
1 parent f4dc1c5 commit eeb8c8d

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

src/librustc_typeck/check/closure.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
219219
&self,
220220
expected_vid: ty::TyVid,
221221
) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
222-
let fulfillment_cx = self.fulfillment_cx.borrow();
223-
// Here `expected_ty` is known to be a type inference variable.
224-
225-
let expected_vid = self.root_var(expected_vid);
226-
let expected_sig = fulfillment_cx
227-
.pending_obligations()
228-
.iter()
229-
.filter_map(|obligation| {
222+
let expected_sig = self.obligations_for_self_ty(expected_vid)
223+
.find_map(|(_, obligation)| {
230224
debug!(
231225
"deduce_expectations_from_obligations: obligation.predicate={:?}",
232226
obligation.predicate
@@ -235,27 +229,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
235229
if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate {
236230
// Given a Projection predicate, we can potentially infer
237231
// the complete signature.
238-
let trait_ref = proj_predicate.to_poly_trait_ref(self.tcx);
239-
Some(()).filter(|()| {
240-
self.self_type_matches_expected_vid(trait_ref, expected_vid)
241-
}).and_then(|()| {
242-
self.deduce_sig_from_projection(
243-
Some(obligation.cause.span),
244-
proj_predicate
245-
)
246-
})
232+
self.deduce_sig_from_projection(
233+
Some(obligation.cause.span),
234+
proj_predicate
235+
)
247236
} else {
248237
None
249238
}
250-
})
251-
.next();
239+
});
252240

253241
// Even if we can't infer the full signature, we may be able to
254242
// infer the kind. This can occur if there is a trait-reference
255243
// like `F : Fn<A>`. Note that due to subtyping we could encounter
256244
// many viable options, so pick the most restrictive.
257245
let expected_kind = self.obligations_for_self_ty(expected_vid)
258-
.filter_map(|tr| self.tcx.lang_items().fn_trait_kind(tr.def_id()))
246+
.filter_map(|(tr, _)| self.tcx.lang_items().fn_trait_kind(tr.def_id()))
259247
.fold(None, |best, cur| {
260248
Some(best.map_or(cur, |best| cmp::min(best, cur)))
261249
});

src/librustc_typeck/check/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -2753,7 +2753,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
27532753
}
27542754

27552755
fn obligations_for_self_ty<'b>(&'b self, self_ty: ty::TyVid)
2756-
-> impl Iterator<Item=ty::PolyTraitRef<'tcx>> + Captures<'gcx> + 'b
2756+
-> impl Iterator<Item=(ty::PolyTraitRef<'tcx>, traits::PredicateObligation<'tcx>)>
2757+
+ Captures<'gcx> + 'b
27572758
{
27582759
let ty_var_root = self.root_var(self_ty);
27592760
debug!("obligations_for_self_ty: self_ty={:?} ty_var_root={:?} pending_obligations={:?}",
@@ -2765,8 +2766,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
27652766
.pending_obligations()
27662767
.into_iter()
27672768
.filter_map(move |obligation| match obligation.predicate {
2768-
ty::Predicate::Projection(ref data) => Some(data.to_poly_trait_ref(self.tcx)),
2769-
ty::Predicate::Trait(ref data) => Some(data.to_poly_trait_ref()),
2769+
ty::Predicate::Projection(ref data) =>
2770+
Some((data.to_poly_trait_ref(self.tcx), obligation)),
2771+
ty::Predicate::Trait(ref data) =>
2772+
Some((data.to_poly_trait_ref(), obligation)),
27702773
ty::Predicate::Subtype(..) => None,
27712774
ty::Predicate::RegionOutlives(..) => None,
27722775
ty::Predicate::TypeOutlives(..) => None,
@@ -2782,11 +2785,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
27822785
// code is looking for a self type of a unresolved
27832786
// inference variable.
27842787
ty::Predicate::ClosureKind(..) => None,
2785-
}).filter(move |tr| self.self_type_matches_expected_vid(*tr, ty_var_root))
2788+
}).filter(move |(tr, _)| self.self_type_matches_expected_vid(*tr, ty_var_root))
27862789
}
27872790

27882791
fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool {
2789-
self.obligations_for_self_ty(self_ty).any(|tr| {
2792+
self.obligations_for_self_ty(self_ty).any(|(tr, _)| {
27902793
Some(tr.def_id()) == self.tcx.lang_items().sized_trait()
27912794
})
27922795
}

0 commit comments

Comments
 (0)