Skip to content

Commit 377d14b

Browse files
committed
More accurate incorrect use of await suggestion
1 parent b5f94c6 commit 377d14b

File tree

5 files changed

+76
-56
lines changed

5 files changed

+76
-56
lines changed

compiler/rustc_parse/messages.ftl

+3-3
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ parse_incorrect_semicolon =
323323
.suggestion = remove this semicolon
324324
.help = {$name} declarations are not followed by a semicolon
325325
326-
parse_incorrect_use_of_await =
327-
incorrect use of `await`
326+
parse_incorrect_use_of_await = incorrect use of `await`
328327
.parentheses_suggestion = `await` is not a method call, remove the parentheses
329-
.postfix_suggestion = `await` is a postfix operation
328+
329+
parse_incorrect_use_of_await_postfix_suggestion = `await` is a postfix operation
330330
331331
parse_incorrect_visibility_restriction = incorrect visibility restriction
332332
.help = some possible visibility restrictions are:

compiler/rustc_parse/src/errors.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,26 @@ pub(crate) struct IncorrectUseOfAwait {
103103
pub span: Span,
104104
}
105105

106+
#[derive(Subdiagnostic)]
107+
#[multipart_suggestion(
108+
parse_incorrect_use_of_await_postfix_suggestion,
109+
applicability = "machine-applicable"
110+
)]
111+
pub(crate) struct AwaitSuggestion {
112+
#[suggestion_part(code = "")]
113+
pub removal: Span,
114+
#[suggestion_part(code = ".await{question_mark}")]
115+
pub dot_await: Span,
116+
pub question_mark: &'static str,
117+
}
118+
106119
#[derive(Diagnostic)]
107120
#[diag(parse_incorrect_use_of_await)]
108121
pub(crate) struct IncorrectAwait {
109122
#[primary_span]
110123
pub span: Span,
111-
#[suggestion(
112-
parse_postfix_suggestion,
113-
style = "verbose",
114-
code = "{expr}.await{question_mark}"
115-
)]
116-
pub sugg_span: (Span, Applicability),
117-
pub expr: String,
118-
pub question_mark: &'static str,
124+
#[subdiagnostic]
125+
pub suggestion: AwaitSuggestion,
119126
}
120127

121128
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/diagnostics.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use super::{
33
BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, TokenType,
44
};
55
use crate::errors::{
6-
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, BadQPathStage2,
7-
BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained,
6+
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, AwaitSuggestion,
7+
BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained,
88
ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces,
99
ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType,
1010
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
@@ -1959,18 +1959,14 @@ impl<'a> Parser<'a> {
19591959
is_question: bool,
19601960
) -> (Span, ErrorGuaranteed) {
19611961
let span = lo.to(hi);
1962-
let applicability = match expr.kind {
1963-
ExprKind::Try(_) => Applicability::MaybeIncorrect, // `await <expr>?`
1964-
_ => Applicability::MachineApplicable,
1965-
};
1966-
19671962
let guar = self.dcx().emit_err(IncorrectAwait {
19681963
span,
1969-
sugg_span: (span, applicability),
1970-
expr: self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(expr)),
1971-
question_mark: if is_question { "?" } else { "" },
1964+
suggestion: AwaitSuggestion {
1965+
removal: lo.until(expr.span),
1966+
dot_await: expr.span.shrink_to_hi(),
1967+
question_mark: if is_question { "?" } else { "" },
1968+
},
19721969
});
1973-
19741970
(span, guar)
19751971
}
19761972

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

+48-32
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ LL | let _ = await bar();
66
|
77
help: `await` is a postfix operation
88
|
9-
LL | let _ = bar().await;
10-
| ~~~~~~~~~~~
9+
LL - let _ = await bar();
10+
LL + let _ = bar().await;
11+
|
1112

1213
error: incorrect use of `await`
1314
--> $DIR/incorrect-syntax-suggestions.rs:12:13
@@ -17,8 +18,9 @@ LL | let _ = await? bar();
1718
|
1819
help: `await` is a postfix operation
1920
|
20-
LL | let _ = bar().await?;
21-
| ~~~~~~~~~~~~
21+
LL - let _ = await? bar();
22+
LL + let _ = bar().await?;
23+
|
2224

2325
error: incorrect use of `await`
2426
--> $DIR/incorrect-syntax-suggestions.rs:16:13
@@ -28,8 +30,9 @@ LL | let _ = await bar()?;
2830
|
2931
help: `await` is a postfix operation
3032
|
31-
LL | let _ = bar()?.await;
32-
| ~~~~~~~~~~~~
33+
LL - let _ = await bar()?;
34+
LL + let _ = bar()?.await;
35+
|
3336

3437
error: incorrect use of `await`
3538
--> $DIR/incorrect-syntax-suggestions.rs:20:13
@@ -39,8 +42,9 @@ LL | let _ = await { bar() };
3942
|
4043
help: `await` is a postfix operation
4144
|
42-
LL | let _ = { bar() }.await;
43-
| ~~~~~~~~~~~~~~~
45+
LL - let _ = await { bar() };
46+
LL + let _ = { bar() }.await;
47+
|
4448

4549
error: incorrect use of `await`
4650
--> $DIR/incorrect-syntax-suggestions.rs:24:13
@@ -50,8 +54,9 @@ LL | let _ = await(bar());
5054
|
5155
help: `await` is a postfix operation
5256
|
53-
LL | let _ = (bar()).await;
54-
| ~~~~~~~~~~~~~
57+
LL - let _ = await(bar());
58+
LL + let _ = (bar()).await;
59+
|
5560

