Skip to content

Commit

Permalink
Rollup merge of rust-lang#101723 - lukas-code:await-diag, r=compiler-…
Browse files Browse the repository at this point in the history
…errors

Impove diagnostic for `.await`ing non-futures

Strip leading whitespace from the span and use a non-verbose suggestion.
fixes rust-lang#101715
  • Loading branch information
GuillaumeGomez authored Sep 12, 2022
2 parents 091e699 + 2b7fb8d commit 5233f68
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
12 changes: 9 additions & 3 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/async-await/issue-101715.rs
Original file line number Diff line number Diff line change
@@ -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() {}
16 changes: 16 additions & 0 deletions src/test/ui/async-await/issue-101715.stderr
Original file line number Diff line number Diff line change
@@ -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`.
10 changes: 4 additions & 6 deletions src/test/ui/async-await/issue-70594.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions src/test/ui/async-await/issues/issue-62009-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5233f68

Please sign in to comment.