Skip to content

Commit 060d439

Browse files
Make obligations_for_self_ty only return an obligation
1 parent 160b194 commit 060d439

File tree

2 files changed

+84
-71
lines changed

2 files changed

+84
-71
lines changed

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -225,33 +225,44 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
225225
&self,
226226
expected_vid: ty::TyVid,
227227
) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
228-
let expected_sig =
229-
self.obligations_for_self_ty(expected_vid).find_map(|(_, obligation)| {
230-
debug!(?obligation.predicate);
231-
232-
let bound_predicate = obligation.predicate.kind();
233-
if let ty::PredicateKind::Projection(proj_predicate) =
234-
obligation.predicate.kind().skip_binder()
235-
{
236-
// Given a Projection predicate, we can potentially infer
237-
// the complete signature.
238-
self.deduce_sig_from_projection(
239-
Some(obligation.cause.span),
240-
bound_predicate.rebind(proj_predicate),
241-
)
242-
} else {
243-
None
244-
}
245-
});
246-
228+
let mut expected_sig = None;
247229
// Even if we can't infer the full signature, we may be able to
248230
// infer the kind. This can occur when we elaborate a predicate
249231
// like `F : Fn<A>`. Note that due to subtyping we could encounter
250232
// many viable options, so pick the most restrictive.
251-
let expected_kind = self
252-
.obligations_for_self_ty(expected_vid)
253-
.filter_map(|(tr, _)| self.tcx.fn_trait_kind_from_lang_item(tr.def_id()))
254-
.fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur))));
233+
let mut expected_kind = None;
234+
235+
for obligation in self.obligations_for_self_ty(expected_vid) {
236+
debug!(?obligation.predicate);
237+
let bound_predicate = obligation.predicate.kind();
238+
239+
if expected_sig.is_none()
240+
&& let ty::PredicateKind::Projection(proj_predicate) = bound_predicate.skip_binder()
241+
{
242+
// Given a Projection predicate, we can potentially infer
243+
// the complete signature.
244+
expected_sig = self.deduce_sig_from_projection(
245+
Some(obligation.cause.span),
246+
bound_predicate.rebind(proj_predicate),
247+
);
248+
}
249+
250+
let trait_def_id = match bound_predicate.skip_binder() {
251+
ty::PredicateKind::Projection(data) => {
252+
Some(data.projection_ty.trait_def_id(self.tcx))
253+
}
254+
ty::PredicateKind::Trait(data) => Some(data.def_id()),
255+
_ => None,
256+
};
257+
if let Some(closure_kind) =
258+
trait_def_id.and_then(|def_id| self.tcx.fn_trait_kind_from_lang_item(def_id))
259+
{
260+
expected_kind = Some(
261+
expected_kind
262+
.map_or_else(|| closure_kind, |current| cmp::min(current, closure_kind)),
263+
);
264+
}
265+
}
255266

256267
(expected_sig, expected_kind)
257268
}
@@ -689,7 +700,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
689700

