summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poveda <git@pvdrz.com>2022-05-31 14:32:07 -0500
committerChristian Poveda <git@pvdrz.com>2022-05-31 14:32:07 -0500
commita06ba4548777cd4f4ac5379fbad6f2ab56ef85b1 (patch)
tree04f5cbbc669ef8bda086656fdb26846dd3d12e4c
parent2a0496cbc822b8e5488f6c4ba4cf9588595daf2a (diff)
downloadrust-a06ba4548777cd4f4ac5379fbad6f2ab56ef85b1.tar.gz
migrate `error_on_incorrect_await` diagnostic
-rw-r--r--compiler/rustc_error_messages/locales/en-US/parser.ftl5
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs31
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 <expr>?`
_ => 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.