5661
error: incorrect use of `await`
5762
--> $DIR/incorrect-syntax-suggestions.rs:28:13
@@ -61,8 +66,9 @@ LL | let _ = await { bar() }?;
6166
|
6267
help: `await` is a postfix operation
6368
|
64-
LL | let _ = { bar() }.await?;
65-
| ~~~~~~~~~~~~~~~
69+
LL - let _ = await { bar() }?;
70+
LL + let _ = { bar() }.await?;
71+
|
6672

6773
error: incorrect use of `await`
6874
--> $DIR/incorrect-syntax-suggestions.rs:32:14
@@ -72,8 +78,9 @@ LL | let _ = (await bar())?;
7278
|
7379
help: `await` is a postfix operation
7480
|
75-
LL | let _ = (bar().await)?;
76-
| ~~~~~~~~~~~
81+
LL - let _ = (await bar())?;
82+
LL + let _ = (bar().await)?;
83+
|
7784

7885
error: incorrect use of `await`
7986
--> $DIR/incorrect-syntax-suggestions.rs:36:24
@@ -107,8 +114,9 @@ LL | let _ = await bar();
107114
|
108115
help: `await` is a postfix operation
109116
|
110-
LL | let _ = bar().await;
111-
| ~~~~~~~~~~~
117+
LL - let _ = await bar();
118+
LL + let _ = bar().await;
119+
|
112120

113121
error: incorrect use of `await`
114122
--> $DIR/incorrect-syntax-suggestions.rs:56:13
@@ -118,8 +126,9 @@ LL | let _ = await? bar();
118126
|
119127
help: `await` is a postfix operation
120128
|
121-
LL | let _ = bar().await?;
122-
| ~~~~~~~~~~~~
129+
LL - let _ = await? bar();
130+
LL + let _ = bar().await?;
131+
|
123132

124133
error: incorrect use of `await`
125134
--> $DIR/incorrect-syntax-suggestions.rs:60:13
@@ -129,8 +138,9 @@ LL | let _ = await bar()?;
129138
|
130139
help: `await` is a postfix operation
131140
|
132-
LL | let _ = bar()?.await;
133-
| ~~~~~~~~~~~~
141+
LL - let _ = await bar()?;
142+
LL + let _ = bar()?.await;
143+
|
134144

135145
error: incorrect use of `await`
136146
--> $DIR/incorrect-syntax-suggestions.rs:64:14
@@ -140,8 +150,9 @@ LL | let _ = (await bar())?;
140150
|
141151
help: `await` is a postfix operation
142152
|
143-
LL | let _ = (bar().await)?;
144-
| ~~~~~~~~~~~
153+
LL - let _ = (await bar())?;
154+
LL + let _ = (bar().await)?;
155+
|
145156

146157
error: incorrect use of `await`
147158
--> $DIR/incorrect-syntax-suggestions.rs:68:24
@@ -175,8 +186,9 @@ LL | let _ = await!(bar());
175186
|
176187
help: `await` is a postfix operation
177188
|
178-
LL | let _ = bar().await;
179-
| ~~~~~~~~~~~
189+
LL - let _ = await!(bar());
190+
LL + let _ = bar().await);
191+
|
180192

181193
error: incorrect use of `await`
182194
--> $DIR/incorrect-syntax-suggestions.rs:105:13
@@ -186,8 +198,9 @@ LL | let _ = await!(bar())?;
186198
|
187199
help: `await` is a postfix operation
188200
|
189-
LL | let _ = bar().await?;
190-
| ~~~~~~~~~~~
201+
LL - let _ = await!(bar())?;
202+
LL + let _ = bar().await)?;
203+
|
191204

192205
error: incorrect use of `await`
193206
--> $DIR/incorrect-syntax-suggestions.rs:110:17
@@ -197,8 +210,9 @@ LL | let _ = await!(bar())?;
197210
|
198211
help: `await` is a postfix operation
199212
|
200-
LL | let _ = bar().await?;
201-
| ~~~~~~~~~~~
213+
LL - let _ = await!(bar())?;
214+
LL + let _ = bar().await)?;
215+
|
202216

203217
error: incorrect use of `await`
204218
--> $DIR/incorrect-syntax-suggestions.rs:117:17
@@ -208,8 +222,9 @@ LL | let _ = await!(bar())?;
208222
|
209223
help: `await` is a postfix operation
210224
|
211-
LL | let _ = bar().await?;
212-
| ~~~~~~~~~~~
225+
LL - let _ = await!(bar())?;
226+
LL + let _ = bar().await)?;
227+
|
213228

214229
error: expected expression, found `=>`
215230
--> $DIR/incorrect-syntax-suggestions.rs:124:25
@@ -227,8 +242,9 @@ LL | match await { await => () }
227242
|
228243
help: `await` is a postfix operation
229244
|
230-
LL | match { await => () }.await
231-
| ~~~~~~~~~~~~~~~~~~~~~
245+
LL - match await { await => () }
246+
LL + match { await => () }.await
247+
|
232248

233249
error: expected one of `.`, `?`, `{`, or an operator, found `}`
234250
--> $DIR/incorrect-syntax-suggestions.rs:127:1

tests/ui/parser/issues/issue-113203.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ LL | await {}()
66
|
77
help: `await` is a postfix operation
88
|
9-
LL | {}.await()
10-
| ~~~~~~~~
9+
LL - await {}()
10+
LL + {}.await()
11+
|
1112

1213
error: aborting due to 1 previous error
1314

0 commit comments

Comments
 (0)