Skip to content

Improve error message for AsyncFn trait failure for RPIT #137910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -829,7 +829,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& let ty::Closure(closure_def_id, _) | ty::CoroutineClosure(closure_def_id, _) =
*typeck_results.node_type(arg_hir_id).kind()
{
// Otherwise, extract the closure kind from the obligation.
// Otherwise, extract the closure kind from the obligation,
// but only if we actually have an argument to deduce the
// closure type from...
let mut err = self.report_closure_error(
&obligation,
closure_def_id,
@@ -844,63 +846,72 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {

let self_ty = trait_pred.self_ty().skip_binder();

if let Some(expected_kind) = self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()) {
let (closure_def_id, found_args, by_ref_captures) = match *self_ty.kind() {
ty::Closure(def_id, args) => {
(def_id, args.as_closure().sig().map_bound(|sig| sig.inputs()[0]), None)
}
ty::CoroutineClosure(def_id, args) => (
def_id,
args.as_coroutine_closure()
.coroutine_closure_sig()
.map_bound(|sig| sig.tupled_inputs_ty),
Some(args.as_coroutine_closure().coroutine_captures_by_ref_ty()),
),
_ => return None,
let (expected_kind, trait_prefix) =
if let Some(expected_kind) = self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()) {
(expected_kind, "")
} else if let Some(expected_kind) =
self.tcx.async_fn_trait_kind_from_def_id(trait_pred.def_id())
{
(expected_kind, "Async")
} else {
return None;
};

let expected_args =
trait_pred.map_bound(|trait_pred| trait_pred.trait_ref.args.type_at(1));

// Verify that the arguments are compatible. If the signature is
// mismatched, then we have a totally different error to report.
if self.enter_forall(found_args, |found_args| {
self.enter_forall(expected_args, |expected_args| {
!self.can_eq(obligation.param_env, expected_args, found_args)
})
}) {
return None;
let (closure_def_id, found_args, by_ref_captures) = match *self_ty.kind() {
ty::Closure(def_id, args) => {
(def_id, args.as_closure().sig().map_bound(|sig| sig.inputs()[0]), None)
}
ty::CoroutineClosure(def_id, args) => (
def_id,
args.as_coroutine_closure()
.coroutine_closure_sig()
.map_bound(|sig| sig.tupled_inputs_ty),
Some(args.as_coroutine_closure().coroutine_captures_by_ref_ty()),
),
_ => return None,
};

if let Some(found_kind) = self.closure_kind(self_ty)
&& !found_kind.extends(expected_kind)
{
let mut err = self.report_closure_error(
&obligation,
closure_def_id,
found_kind,
expected_kind,
"",
);
self.note_obligation_cause(&mut err, &obligation);
return Some(err.emit());
}
let expected_args = trait_pred.map_bound(|trait_pred| trait_pred.trait_ref.args.type_at(1));

// If the closure has captures, then perhaps the reason that the trait
// is unimplemented is because async closures don't implement `Fn`/`FnMut`
// if they have captures.
if let Some(by_ref_captures) = by_ref_captures
&& let ty::FnPtr(sig_tys, _) = by_ref_captures.kind()
&& !sig_tys.skip_binder().output().is_unit()
{
let mut err = self.dcx().create_err(AsyncClosureNotFn {
span: self.tcx.def_span(closure_def_id),
kind: expected_kind.as_str(),
});
self.note_obligation_cause(&mut err, &obligation);
return Some(err.emit());
}
// Verify that the arguments are compatible. If the signature is
// mismatched, then we have a totally different error to report.
if self.enter_forall(found_args, |found_args| {
self.enter_forall(expected_args, |expected_args| {
!self.can_eq(obligation.param_env, expected_args, found_args)
})
}) {
return None;
}

if let Some(found_kind) = self.closure_kind(self_ty)
&& !found_kind.extends(expected_kind)
{
let mut err = self.report_closure_error(
&obligation,
closure_def_id,
found_kind,
expected_kind,
trait_prefix,
);
self.note_obligation_cause(&mut err, &obligation);
return Some(err.emit());
}

// If the closure has captures, then perhaps the reason that the trait
// is unimplemented is because async closures don't implement `Fn`/`FnMut`
// if they have captures.
if let Some(by_ref_captures) = by_ref_captures
&& let ty::FnPtr(sig_tys, _) = by_ref_captures.kind()
&& !sig_tys.skip_binder().output().is_unit()
{
let mut err = self.dcx().create_err(AsyncClosureNotFn {
span: self.tcx.def_span(closure_def_id),
kind: expected_kind.as_str(),
});
self.note_obligation_cause(&mut err, &obligation);
return Some(err.emit());
}

None
}

Original file line number Diff line number Diff line change
@@ -3191,7 +3191,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
false
};

if !is_upvar_tys_infer_tuple {
let is_builtin_async_fn_trait =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, we'll report a misleading "due to this closure" note.

tcx.async_fn_trait_kind_from_def_id(data.parent_trait_pred.def_id()).is_some();

if !is_upvar_tys_infer_tuple && !is_builtin_async_fn_trait {
let ty_str = tcx.short_string(ty, err.long_ty_path());
let msg = format!("required because it appears within the type `{ty_str}`");
match ty.kind() {
Original file line number Diff line number Diff line change
@@ -984,8 +984,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
return Err(SelectionError::Unimplemented);
}
} else {
nested.push(obligation.with(
nested.push(Obligation::new(
self.tcx(),
obligation.derived_cause(ObligationCauseCode::BuiltinDerived),
obligation.param_env,
ty::TraitRef::new(
self.tcx(),
self.tcx().require_lang_item(
14 changes: 14 additions & 0 deletions tests/ui/async-await/async-closures/kind-due-to-rpit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ edition: 2024

// Make sure the error message is understandable when an `AsyncFn` goal is not satisfied
// (due to closure kind), and that goal originates from an RPIT.

fn repro(foo: impl Into<bool>) -> impl AsyncFn() {
let inner_fn = async move || {
//~^ ERROR expected a closure that implements the `AsyncFn` trait
let _ = foo.into();
};
inner_fn
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/async-await/async-closures/kind-due-to-rpit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0525]: expected a closure that implements the `AsyncFn` trait, but this closure only implements `AsyncFnOnce`
--> $DIR/kind-due-to-rpit.rs:7:20
|
LL | fn repro(foo: impl Into<bool>) -> impl AsyncFn() {
| -------------- the requirement to implement `AsyncFn` derives from here
LL | let inner_fn = async move || {
| ^^^^^^^^^^^^^ this closure implements `AsyncFnOnce`, not `AsyncFn`
LL |
LL | let _ = foo.into();
| --- closure is `AsyncFnOnce` because it moves the variable `foo` out of its environment
LL | };
LL | inner_fn
| -------- return type was inferred to be `{async closure@$DIR/kind-due-to-rpit.rs:7:20: 7:33}` here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0525`.
Loading