From bd4d1cd7a2edd98a92b85997812e70aee5e740c7 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 11:22:26 -0500 Subject: [PATCH 1/8] migrate `maybe_recover_from_bad_qpath_stage_2` diagnostic --- .../locales/en-US/parser.ftl | 4 ++++ .../rustc_parse/src/parser/diagnostics.rs | 22 +++++++++++-------- 2 files changed, 17 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 c98989b23c1a5..985173277e170 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -14,3 +14,7 @@ parser-add-paren = try adding parentheses parser-forgot-paren = perhaps you forgot parentheses? parser-expect-path = expected a path + +parser-maybe-recover-from-bad-qpath-stage-2 = + missing angle brackets in associated item path + .suggestion = try: `{$ty}` diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 12302315e9044..f57338ae4dd6e 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -286,6 +286,15 @@ pub enum BadTypePlusSub { }, } +#[derive(SessionDiagnostic)] +#[error(slug = "parser-maybe-recover-from-bad-qpath-stage-2")] +struct BadQPathStage2 { + #[primary_span] + #[suggestion(applicability = "maybe-incorrect")] + span: Span, + ty: String, +} + // SnapshotParser is used to create a snapshot of the parser // without causing duplicate errors being emitted when the `Parser` // is dropped. @@ -1469,15 +1478,10 @@ impl<'a> Parser<'a> { path.span = ty_span.to(self.prev_token.span); let ty_str = self.span_to_snippet(ty_span).unwrap_or_else(|_| pprust::ty_to_string(&ty)); - self.struct_span_err(path.span, "missing angle brackets in associated item path") - .span_suggestion( - // This is a best-effort recovery. - path.span, - "try", - format!("<{}>::{}", ty_str, pprust::path_to_string(&path)), - Applicability::MaybeIncorrect, - ) - .emit(); + self.sess.emit_err(BadQPathStage2 { + span: path.span, + ty: format!("<{}>::{}", ty_str, pprust::path_to_string(&path)), + }); let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`. Ok(P(T::recovered(Some(QSelf { ty, path_span, position: 0 }), path))) From 29ed9a56e3b5538f52f45723933fd769e9411d58 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 11:45:44 -0500 Subject: [PATCH 2/8] migrate `maybe_consume_incorrect_semicolon` diagnostic --- .../locales/en-US/parser.ftl | 5 ++++ .../rustc_parse/src/parser/diagnostics.rs | 27 ++++++++++++------- 2 files changed, 23 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 985173277e170..90124b64f3cb3 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -18,3 +18,8 @@ parser-expect-path = expected a path parser-maybe-recover-from-bad-qpath-stage-2 = missing angle brackets in associated item path .suggestion = try: `{$ty}` + +parser-incorrect-semicolon = + expected item, found `;` + .suggestion = remove this semicolon + .help = {$name} declarations are not followed by a semicolon diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index f57338ae4dd6e..9bfd4098968d3 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -295,6 +295,17 @@ struct BadQPathStage2 { ty: String, } +#[derive(SessionDiagnostic)] +#[error(slug = "parser-incorrect-semicolon")] +struct IncorrectSemicolon<'a> { + #[primary_span] + #[suggestion(applicability = "machine-applicable")] + span: Span, + #[help] + opt_help: Option<()>, + name: &'a str, +} + // SnapshotParser is used to create a snapshot of the parser // without causing duplicate errors being emitted when the `Parser` // is dropped. @@ -1490,13 +1501,10 @@ impl<'a> Parser<'a> { pub fn maybe_consume_incorrect_semicolon(&mut self, items: &[P]) -> bool { if self.token.kind == TokenKind::Semi { self.bump(); - let mut err = self.struct_span_err(self.prev_token.span, "expected item, found `;`"); - err.span_suggestion_short( - self.prev_token.span, - "remove this semicolon", - String::new(), - Applicability::MachineApplicable, - ); + + let mut err = + IncorrectSemicolon { span: self.prev_token.span, opt_help: None, name: "" }; + if !items.is_empty() { let previous_item = &items[items.len() - 1]; let previous_item_kind_name = match previous_item.kind { @@ -1509,10 +1517,11 @@ impl<'a> Parser<'a> { _ => None, }; if let Some(name) = previous_item_kind_name { - err.help(&format!("{name} declarations are not followed by a semicolon")); + err.opt_help = Some(()); + err.name = name; } } - err.emit(); + self.sess.emit_err(err); true } else { false From 93a427e3ca48bdb7f107e0dd0cf48dd53fb8aea8 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 12:33:35 -0500 Subject: [PATCH 3/8] migrate `recover_from_await_method_call` diagnostic --- .../locales/en-US/parser.ftl | 4 ++++ compiler/rustc_parse/src/parser/diagnostics.rs | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/parser.ftl b/compiler/rustc_error_messages/locales/en-US/parser.ftl index 90124b64f3cb3..740ebcab0f86b 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -23,3 +23,7 @@ parser-incorrect-semicolon = expected item, found `;` .suggestion = remove this semicolon .help = {$name} declarations are not followed by a semicolon + +parser-incorrect-use-of-await = + incorrect use of `await` + .suggestion = `await` is not a method call, remove the parentheses diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 9bfd4098968d3..466b69ec33998 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -306,6 +306,14 @@ struct IncorrectSemicolon<'a> { name: &'a str, } +#[derive(SessionDiagnostic)] +#[error(slug = "parser-incorrect-use-of-await")] +struct IncorrectUseOfAwait { + #[primary_span] + #[suggestion(applicability = "machine-applicable")] + span: Span, +} + // SnapshotParser is used to create a snapshot of the parser // without causing duplicate errors being emitted when the `Parser` // is dropped. @@ -1659,14 +1667,8 @@ impl<'a> Parser<'a> { self.bump(); // ( let sp = lo.to(self.token.span); self.bump(); // ) - self.struct_span_err(sp, "incorrect use of `await`") - .span_suggestion( - sp, - "`await` is not a method call, remove the parentheses", - String::new(), - Applicability::MachineApplicable, - ) - .emit(); + + self.sess.emit_err(IncorrectUseOfAwait { span: sp }); } } From 2a0496cbc822b8e5488f6c4ba4cf9588595daf2a Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 12:56:26 -0500 Subject: [PATCH 4/8] use `suggestion_short` for incorrect semicolon diagnostic --- compiler/rustc_parse/src/parser/diagnostics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 466b69ec33998..6252182d09acb 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -299,7 +299,7 @@ struct BadQPathStage2 { #[error(slug = "parser-incorrect-semicolon")] struct IncorrectSemicolon<'a> { #[primary_span] - #[suggestion(applicability = "machine-applicable")] + #[suggestion_short(applicability = "machine-applicable")] span: Span, #[help] opt_help: Option<()>, From a06ba4548777cd4f4ac5379fbad6f2ab56ef85b1 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 14:32:07 -0500 Subject: [PATCH 5/8] migrate `error_on_incorrect_await` diagnostic --- .../locales/en-US/parser.ftl | 5 +++ .../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 740ebcab0f86b..98836bbc485b9 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 6252182d09acb..1c09f56e4e604 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. From 9ce04e378338b35d51a18c298f9f82fb4dbf920d Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 15:07:44 -0500 Subject: [PATCH 6/8] merge diagnostics about incorrect uses of `.await` --- compiler/rustc_error_messages/locales/en-US/parser.ftl | 8 ++------ compiler/rustc_parse/src/parser/diagnostics.rs | 6 +++--- 2 files changed, 5 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 98836bbc485b9..f9feba8012be6 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -26,9 +26,5 @@ 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 + .parentheses-suggestion = `await` is not a method call, remove the parentheses + .postfix-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 1c09f56e4e604..dea9681a9af78 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -310,16 +310,16 @@ struct IncorrectSemicolon<'a> { #[error(slug = "parser-incorrect-use-of-await")] struct IncorrectUseOfAwait { #[primary_span] - #[suggestion(applicability = "machine-applicable")] + #[suggestion(message = "parentheses-suggestion", applicability = "machine-applicable")] span: Span, } #[derive(SessionDiagnostic)] -#[error(slug = "parser-incorrect-await")] +#[error(slug = "parser-incorrect-use-of-await")] struct IncorrectAwait { #[primary_span] span: Span, - #[suggestion(code = "{expr}.await{question_mark}")] + #[suggestion(message = "postfix-suggestion", code = "{expr}.await{question_mark}")] sugg_span: (Span, Applicability), expr: String, question_mark: &'static str, From e1d63d1d7c19c6d341b9baed971493eb3354117c Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 16:28:05 -0500 Subject: [PATCH 7/8] migrate `check_for_for_in_in_typo` diagnostic --- .../locales/en-US/parser.ftl | 4 ++++ .../rustc_parse/src/parser/diagnostics.rs | 21 ++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/parser.ftl b/compiler/rustc_error_messages/locales/en-US/parser.ftl index f9feba8012be6..076b1b1caed72 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -28,3 +28,7 @@ parser-incorrect-use-of-await = incorrect use of `await` .parentheses-suggestion = `await` is not a method call, remove the parentheses .postfix-suggestion = `await` is a postfix operation + +parser-in-in-typo = + expected iterable, found keyword `in` + .suggestion = remove the duplicated `in` diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index dea9681a9af78..d6b981bdeefc5 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -325,6 +325,15 @@ struct IncorrectAwait { question_mark: &'static str, } +#[derive(SessionDiagnostic)] +#[error(slug = "parser-in-in-typo")] +struct InInTypo { + #[primary_span] + span: Span, + #[suggestion(applicability = "machine-applicable")] + sugg_span: Span, +} + // SnapshotParser is used to create a snapshot of the parser // without causing duplicate errors being emitted when the `Parser` // is dropped. @@ -1953,14 +1962,10 @@ impl<'a> Parser<'a> { pub(super) fn check_for_for_in_in_typo(&mut self, in_span: Span) { if self.eat_keyword(kw::In) { // a common typo: `for _ in in bar {}` - self.struct_span_err(self.prev_token.span, "expected iterable, found keyword `in`") - .span_suggestion_short( - in_span.until(self.prev_token.span), - "remove the duplicated `in`", - String::new(), - Applicability::MachineApplicable, - ) - .emit(); + self.sess.emit_err(InInTypo { + span: self.prev_token.span, + sugg_span: in_span.until(self.prev_token.span), + }); } } From 0fa70a8e2a95f0e33c4598ced446cfe3e7df1ae6 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 1 Jun 2022 07:14:33 -0500 Subject: [PATCH 8/8] rename `sp` to `span` --- compiler/rustc_parse/src/parser/diagnostics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index d6b981bdeefc5..8198938da646e 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1687,10 +1687,10 @@ impl<'a> Parser<'a> { // future.await() let lo = self.token.span; self.bump(); // ( - let sp = lo.to(self.token.span); + let span = lo.to(self.token.span); self.bump(); // ) - self.sess.emit_err(IncorrectUseOfAwait { span: sp }); + self.sess.emit_err(IncorrectUseOfAwait { span }); } }