Skip to content

Commit a06ba45

Browse files
committed
migrate error_on_incorrect_await diagnostic
1 parent 2a0496c commit a06ba45

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

compiler/rustc_error_messages/locales/en-US/parser.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ parser-incorrect-semicolon =
2727
parser-incorrect-use-of-await =
2828
incorrect use of `await`
2929
.suggestion = `await` is not a method call, remove the parentheses
30+
31+
32+
parser-incorrect-await =
33+
incorrect use of `await`
34+
.suggestion = `await` is a postfix operation

compiler/rustc_parse/src/parser/diagnostics.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,17 @@ struct IncorrectUseOfAwait {
314314
span: Span,
315315
}
316316

317+
#[derive(SessionDiagnostic)]
318+
#[error(slug = "parser-incorrect-await")]
319+
struct IncorrectAwait {
320+
#[primary_span]
321+
span: Span,
322+
#[suggestion(code = "{expr}.await{question_mark}")]
323+
sugg_span: (Span, Applicability),
324+
expr: String,
325+
question_mark: &'static str,
326+
}
327+
317328
// SnapshotParser is used to create a snapshot of the parser
318329
// without causing duplicate errors being emitted when the `Parser`
319330
// is dropped.
@@ -1643,18 +1654,20 @@ impl<'a> Parser<'a> {
16431654
}
16441655

16451656
fn error_on_incorrect_await(&self, lo: Span, hi: Span, expr: &Expr, is_question: bool) -> Span {
1646-
let expr_str =
1647-
self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(&expr));
1648-
let suggestion = format!("{}.await{}", expr_str, if is_question { "?" } else { "" });
1649-
let sp = lo.to(hi);
1650-
let app = match expr.kind {
1657+
let span = lo.to(hi);
1658+
let applicability = match expr.kind {
16511659
ExprKind::Try(_) => Applicability::MaybeIncorrect, // `await <expr>?`
16521660
_ => Applicability::MachineApplicable,
16531661
};
1654-
self.struct_span_err(sp, "incorrect use of `await`")
1655-
.span_suggestion(sp, "`await` is a postfix operation", suggestion, app)
1656-
.emit();
1657-
sp
1662+
1663+
self.sess.emit_err(IncorrectAwait {
1664+
span,
1665+
sugg_span: (span, applicability),
1666+
expr: self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(&expr)),
1667+
question_mark: if is_question { "?" } else { "" },
1668+
});
1669+
1670+
span
16581671
}
16591672

16601673
/// If encountering `future.await()`, consumes and emits an error.

0 commit comments

Comments
 (0)