690701
let output_ty = match *ret_ty.kind() {
691702
ty::Infer(ty::TyVar(ret_vid)) => {
692-
self.obligations_for_self_ty(ret_vid).find_map(|(_, obligation)| {
703+
self.obligations_for_self_ty(ret_vid).find_map(|obligation| {
693704
get_future_output(obligation.predicate, obligation.cause.span)
694705
})?
695706
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMut
2121
use rustc_middle::ty::fold::TypeFoldable;
2222
use rustc_middle::ty::visit::TypeVisitable;
2323
use rustc_middle::ty::{
24-
self, AdtKind, CanonicalUserType, DefIdTree, EarlyBinder, GenericParamDefKind, ToPolyTraitRef,
25-
ToPredicate, Ty, UserType,
24+
self, AdtKind, CanonicalUserType, DefIdTree, EarlyBinder, GenericParamDefKind, ToPredicate, Ty,
25+
UserType,
2626
};
2727
use rustc_middle::ty::{GenericArgKind, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts};
2828
use rustc_session::lint;
@@ -650,12 +650,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
650650
}
651651

652652
#[instrument(skip(self), level = "debug")]
653-
fn self_type_matches_expected_vid(
654-
&self,
655-
trait_ref: ty::PolyTraitRef<'tcx>,
656-
expected_vid: ty::TyVid,
657-
) -> bool {
658-
let self_ty = self.shallow_resolve(trait_ref.skip_binder().self_ty());
653+
fn self_type_matches_expected_vid(&self, self_ty: Ty<'tcx>, expected_vid: ty::TyVid) -> bool {
654+
let self_ty = self.shallow_resolve(self_ty);
659655
debug!(?self_ty);
660656

661657
match *self_ty.kind() {
@@ -674,54 +670,60 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
674670
pub(in super::super) fn obligations_for_self_ty<'b>(
675671
&'b self,
676672
self_ty: ty::TyVid,
677-
) -> impl Iterator<Item = (ty::PolyTraitRef<'tcx>, traits::PredicateObligation<'tcx>)>
678-
+ Captures<'tcx>
679-
+ 'b {
673+
) -> impl Iterator<Item = traits::PredicateObligation<'tcx>> + Captures<'tcx> + 'b {
680674
// FIXME: consider using `sub_root_var` here so we
681675
// can see through subtyping.
682676
let ty_var_root = self.root_var(self_ty);
683677
trace!("pending_obligations = {:#?}", self.fulfillment_cx.borrow().pending_obligations());
684678

685-
self.fulfillment_cx
686-
.borrow()
687-
.pending_obligations()
688-
.into_iter()
689-
.filter_map(move |obligation| {
690-
let bound_predicate = obligation.predicate.kind();
691-
match bound_predicate.skip_binder() {
692-
ty::PredicateKind::Projection(data) => Some((
693-
bound_predicate.rebind(data).required_poly_trait_ref(self.tcx),
694-
obligation,
695-
)),
696-
ty::PredicateKind::Trait(data) => {
697-
Some((bound_predicate.rebind(data).to_poly_trait_ref(), obligation))
698-
}
699-
ty::PredicateKind::Subtype(..) => None,
700-
ty::PredicateKind::Coerce(..) => None,
701-
ty::PredicateKind::RegionOutlives(..) => None,
702-
ty::PredicateKind::TypeOutlives(..) => None,
703-
ty::PredicateKind::WellFormed(..) => None,
704-
ty::PredicateKind::ObjectSafe(..) => None,
705-
ty::PredicateKind::ConstEvaluatable(..) => None,
706-
ty::PredicateKind::ConstEquate(..) => None,
707-
// N.B., this predicate is created by breaking down a
708-
// `ClosureType: FnFoo()` predicate, where
709-
// `ClosureType` represents some `Closure`. It can't
710-
// possibly be referring to the current closure,
711-
// because we haven't produced the `Closure` for
712-
// this closure yet; this is exactly why the other
713-
// code is looking for a self type of an unresolved
714-
// inference variable.
715-
ty::PredicateKind::ClosureKind(..) => None,
716-
ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
679+
self.fulfillment_cx.borrow().pending_obligations().into_iter().filter_map(
680+
move |obligation| match &obligation.predicate.kind().skip_binder() {
681+
ty::PredicateKind::Projection(data)
682+
if self.self_type_matches_expected_vid(
683+
data.projection_ty.self_ty(),
684+
ty_var_root,
685+
) =>
686+
{
687+
Some(obligation)
717688
}
718-
})
719-
.filter(move |(tr, _)| self.self_type_matches_expected_vid(*tr, ty_var_root))
689+
ty::PredicateKind::Trait(data)
690+
if self.self_type_matches_expected_vid(data.self_ty(), ty_var_root) =>
691+
{
692+
Some(obligation)
693+
}
694+
695+
ty::PredicateKind::Trait(..)
696+
| ty::PredicateKind::Projection(..)
697+
| ty::PredicateKind::Subtype(..)
698+
| ty::PredicateKind::Coerce(..)
699+
| ty::PredicateKind::RegionOutlives(..)
700+
| ty::PredicateKind::TypeOutlives(..)
701+
| ty::PredicateKind::WellFormed(..)
702+
| ty::PredicateKind::ObjectSafe(..)
703+
| ty::PredicateKind::ConstEvaluatable(..)
704+
| ty::PredicateKind::ConstEquate(..)
705+
// N.B., this predicate is created by breaking down a
706+
// `ClosureType: FnFoo()` predicate, where
707+
// `ClosureType` represents some `Closure`. It can't
708+
// possibly be referring to the current closure,
709+
// because we haven't produced the `Closure` for
710+
// this closure yet; this is exactly why the other
711+
// code is looking for a self type of an unresolved
712+
// inference variable.
713+
| ty::PredicateKind::ClosureKind(..)
714+
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
715+
},
716+
)
720717
}
721718

722719
pub(in super::super) fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool {
723-
self.obligations_for_self_ty(self_ty)
724-
.any(|(tr, _)| Some(tr.def_id()) == self.tcx.lang_items().sized_trait())
720+
let sized_did = self.tcx.lang_items().sized_trait();
721+
self.obligations_for_self_ty(self_ty).any(|obligation| {
722+
match obligation.predicate.kind().skip_binder() {
723+
ty::PredicateKind::Trait(data) => Some(data.def_id()) == sized_did,
724+
_ => false,
725+
}
726+
})
725727
}
726728

727729
pub(in super::super) fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {

0 commit comments

Comments
 (0)