Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5233f68

Browse files
authoredSep 12, 2022
Rollup merge of rust-lang#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 rust-lang#101715
2 parents 091e699 + 2b7fb8d commit 5233f68

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed
 

‎compiler/rustc_ast_lowering/src/expr.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
146146
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
147147
),
148148
ExprKind::Await(ref expr) => {
149-
let span = if expr.span.hi() < e.span.hi() {
150-
expr.span.shrink_to_hi().with_hi(e.span.hi())
149+
let dot_await_span = if expr.span.hi() < e.span.hi() {
150+
let span_with_whitespace = self
151+
.tcx
152+
.sess
153+
.source_map()
154+
.span_extend_while(expr.span, char::is_whitespace)
155+
.unwrap_or(expr.span);
156+
span_with_whitespace.shrink_to_hi().with_hi(e.span.hi())
151157
} else {
152158
// this is a recovered `await expr`
153159
e.span
154160
};
155-
self.lower_expr_await(span, expr)
161+
self.lower_expr_await(dot_await_span, expr)
156162
}
157163
ExprKind::Closure(
158164
ref binder,

‎compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11601160
// and if not maybe suggest doing something else? If we kept the expression around we
11611161
// could also check if it is an fn call (very likely) and suggest changing *that*, if
11621162
// it is from the local crate.
1163-
err.span_suggestion_verbose(
1164-
expr.span.shrink_to_hi().with_hi(span.hi()),
1163+
err.span_suggestion(
1164+
span,
11651165
"remove the `.await`",
11661166
"",
11671167
Applicability::MachineApplicable,
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition:2018
2+
3+
struct S;
4+
5+
impl S {
6+
fn very_long_method_name_the_longest_method_name_in_the_whole_universe(self) {}
7+
}
8+
9+
async fn foo() {
10+
S.very_long_method_name_the_longest_method_name_in_the_whole_universe()
11+
.await
12+
//~^ error: `()` is not a future
13+
//~| help: remove the `.await`
14+
//~| help: the trait `Future` is not implemented for `()`
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0277]: `()` is not a future
2+
--> $DIR/issue-101715.rs:11:9
3+
|
4+
LL | .await
5+
| ^^^^^^
6+
| |
7+
| `()` is not a future
8+
| help: remove the `.await`
9+
|
10+
= help: the trait `Future` is not implemented for `()`
11+
= note: () must be a future or must implement `IntoFuture` to be awaited
12+
= note: required for `()` to implement `IntoFuture`
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0277`.

‎src/test/ui/async-await/issue-70594.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ error[E0277]: `()` is not a future
2222
--> $DIR/issue-70594.rs:4:11
2323
|
2424
LL | [1; ().await];
25-
| ^^^^^^ `()` is not a future
25+
| ^^^^^^
26+
| |
27+
| `()` is not a future
28+
| help: remove the `.await`
2629
|
2730
= help: the trait `Future` is not implemented for `()`
2831
= note: () must be a future or must implement `IntoFuture` to be awaited
2932
= note: required for `()` to implement `IntoFuture`
30-
help: remove the `.await`
31-
|
32-
LL - [1; ().await];
33-
LL + [1; ()];
34-
|
3533

3634
error: aborting due to 4 previous errors
3735

‎src/test/ui/async-await/issues/issue-62009-1.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ error[E0277]: `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future
2828
--> $DIR/issue-62009-1.rs:12:15
2929
|
3030
LL | (|_| 2333).await;
31-
| ^^^^^^ `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future
31+
| ^^^^^^
32+
| |
33+
| `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future
34+
| help: remove the `.await`
3235
|
3336
= help: the trait `Future` is not implemented for closure `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]`
3437
= note: [closure@$DIR/issue-62009-1.rs:12:6: 12:9] must be a future or must implement `IntoFuture` to be awaited
3538
= note: required for `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` to implement `IntoFuture`
36-
help: remove the `.await`
37-
|
38-
LL - (|_| 2333).await;
39-
LL + (|_| 2333);
40-
|
4139

4240
error: aborting due to 4 previous errors
4341

0 commit comments

Comments
 (0)
Please sign in to comment.