Skip to content

Commit 8b09cf2

Browse files
Rollup merge of #163238 - chenyukang:yukang-fix-await-macro-suggestion-parens, r=khyperia
Fix incorrect use of await in parser suggestion The old suggestion of: ```diff LL - let _ = await!(bar())?; LL + let _ = bar().await)?; ``` is wrong because of the lefted `)` was unremoved, and also here we need to take care of expr precedence.
2 parents a9ddedf + 3ea2995 commit 8b09cf2

5 files changed

Lines changed: 119 additions & 14 deletions

File tree

‎compiler/rustc_parse/src/parser/diagnostics.rs‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut};
33

44
use ast::token::IdentKind;
55
use rustc_ast::token::{self, Lit, LitKind, Token, TokenKind};
6-
use rustc_ast::util::parser::AssocOp;
6+
use rustc_ast::util::parser::{AssocOp, ExprPrecedence};
77
use rustc_ast::{
88
self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode,
99
Block, BlockCheckMode, Expr, ExprKind, GenericArg, GenericArgs, Generics, Item, ItemKind,
@@ -1719,26 +1719,35 @@ impl<'a> Parser<'a> {
17191719
&mut self,
17201720
await_sp: Span,
17211721
) -> PResult<'a, Box<Expr>> {
1722-
let (hi, expr, is_question) = if self.token == token::Bang {
1722+
let (hi, expr_span, is_question) = if self.token == token::Bang {
17231723
// Handle `await!(<expr>)`.
17241724
self.recover_await_macro()?
17251725
} else {
17261726
self.recover_await_prefix(await_sp)?
17271727
};
1728-
let (sp, guar) = self.error_on_incorrect_await(await_sp, hi, &expr, is_question);
1728+
let (sp, guar) = self.error_on_incorrect_await(await_sp, hi, expr_span, is_question);
17291729
let expr = self.mk_expr_err(await_sp.to(sp), guar);
17301730
self.maybe_recover_from_bad_qpath(expr)
17311731
}
17321732

1733-
fn recover_await_macro(&mut self) -> PResult<'a, (Span, Box<Expr>, bool)> {
1733+
fn recover_await_macro(&mut self) -> PResult<'a, (Span, Span, bool)> {
17341734
self.expect(exp!(Bang))?;
17351735
self.expect(exp!(OpenParen))?;
1736+
let open = self.prev_token.span;
17361737
let expr = self.parse_expr()?;
17371738
self.expect(exp!(CloseParen))?;
1738-
Ok((self.prev_token.span, expr, false))
1739+
let close = self.prev_token.span;
1740+
// Keep parentheses when needed for `.await`, e.g. `(&mut future).await`.
1741+
// Use the delimiters to preserve any comments around the operand.
1742+
let expr_span = if expr.precedence() < ExprPrecedence::Unambiguous {
1743+
open.to(close)
1744+
} else {
1745+
open.shrink_to_hi().to(close.shrink_to_lo())
1746+
};
1747+
Ok((close, expr_span, false))
17391748
}
17401749

1741-
fn recover_await_prefix(&mut self, await_sp: Span) -> PResult<'a, (Span, Box<Expr>, bool)> {
1750+
fn recover_await_prefix(&mut self, await_sp: Span) -> PResult<'a, (Span, Span, bool)> {
17421751
let is_question = self.eat(exp!(Question)); // Handle `await? <expr>`.
17431752
let expr = if self.token == token::OpenBrace {
17441753
// Handle `await { <expr> }`.
@@ -1752,22 +1761,22 @@ impl<'a> Parser<'a> {
17521761
err.span_label(await_sp, format!("while parsing this incorrect await expression"));
17531762
err
17541763
})?;
1755-
Ok((expr.span, expr, is_question))
1764+
Ok((expr.span, expr.span, is_question))
17561765
}
17571766

