Skip to content

Commit 84a4095

Browse files
authoredDec 10, 2024
Rollup merge of rust-lang#134017 - compiler-errors:call-once-deduction, r=jieyouxu
Don't use `AsyncFnOnce::CallOnceFuture` bounds for signature deduction We shouldn't be using `AsyncFnOnce::CallOnceFuture` projection bounds to deduce anything about the return type of an async closure, **only** `AsyncFnOnce::Output`. This was accidental b/c all we were looking at was the def id of the trait, rather than the projection. This PR fixes that. This doesn't affect stable code, since `CallOnceFuture` bounds cannot be written on stable. Fixes rust-lang#134015
2 parents 4fe6179 + 88669ae commit 84a4095

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed
 

‎compiler/rustc_hir_typeck/src/closure.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -454,28 +454,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
454454
closure_kind: hir::ClosureKind,
455455
projection: ty::PolyProjectionPredicate<'tcx>,
456456
) -> Option<ExpectedSig<'tcx>> {
457-
let tcx = self.tcx;
458-
459-
let trait_def_id = projection.trait_def_id(tcx);
457+
let def_id = projection.projection_def_id();
460458

461459
// For now, we only do signature deduction based off of the `Fn` and `AsyncFn` traits,
462460
// for closures and async closures, respectively.
463461
match closure_kind {
464-
hir::ClosureKind::Closure
465-
if self.tcx.fn_trait_kind_from_def_id(trait_def_id).is_some() =>
466-
{
462+
hir::ClosureKind::Closure if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) => {
467463
self.extract_sig_from_projection(cause_span, projection)
468464
}
469465
hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
470-
if self.tcx.async_fn_trait_kind_from_def_id(trait_def_id).is_some() =>
466+
if self.tcx.is_lang_item(def_id, LangItem::AsyncFnOnceOutput) =>
471467
{
472468
self.extract_sig_from_projection(cause_span, projection)
473469
}
474470
// It's possible we've passed the closure to a (somewhat out-of-fashion)
475471
// `F: FnOnce() -> Fut, Fut: Future<Output = T>` style bound. Let's still
476472
// guide inference here, since it's beneficial for the user.
477473
hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
478-
if self.tcx.fn_trait_kind_from_def_id(trait_def_id).is_some() =>
474+
if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) =>
479475
{
480476
self.extract_sig_from_projection_and_future_bound(cause_span, projection)
481477
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ edition: 2021
2+
//@ check-pass
3+
4+
#![feature(async_closure, async_fn_traits, unboxed_closures)]
5+
6+
fn bar<F, O>(_: F)
7+
where
8+
F: AsyncFnOnce<(), CallOnceFuture = O>,
9+
{
10+
}
11+
12+
fn main() {
13+
bar(async move || {});
14+
}

0 commit comments

Comments
 (0)