Rollup merge of #101723 - lukas-code:await-diag, r=compiler-errors

Impove diagnostic for `.await`ing non-futures

Strip leading whitespace from the span and use a non-verbose suggestion.
fixes #101715
This commit is contained in:
Guillaume Gomez 2022-09-12 22:47:15 +02:00 committed by GitHub
commit 8dc4b26b60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 17 deletions

View File

@ -146,13 +146,19 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'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,

View File

@ -1160,8 +1160,8 @@ fn suggest_remove_await(&self, obligation: &PredicateObligation<'tcx>, err: &mut
// 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,

View File

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

View File

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

View File

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

View File

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