17581767
fn error_on_incorrect_await(
17591768
&self,
17601769
lo: Span,
17611770
hi: Span,
1762-
expr: &Expr,
1771+
expr_span: Span,
17631772
is_question: bool,
17641773
) -> (Span, ErrorGuaranteed) {
17651774
let span = lo.to(hi);
17661775
let guar = self.dcx().emit_err(IncorrectAwait {
17671776
span,
17681777
suggestion: AwaitSuggestion {
1769-
removal: lo.until(expr.span),
1770-
dot_await: expr.span.shrink_to_hi(),
1778+
removal: lo.until(expr_span),
1779+
dot_await: expr_span.shrink_to_hi().to(hi.shrink_to_hi()),
17711780
question_mark: if is_question { "?" } else { "" },
17721781
},
17731782
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-rustfix
2+
//@ edition: 2024
3+
4+
// Replacing `await!(...)` must preserve grouping and any following `?`.
5+
6+
use std::future::ready;
7+
8+
async fn check() -> Result<(), ()> {
9+
let future = ready(1);
10+
let _: i32 = future.await;
11+
//~^ ERROR incorrect use of `await`
12+
let _: i32 = ready(1).await;
13+
//~^ ERROR incorrect use of `await`
14+
let _: i32 = ready(Ok::<i32, ()>(1)).await?;
15+
//~^ ERROR incorrect use of `await`
16+
let _: i32 = (/* keep */ &mut ready(1) /* keep */).await;
17+
//~^ ERROR incorrect use of `await`
18+
Ok(())
19+
}
20+
21+
fn main() {
22+
let _ = check();
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-rustfix
2+
//@ edition: 2024
3+
4+
// Replacing `await!(...)` must preserve grouping and any following `?`.
5+
6+
use std::future::ready;
7+
8+
async fn check() -> Result<(), ()> {
9+
let future = ready(1);
10+
let _: i32 = await!(future);
11+
//~^ ERROR incorrect use of `await`
12+
let _: i32 = await!(ready(1));
13+
//~^ ERROR incorrect use of `await`
14+
let _: i32 = await!(ready(Ok::<i32, ()>(1)))?;
15+
//~^ ERROR incorrect use of `await`
16+
let _: i32 = await!(/* keep */ &mut ready(1) /* keep */);
17+
//~^ ERROR incorrect use of `await`
18+
Ok(())
19+
}
20+
21+
fn main() {
22+
let _ = check();
23+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: incorrect use of `await`
2+
--> $DIR/await-macro-rustfix.rs:10:18
3+
|
4+
LL | let _: i32 = await!(future);
5+
| ^^^^^^^^^^^^^^
6+
|
7+
help: `await` is a postfix operation
8+
|
9+
LL - let _: i32 = await!(future);
10+
LL + let _: i32 = future.await;
11+
|
12+
13+
error: incorrect use of `await`
14+
--> $DIR/await-macro-rustfix.rs:12:18
15+
|
16+
LL | let _: i32 = await!(ready(1));
17+
| ^^^^^^^^^^^^^^^^
18+
|
19+
help: `await` is a postfix operation
20+
|
21+
LL - let _: i32 = await!(ready(1));
22+
LL + let _: i32 = ready(1).await;
23+
|
24+
25+
error: incorrect use of `await`
26+
--> $DIR/await-macro-rustfix.rs:14:18
27+
|
28+
LL | let _: i32 = await!(ready(Ok::<i32, ()>(1)))?;
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
help: `await` is a postfix operation
32+
|
33+
LL - let _: i32 = await!(ready(Ok::<i32, ()>(1)))?;
34+
LL + let _: i32 = ready(Ok::<i32, ()>(1)).await?;
35+
|
36+
37+
error: incorrect use of `await`
38+
--> $DIR/await-macro-rustfix.rs:16:18
39+
|
40+
LL | let _: i32 = await!(/* keep */ &mut ready(1) /* keep */);
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
|
43+
help: `await` is a postfix operation
44+
|
45+
LL - let _: i32 = await!(/* keep */ &mut ready(1) /* keep */);
46+
LL + let _: i32 = (/* keep */ &mut ready(1) /* keep */).await;
47+
|
48+
49+
error: aborting due to 4 previous errors
50+

‎tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ LL | let _ = await!(bar());
187187
help: `await` is a postfix operation
188188
|
189189
LL - let _ = await!(bar());
190-
LL + let _ = bar().await);
190+
LL + let _ = bar().await;
191191
|
192192

193193
error: incorrect use of `await`
@@ -199,7 +199,7 @@ LL | let _ = await!(bar())?;
199199
help: `await` is a postfix operation
200200
|
201201
LL - let _ = await!(bar())?;
202-
LL + let _ = bar().await)?;
202+
LL + let _ = bar().await?;
203203
|
204204

205205
error: incorrect use of `await`
@@ -211,7 +211,7 @@ LL | let _ = await!(bar())?;
211211
help: `await` is a postfix operation
212212
|
213213
LL - let _ = await!(bar())?;
214-
LL + let _ = bar().await)?;
214+
LL + let _ = bar().await?;
215215
|
216216

217217
error: incorrect use of `await`
@@ -223,7 +223,7 @@ LL | let _ = await!(bar())?;
223223
help: `await` is a postfix operation
224224
|
225225
LL - let _ = await!(bar())?;
226-
LL + let _ = bar().await)?;
226+
LL + let _ = bar().await?;
227227
|
228228

229229
error: expected expression, found `=>`

0 commit comments

Comments
 (0)