From a06ba4548777cd4f4ac5379fbad6f2ab56ef85b1 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 14:32:07 -0500 Subject: migrate `error_on_incorrect_await` diagnostic --- .../rustc_error_messages/locales/en-US/parser.ftl | 5 ++++ compiler/rustc_parse/src/parser/diagnostics.rs | 31 +++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/parser.ftl b/compiler/rustc_error_messages/locales/en-US/parser.ftl index 740ebcab0f8..98836bbc485 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -27,3 +27,8 @@ parser-incorrect-semicolon = parser-incorrect-use-of-await = incorrect use of `await` .suggestion = `await` is not a method call, remove the parentheses + + +parser-incorrect-await = + incorrect use of `await` + .suggestion = `await` is a postfix operation diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 6252182d09a..1c09f56e4e6 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -314,6 +314,17 @@ struct IncorrectUseOfAwait { span: Span, } +#[derive(SessionDiagnostic)] +#[error(slug = "parser-incorrect-await")] +struct IncorrectAwait { + #[primary_span] + span: Span, + #[suggestion(code = "{expr}.await{question_mark}")] + sugg_span: (Span, Applicability), + expr: String, + question_mark: &'static str, +} + // SnapshotParser is used to create a snapshot of the parser // without causing duplicate errors being emitted when the `Parser` // is dropped. @@ -1643,18 +1654,20 @@ impl<'a> Parser<'a> { } fn error_on_incorrect_await(&self, lo: Span, hi: Span, expr: &Expr, is_question: bool) -> Span { - let expr_str = - self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(&expr)); - let suggestion = format!("{}.await{}", expr_str, if is_question { "?" } else { "" }); - let sp = lo.to(hi); - let app = match expr.kind { + let span = lo.to(hi); + let applicability = match expr.kind { ExprKind::Try(_) => Applicability::MaybeIncorrect, // `await ?` _ => Applicability::MachineApplicable, }; - self.struct_span_err(sp, "incorrect use of `await`") - .span_suggestion(sp, "`await` is a postfix operation", suggestion, app) - .emit(); - sp + + self.sess.emit_err(IncorrectAwait { + span, + sugg_span: (span, applicability), + expr: self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(&expr)), + question_mark: if is_question { "?" } else { "" }, + }); + + span } /// If encountering `future.await()`, consumes and emits an error. -- cgit v1.2.1