1
Fork 0

Rollup merge of #137910 - compiler-errors:async-fn-goal-error, r=oli-obk

Improve error message for `AsyncFn` trait failure for RPIT

Use a `WellFormedDerived` obligation cause to make sure we can turn an `AsyncFnKindHelper` trait goal into its parent `AsyncFn*` goal, then fix the logic for reporting `AsyncFn*` kind mismatches.

Best reviewed without whitespace.

Fixes #137905

r? oli-obk
This commit is contained in:
Michael Goulet 2025-03-06 12:22:25 -05:00 committed by GitHub
commit 1458b3560f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 101 additions and 54 deletions

View file

@ -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,7 +846,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let self_ty = trait_pred.self_ty().skip_binder();
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 (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)
@ -859,8 +871,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
_ => return None,
};
let expected_args =
trait_pred.map_bound(|trait_pred| trait_pred.trait_ref.args.type_at(1));
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.
@ -880,7 +891,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
closure_def_id,
found_kind,
expected_kind,
"",
trait_prefix,
);
self.note_obligation_cause(&mut err, &obligation);
return Some(err.emit());
@ -900,7 +911,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
self.note_obligation_cause(&mut err, &obligation);
return Some(err.emit());
}
}
None
}

View file

@ -3191,7 +3191,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
false
};
if !is_upvar_tys_infer_tuple {
let is_builtin_async_fn_trait =
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() {

View file

@ -983,8 +983,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(

View file

@ -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() {}

View file

@ -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`.