Skip to content

Commit 3ea2995

Browse files
committed
Fix incorrect use of await in parser suggestion
1 parent 603ca3f commit 3ea2995

5 files changed

Lines changed: 55 additions & 22 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+
}

‎tests/ui/async-await/await-keyword/await-macro-rustfix.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ run-rustfix
12
//@ edition: 2024
23

34
// Replacing `await!(...)` must preserve grouping and any following `?`.

‎tests/ui/async-await/await-keyword/await-macro-rustfix.stderr‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
error: incorrect use of `await`
2-
--> $DIR/await-macro-rustfix.rs:9:18
2+
--> $DIR/await-macro-rustfix.rs:10:18
33
|
44
LL | let _: i32 = await!(future);
55
| ^^^^^^^^^^^^^^
66
|
77
help: `await` is a postfix operation
88
|
99
LL - let _: i32 = await!(future);
10-
LL + let _: i32 = future.await);
10+
LL + let _: i32 = future.await;
1111
|
1212

1313
error: incorrect use of `await`
14-
--> $DIR/await-macro-rustfix.rs:11:18
14+
--> $DIR/await-macro-rustfix.rs:12:18
1515
|
1616
LL | let _: i32 = await!(ready(1));
1717
| ^^^^^^^^^^^^^^^^
1818
|
1919
help: `await` is a postfix operation
2020
|
2121
LL - let _: i32 = await!(ready(1));
22-
LL + let _: i32 = ready(1).await);
22+
LL + let _: i32 = ready(1).await;
2323
|
2424

2525
error: incorrect use of `await`
26-
--> $DIR/await-macro-rustfix.rs:13:18
26+
--> $DIR/await-macro-rustfix.rs:14:18
2727
|
2828
LL | let _: i32 = await!(ready(Ok::<i32, ()>(1)))?;
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030
|
3131
help: `await` is a postfix operation
3232
|
3333
LL - let _: i32 = await!(ready(Ok::<i32, ()>(1)))?;
34-
LL + let _: i32 = ready(Ok::<i32, ()>(1)).await)?;
34+
LL + let _: i32 = ready(Ok::<i32, ()>(1)).await?;
3535
|
3636

3737
error: incorrect use of `await`
38-
--> $DIR/await-macro-rustfix.rs:15:18
38+
--> $DIR/await-macro-rustfix.rs:16:18
3939
|
4040
LL | let _: i32 = await!(/* keep */ &mut ready(1) /* keep */);
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4242
|
4343
help: `await` is a postfix operation
4444
|
4545
LL - let _: i32 = await!(/* keep */ &mut ready(1) /* keep */);
46-
LL + let _: i32 = &mut ready(1).await /* keep */);
46+
LL + let _: i32 = (/* keep */ &mut ready(1) /* keep */).await;
4747
|
4848

4949
error: aborting due to 4 previous errors

‎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)