Skip to content

Commit 3a0227b

Browse files
committed
Silence unnecessary await foo? knock-down error
1 parent 62ba365 commit 3a0227b

File tree

3 files changed

+40
-68
lines changed

3 files changed

+40
-68
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,13 @@ impl<'a> Parser<'a> {
12071207
self.recover_await_prefix(await_sp)?
12081208
};
12091209
let sp = self.error_on_incorrect_await(lo, hi, &expr, is_question);
1210-
let expr = self.mk_expr(lo.to(sp), ExprKind::Await(expr), attrs);
1210+
let kind = match expr.kind {
1211+
// Avoid knock-down errors as we don't know whether to interpret this as `foo().await?`
1212+
// or `foo()?.await` (the very reason we went with postfix syntax 😅).
1213+
ExprKind::Try(_) => ExprKind::Err,
1214+
_ => ExprKind::Await(expr),
1215+
};
1216+
let expr = self.mk_expr(lo.to(sp), kind, attrs);
12111217
self.maybe_recover_from_bad_qpath(expr, true)
12121218
}
12131219

src/test/ui/async-await/await-keyword/incorrect-syntax-suggestions.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ async fn foo2() -> Result<(), ()> {
1414
}
1515
async fn foo3() -> Result<(), ()> {
1616
let _ = await bar()?; //~ ERROR incorrect use of `await`
17-
//~^ ERROR the `?` operator can only be applied to values that implement `Try`
1817
Ok(())
1918
}
2019
async fn foo21() -> Result<(), ()> {
@@ -60,9 +59,7 @@ fn foo10() -> Result<(), ()> {
6059
Ok(())
6160
}
6261
fn foo11() -> Result<(), ()> {
63-
let _ = await bar()?; //~ ERROR `await` is only allowed inside `async` functions and blocks
64-
//~^ ERROR incorrect use of `await`
65-
//~| ERROR the `?` operator can only be applied to values that implement `Try`
62+
let _ = await bar()?; //~ ERROR incorrect use of `await`
6663
Ok(())
6764
}
6865
fn foo12() -> Result<(), ()> {

src/test/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,117 +17,117 @@ LL | let _ = await bar()?;
1717
| ^^^^^^^^^^^^ help: `await` is a postfix operation: `bar()?.await`
1818

1919
error: incorrect use of `await`
20-
--> $DIR/incorrect-syntax-suggestions.rs:21:13
20+
--> $DIR/incorrect-syntax-suggestions.rs:20:13
2121
|
2222
LL | let _ = await { bar() };
2323
| ^^^^^^^^^^^^^^^ help: `await` is a postfix operation: `{ bar() }.await`
2424

2525
error: incorrect use of `await`
26-
--> $DIR/incorrect-syntax-suggestions.rs:25:13
26+
--> $DIR/incorrect-syntax-suggestions.rs:24:13
2727
|
2828
LL | let _ = await(bar());
2929
| ^^^^^^^^^^^^ help: `await` is a postfix operation: `(bar()).await`
3030

3131
error: incorrect use of `await`
32-
--> $DIR/incorrect-syntax-suggestions.rs:29:13
32+
--> $DIR/incorrect-syntax-suggestions.rs:28:13
3333
|
3434
LL | let _ = await { bar() }?;
3535
| ^^^^^^^^^^^^^^^ help: `await` is a postfix operation: `{ bar() }.await`
3636

3737
error: incorrect use of `await`
38-
--> $DIR/incorrect-syntax-suggestions.rs:33:14
38+
--> $DIR/incorrect-syntax-suggestions.rs:32:14
3939
|
4040
LL | let _ = (await bar())?;
4141
| ^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await`
4242

4343
error: incorrect use of `await`
44-
--> $DIR/incorrect-syntax-suggestions.rs:37:24
44+
--> $DIR/incorrect-syntax-suggestions.rs:36:24
4545
|
4646
LL | let _ = bar().await();
4747
| ^^ help: `await` is not a method call, remove the parentheses
4848

4949
error: incorrect use of `await`
50-
--> $DIR/incorrect-syntax-suggestions.rs:41:24
50+
--> $DIR/incorrect-syntax-suggestions.rs:40:24
5151
|
5252
LL | let _ = bar().await()?;
5353
| ^^ help: `await` is not a method call, remove the parentheses
5454

5555
error: incorrect use of `await`
56-
--> $DIR/incorrect-syntax-suggestions.rs:53:13
56+
--> $DIR/incorrect-syntax-suggestions.rs:52:13
5757
|
5858
LL | let _ = await bar();
5959
| ^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await`
6060

6161
error: incorrect use of `await`
62-
--> $DIR/incorrect-syntax-suggestions.rs:58:13
62+
--> $DIR/incorrect-syntax-suggestions.rs:57:13
6363
|
6464
LL | let _ = await? bar();
6565
| ^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await?`
6666

6767
error: incorrect use of `await`
68-
--> $DIR/incorrect-syntax-suggestions.rs:63:13
68+
--> $DIR/incorrect-syntax-suggestions.rs:62:13
6969
|
7070
LL | let _ = await bar()?;
7171
| ^^^^^^^^^^^^ help: `await` is a postfix operation: `bar()?.await`
7272

7373
error: incorrect use of `await`
74-
--> $DIR/incorrect-syntax-suggestions.rs:69:14
74+
--> $DIR/incorrect-syntax-suggestions.rs:66:14
7575
|
7676
LL | let _ = (await bar())?;
7777
| ^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await`
7878

7979
error: incorrect use of `await`
80-
--> $DIR/incorrect-syntax-suggestions.rs:74:24
80+
--> $DIR/incorrect-syntax-suggestions.rs:71:24
8181
|
8282
LL | let _ = bar().await();
8383
| ^^ help: `await` is not a method call, remove the parentheses
8484

8585
error: incorrect use of `await`
86-
--> $DIR/incorrect-syntax-suggestions.rs:79:24
86+
--> $DIR/incorrect-syntax-suggestions.rs:76:24
8787
|
8888
LL | let _ = bar().await()?;
8989
| ^^ help: `await` is not a method call, remove the parentheses
9090

9191
error: incorrect use of `await`
92-
--> $DIR/incorrect-syntax-suggestions.rs:107:13
92+
--> $DIR/incorrect-syntax-suggestions.rs:104:13
9393
|
9494
LL | let _ = await!(bar());
9595
| ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await`
9696

9797
error: incorrect use of `await`
98-
--> $DIR/incorrect-syntax-suggestions.rs:111:13
98+
--> $DIR/incorrect-syntax-suggestions.rs:108:13
9999
|
100100
LL | let _ = await!(bar())?;
101101
| ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await`
102102

103103
error: incorrect use of `await`
104-
--> $DIR/incorrect-syntax-suggestions.rs:116:17
104+
--> $DIR/incorrect-syntax-suggestions.rs:113:17
105105
|
106106
LL | let _ = await!(bar())?;
107107
| ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await`
108108

109109
error: incorrect use of `await`
110-
--> $DIR/incorrect-syntax-suggestions.rs:124:17
110+
--> $DIR/incorrect-syntax-suggestions.rs:121:17
111111
|
112112
LL | let _ = await!(bar())?;
113113
| ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await`
114114

115115
error: expected expression, found `=>`
116-
--> $DIR/incorrect-syntax-suggestions.rs:132:25
116+
--> $DIR/incorrect-syntax-suggestions.rs:129:25
117117
|
118118
LL | match await { await => () }
119119
| ----- ^^ expected expression
120120
| |
121121
| while parsing this incorrect await expression
122122

123123
error: incorrect use of `await`
124-
--> $DIR/incorrect-syntax-suggestions.rs:132:11
124+
--> $DIR/incorrect-syntax-suggestions.rs:129:11
125125
|
126126
LL | match await { await => () }
127127
| ^^^^^^^^^^^^^^^^^^^^^ help: `await` is a postfix operation: `{ await => () }.await`
128128

129129
error: expected one of `.`, `?`, `{`, or an operator, found `}`
130-
--> $DIR/incorrect-syntax-suggestions.rs:135:1
130+
--> $DIR/incorrect-syntax-suggestions.rs:132:1
131131
|
132132
LL | match await { await => () }
133133
| ----- - expected one of `.`, `?`, `{`, or an operator
@@ -138,124 +138,93 @@ LL | }
138138
| ^ unexpected token
139139

140140
error[E0728]: `await` is only allowed inside `async` functions and blocks
141-
--> $DIR/incorrect-syntax-suggestions.rs:53:13
141+
--> $DIR/incorrect-syntax-suggestions.rs:52:13
142142
|
143143
LL | fn foo9() -> Result<(), ()> {
144144
| ---- this is not `async`
145145
LL | let _ = await bar();
146146
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
147147

148148
error[E0728]: `await` is only allowed inside `async` functions and blocks
149-
--> $DIR/incorrect-syntax-suggestions.rs:58:13
149+
--> $DIR/incorrect-syntax-suggestions.rs:57:13
150150
|
151151
LL | fn foo10() -> Result<(), ()> {
152152
| ----- this is not `async`
153153
LL | let _ = await? bar();
154154
| ^^^^^^^^^^^^ only allowed inside `async` functions and blocks
155155

156156
error[E0728]: `await` is only allowed inside `async` functions and blocks
157-
--> $DIR/incorrect-syntax-suggestions.rs:63:13
158-
|
159-
LL | fn foo11() -> Result<(), ()> {
160-
| ----- this is not `async`
161-
LL | let _ = await bar()?;
162-
| ^^^^^^^^^^^^ only allowed inside `async` functions and blocks
163-
164-
error[E0728]: `await` is only allowed inside `async` functions and blocks
165-
--> $DIR/incorrect-syntax-suggestions.rs:69:14
157+
--> $DIR/incorrect-syntax-suggestions.rs:66:14
166158
|
167159
LL | fn foo12() -> Result<(), ()> {
168160
| ----- this is not `async`
169161
LL | let _ = (await bar())?;
170162
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
171163

172164
error[E0728]: `await` is only allowed inside `async` functions and blocks
173-
--> $DIR/incorrect-syntax-suggestions.rs:74:13
165+
--> $DIR/incorrect-syntax-suggestions.rs:71:13
174166
|
175167
LL | fn foo13() -> Result<(), ()> {
176168
| ----- this is not `async`
177169
LL | let _ = bar().await();
178170
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
179171

180172
error[E0728]: `await` is only allowed inside `async` functions and blocks
181-
--> $DIR/incorrect-syntax-suggestions.rs:79:13
173+
--> $DIR/incorrect-syntax-suggestions.rs:76:13
182174
|
183175
LL | fn foo14() -> Result<(), ()> {
184176
| ----- this is not `async`
185177
LL | let _ = bar().await()?;
186178
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
187179

188180
error[E0728]: `await` is only allowed inside `async` functions and blocks
189-
--> $DIR/incorrect-syntax-suggestions.rs:84:13
181+
--> $DIR/incorrect-syntax-suggestions.rs:81:13
190182
|
191183
LL | fn foo15() -> Result<(), ()> {
192184
| ----- this is not `async`
193185
LL | let _ = bar().await;
194186
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
195187

196188
error[E0728]: `await` is only allowed inside `async` functions and blocks
197-
--> $DIR/incorrect-syntax-suggestions.rs:88:13
189+
--> $DIR/incorrect-syntax-suggestions.rs:85:13
198190
|
199191
LL | fn foo16() -> Result<(), ()> {
200192
| ----- this is not `async`
201193
LL | let _ = bar().await?;
202194
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
203195

204196
error[E0728]: `await` is only allowed inside `async` functions and blocks
205-
--> $DIR/incorrect-syntax-suggestions.rs:93:17
197+
--> $DIR/incorrect-syntax-suggestions.rs:90:17
206198
|
207199
LL | fn foo() -> Result<(), ()> {
208200
| --- this is not `async`
209201
LL | let _ = bar().await?;
210202
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
211203

212204
error[E0728]: `await` is only allowed inside `async` functions and blocks
213-
--> $DIR/incorrect-syntax-suggestions.rs:100:17
205+
--> $DIR/incorrect-syntax-suggestions.rs:97:17
214206
|
215207
LL | let foo = || {
216208
| -- this is not `async`
217209
LL | let _ = bar().await?;
218210
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
219211

220212
error[E0728]: `await` is only allowed inside `async` functions and blocks
221-
--> $DIR/incorrect-syntax-suggestions.rs:116:17
213+
--> $DIR/incorrect-syntax-suggestions.rs:113:17
222214
|
223215
LL | fn foo() -> Result<(), ()> {
224216
| --- this is not `async`
225217
LL | let _ = await!(bar())?;
226218
| ^^^^^^^^^^^^^ only allowed inside `async` functions and blocks
227219

228220
error[E0728]: `await` is only allowed inside `async` functions and blocks
229-
--> $DIR/incorrect-syntax-suggestions.rs:124:17
221+
--> $DIR/incorrect-syntax-suggestions.rs:121:17
230222
|
231223
LL | let foo = || {
232224
| -- this is not `async`
233225
LL | let _ = await!(bar())?;
234226
| ^^^^^^^^^^^^^ only allowed inside `async` functions and blocks
235227

236-
error[E0277]: the `?` operator can only be applied to values that implement `Try`
237-
--> $DIR/incorrect-syntax-suggestions.rs:16:19
238-
|
239-
LL | let _ = await bar()?;
240-
| ^^^^^^ the `?` operator cannot be applied to type `impl Future`
241-
|
242-
= help: the trait `Try` is not implemented for `impl Future`
243-
= note: required by `into_result`
244-
help: consider `await`ing on the `Future`
245-
|
246-
LL | let _ = await bar().await?;
247-
| ^^^^^^
248-
249-
error[E0277]: the `?` operator can only be applied to values that implement `Try`
250-
--> $DIR/incorrect-syntax-suggestions.rs:63:19
251-
|
252-
LL | let _ = await bar()?;
253-
| ^^^^^^ the `?` operator cannot be applied to type `impl Future`
254-
|
255-
= help: the trait `Try` is not implemented for `impl Future`
256-
= note: required by `into_result`
257-
258-
error: aborting due to 36 previous errors
228+
error: aborting due to 33 previous errors
259229

260-
Some errors have detailed explanations: E0277, E0728.
261-
For more information about an error, try `rustc --explain E0277`.
230+
For more information about this error, try `rustc --explain E0728`.

0 commit comments

Comments
 (0)