Skip to content
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

Impove diagnostic for .awaiting non-futures #101723

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
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
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