Rollup merge of #107902 - vincenzopalazzo:macros/async_fn_suggestion, r=compiler-errors
fix: improve the suggestion on future not awaited Considering the following code ```rust fn foo() -> u8 { async fn async_fn() -> u8 { 22 } async_fn() } fn main() {} ``` the error generated before this commit from the compiler is ``` ➜ rust git:(macros/async_fn_suggestion) ✗ rustc test.rs --edition 2021 error[E0308]: mismatched types --> test.rs:4:5 | 1 | fn foo() -> u8 { | -- expected `u8` because of return type ... 4 | async_fn() | ^^^^^^^^^^ expected `u8`, found opaque type | = note: expected type `u8` found opaque type `impl Future<Output = u8>` help: consider `await`ing on the `Future` | 4 | async_fn().await | ++++++ error: aborting due to previous error ``` In this case the error is nor perfect, and can confuse the user that do not know that the opaque type is the future. So this commit will propose (and conclude the work start in https://github.com/rust-lang/rust/issues/80658) to change the string `opaque type` to `future` when applicable and also remove the Expected vs Received note by adding a more specific one regarding the async function that return a future type. So the new error emitted by the compiler is ``` error[E0308]: mismatched types --> test.rs:4:5 | 1 | fn foo() -> u8 { | -- expected `u8` because of return type ... 4 | async_fn() | ^^^^^^^^^^ expected `u8`, found future | note: calling an async function returns a future --> test.rs:4:5 | 4 | async_fn() | ^^^^^^^^^^ help: consider `await`ing on the `Future` | 4 | async_fn().await | ++++++ error: aborting due to previous error ``` Fixes https://github.com/rust-lang/rust/issues/80658 It remains to rework the case described in the following issue https://github.com/rust-lang/rust/issues/107899 but I think this deserves its own PR after we discuss a little bit how to handle these kinds of cases. r? `@eholk` `@rustbot` label +I-async-nominated Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
commit
73b022b8e1
22 changed files with 132 additions and 116 deletions
|
@ -1783,14 +1783,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
}
|
||||
}))
|
||||
{
|
||||
diag.note_expected_found_extra(
|
||||
&expected_label,
|
||||
expected,
|
||||
&found_label,
|
||||
found,
|
||||
&sort_string(values.expected, exp_p),
|
||||
&sort_string(values.found, found_p),
|
||||
);
|
||||
if let Some(ExpectedFound { found: found_ty, .. }) = exp_found {
|
||||
// `Future` is a special opaque type that the compiler
|
||||
// will try to hide in some case such as `async fn`, so
|
||||
// to make an error more use friendly we will
|
||||
// avoid to suggest a mismatch type with a
|
||||
// type that the user usually are not usign
|
||||
// directly such as `impl Future<Output = u8>`.
|
||||
if !self.tcx.ty_is_opaque_future(found_ty) {
|
||||
diag.note_expected_found_extra(
|
||||
&expected_label,
|
||||
expected,
|
||||
&found_label,
|
||||
found,
|
||||
&sort_string(values.expected, exp_p),
|
||||
&sort_string(values.found, found_p),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
@ -2854,6 +2864,7 @@ impl IntoDiagnosticArg for ObligationCauseAsDiagArg<'_> {
|
|||
pub enum TyCategory {
|
||||
Closure,
|
||||
Opaque,
|
||||
OpaqueFuture,
|
||||
Generator(hir::GeneratorKind),
|
||||
Foreign,
|
||||
}
|
||||
|
@ -2863,6 +2874,7 @@ impl TyCategory {
|
|||
match self {
|
||||
Self::Closure => "closure",
|
||||
Self::Opaque => "opaque type",
|
||||
Self::OpaqueFuture => "future",
|
||||
Self::Generator(gk) => gk.descr(),
|
||||
Self::Foreign => "foreign type",
|
||||
}
|
||||
|
@ -2871,7 +2883,11 @@ impl TyCategory {
|
|||
pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
|
||||
match *ty.kind() {
|
||||
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
|
||||
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => Some((Self::Opaque, def_id)),
|
||||
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
|
||||
let kind =
|
||||
if tcx.ty_is_opaque_future(ty) { Self::OpaqueFuture } else { Self::Opaque };
|
||||
Some((kind, def_id))
|
||||
}
|
||||
ty::Generator(def_id, ..) => {
|
||||
Some((Self::Generator(tcx.generator_kind(def_id).unwrap()), def_id))
|
||||
}
|
||||
|
|
|
@ -238,31 +238,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
}
|
||||
},
|
||||
(_, Some(ty)) if self.same_type_modulo_infer(exp_found.expected, ty) => {
|
||||
diag.span_suggestion_verbose(
|
||||
exp_span.shrink_to_hi(),
|
||||
"consider `await`ing on the `Future`",
|
||||
".await",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
self.suggest_await_on_future(diag, exp_span);
|
||||
diag.span_note(exp_span, "calling an async function returns a future");
|
||||
}
|
||||
(Some(ty), _) if self.same_type_modulo_infer(ty, exp_found.found) => match cause.code()
|
||||
{
|
||||
ObligationCauseCode::Pattern { span: Some(then_span), .. } => {
|
||||
diag.span_suggestion_verbose(
|
||||
then_span.shrink_to_hi(),
|
||||
"consider `await`ing on the `Future`",
|
||||
".await",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
self.suggest_await_on_future(diag, then_span.shrink_to_hi());
|
||||
}
|
||||
ObligationCauseCode::IfExpression(box IfExpressionCause { then_id, .. }) => {
|
||||
let then_span = self.find_block_span_from_hir_id(*then_id);
|
||||
diag.span_suggestion_verbose(
|
||||
then_span.shrink_to_hi(),
|
||||
"consider `await`ing on the `Future`",
|
||||
".await",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
self.suggest_await_on_future(diag, then_span.shrink_to_hi());
|
||||
}
|
||||
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
|
||||
ref prior_arms,
|
||||
|
@ -283,6 +269,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn suggest_await_on_future(&self, diag: &mut Diagnostic, sp: Span) {
|
||||
diag.span_suggestion_verbose(
|
||||
sp.shrink_to_hi(),
|
||||
"consider `await`ing on the `Future`",
|
||||
".await",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn suggest_accessing_field_where_appropriate(
|
||||
&self,
|
||||
cause: &ObligationCause<'tcx>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue