Skip to content

Commit 9c8037c

Browse files
Deduce closure signature from TAIT supertraits
1 parent 033cf98 commit 9c8037c

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -173,34 +173,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
173173
expected_ty: Ty<'tcx>,
174174
) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
175175
match *expected_ty.kind() {
176-
ty::Opaque(def_id, substs) => {
177-
let bounds = self.tcx.bound_explicit_item_bounds(def_id);
178-
let sig =
179-
bounds.subst_iter_copied(self.tcx, substs).find_map(|(pred, span)| match pred
180-
.kind()
181-
.skip_binder()
182-
{
183-
ty::PredicateKind::Projection(proj_predicate) => self
184-
.deduce_sig_from_projection(
185-
Some(span),
186-
pred.kind().rebind(proj_predicate),
187-
),
188-
_ => None,
189-
});
190-
191-
let kind = bounds
192-
.0
193-
.iter()
194-
.filter_map(|(pred, _)| match pred.kind().skip_binder() {
195-
ty::PredicateKind::Trait(tp) => {
196-
self.tcx.fn_trait_kind_from_lang_item(tp.def_id())
197-
}
198-
_ => None,
199-
})
200-
.fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur))));
201-
trace!(?sig, ?kind);
202-
(sig, kind)
203-
}
176+
ty::Opaque(def_id, substs) => self.deduce_signature_from_predicates(
177+
self.tcx.bound_explicit_item_bounds(def_id).subst_iter_copied(self.tcx, substs),
178+
),
204179
ty::Dynamic(ref object_type, ..) => {
205180
let sig = object_type.projection_bounds().find_map(|pb| {
206181
let pb = pb.with_self_ty(self.tcx, self.tcx.types.trait_object_dummy_self);
@@ -211,7 +186,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
211186
.and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did));
212187
(sig, kind)
213188
}
214-
ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),
189+
ty::Infer(ty::TyVar(vid)) => self.deduce_signature_from_predicates(
190+
self.obligations_for_self_ty(vid).map(|obl| (obl.predicate, obl.cause.span)),
191+
),
215192
ty::FnPtr(sig) => {
216193
let expected_sig = ExpectedSig { cause_span: None, sig };
217194
(Some(expected_sig), Some(ty::ClosureKind::Fn))
@@ -220,19 +197,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
220197
}
221198
}
222199

223-
fn deduce_expectations_from_obligations(
200+
fn deduce_signature_from_predicates(
224201
&self,
225-
expected_vid: ty::TyVid,
202+
predicates: impl DoubleEndedIterator<Item = (ty::Predicate<'tcx>, Span)>,
226203
) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
227204
let mut expected_sig = None;
228205
let mut expected_kind = None;
229206

230-
for obligation in traits::elaborate_obligations(
207+
for obligation in traits::elaborate_predicates_with_span(
231208
self.tcx,
232209
// Reverse the obligations here, since `elaborate_*` uses a stack,
233210
// and we want to keep inference generally in the same order of
234211
// the registered obligations.
235-
self.obligations_for_self_ty(expected_vid).rev().collect(),
212+
predicates.rev(),
236213
) {
237214
debug!(?obligation.predicate);
238215
let bound_predicate = obligation.predicate.kind();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
3+
#![feature(type_alias_impl_trait)]
4+
5+
trait SuperExpectation: Fn(i32) {}
6+
7+
impl<T: Fn(i32)> SuperExpectation for T {}
8+
9+
type Foo = impl SuperExpectation;
10+
11+
fn main() {
12+
let _: Foo = |x| {
13+
let _ = x.to_string();
14+
};
15+
}

0 commit comments

Comments
 (0)