From 2b7fb8d9418dbf98e6ee8ed406f830b4fcbea55f Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Mon, 12 Sep 2022 16:54:25 +0200 Subject: [PATCH] Impove diagnostic for .await-ing non-futures --- compiler/rustc_ast_lowering/src/expr.rs | 12 +++++++++--- .../src/traits/error_reporting/suggestions.rs | 4 ++-- src/test/ui/async-await/issue-101715.rs | 17 +++++++++++++++++ src/test/ui/async-await/issue-101715.stderr | 16 ++++++++++++++++ src/test/ui/async-await/issue-70594.stderr | 10 ++++------ .../ui/async-await/issues/issue-62009-1.stderr | 10 ++++------ 6 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 src/test/ui/async-await/issue-101715.rs create mode 100644 src/test/ui/async-await/issue-101715.stderr diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index f929549d70448..cd03e3fb4572d 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -146,13 +146,19 @@ impl<'hir> LoweringContext<'_, 'hir> { |this| this.with_new_scopes(|this| this.lower_block_expr(block)), ), ExprKind::Await(ref expr) => { - let span = if expr.span.hi() < e.span.hi() { - expr.span.shrink_to_hi().with_hi(e.span.hi()) + let dot_await_span = if expr.span.hi() < e.span.hi() { + let span_with_whitespace = self + .tcx + .sess + .source_map() + .span_extend_while(expr.span, char::is_whitespace) + .unwrap_or(expr.span); + span_with_whitespace.shrink_to_hi().with_hi(e.span.hi()) } else { // this is a recovered `await expr` e.span }; - self.lower_expr_await(span, expr) + self.lower_expr_await(dot_await_span, expr) } ExprKind::Closure( ref binder, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index ecbeb9d79b118..cb605cacc9c98 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1160,8 +1160,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // and if not maybe suggest doing something else? If we kept the expression around we // could also check if it is an fn call (very likely) and suggest changing *that*, if // it is from the local crate. - err.span_suggestion_verbose( - expr.span.shrink_to_hi().with_hi(span.hi()), + err.span_suggestion( + span, "remove the `.await`", "", Applicability::MachineApplicable, diff --git a/src/test/ui/async-await/issue-101715.rs b/src/test/ui/async-await/issue-101715.rs new file mode 100644 index 0000000000000..1be5d02482e84 --- /dev/null +++ b/src/test/ui/async-await/issue-101715.rs @@ -0,0 +1,17 @@ +// edition:2018 + +struct S; + +impl S { + fn very_long_method_name_the_longest_method_name_in_the_whole_universe(self) {} +} + +async fn foo() { + S.very_long_method_name_the_longest_method_name_in_the_whole_universe() + .await + //~^ error: `()` is not a future + //~| help: remove the `.await` + //~| help: the trait `Future` is not implemented for `()` +} + +fn main() {} diff --git a/src/test/ui/async-await/issue-101715.stderr b/src/test/ui/async-await/issue-101715.stderr new file mode 100644 index 0000000000000..a0e8d2a894371 --- /dev/null +++ b/src/test/ui/async-await/issue-101715.stderr @@ -0,0 +1,16 @@ +error[E0277]: `()` is not a future + --> $DIR/issue-101715.rs:11:9 + | +LL | .await + | ^^^^^^ + | | + | `()` is not a future + | help: remove the `.await` + | + = help: the trait `Future` is not implemented for `()` + = note: () must be a future or must implement `IntoFuture` to be awaited + = note: required for `()` to implement `IntoFuture` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/async-await/issue-70594.stderr b/src/test/ui/async-await/issue-70594.stderr index f6ff52a5fd29f..d3cf57d3b1402 100644 --- a/src/test/ui/async-await/issue-70594.stderr +++ b/src/test/ui/async-await/issue-70594.stderr @@ -22,16 +22,14 @@ error[E0277]: `()` is not a future --> $DIR/issue-70594.rs:4:11 | LL | [1; ().await]; - | ^^^^^^ `()` is not a future + | ^^^^^^ + | | + | `()` is not a future + | help: remove the `.await` | = help: the trait `Future` is not implemented for `()` = note: () must be a future or must implement `IntoFuture` to be awaited = note: required for `()` to implement `IntoFuture` -help: remove the `.await` - | -LL - [1; ().await]; -LL + [1; ()]; - | error: aborting due to 4 previous errors diff --git a/src/test/ui/async-await/issues/issue-62009-1.stderr b/src/test/ui/async-await/issues/issue-62009-1.stderr index 0e323443ae8b3..222afb2c7b2bb 100644 --- a/src/test/ui/async-await/issues/issue-62009-1.stderr +++ b/src/test/ui/async-await/issues/issue-62009-1.stderr @@ -28,16 +28,14 @@ error[E0277]: `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future --> $DIR/issue-62009-1.rs:12:15 | LL | (|_| 2333).await; - | ^^^^^^ `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future + | ^^^^^^ + | | + | `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future + | help: remove the `.await` | = help: the trait `Future` is not implemented for closure `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` = note: [closure@$DIR/issue-62009-1.rs:12:6: 12:9] must be a future or must implement `IntoFuture` to be awaited = note: required for `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` to implement `IntoFuture` -help: remove the `.await` - | -LL - (|_| 2333).await; -LL + (|_| 2333); - | error: aborting due to 4 previous errors