Skip to content

Commit

Permalink
move sugg to derive session diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed May 9, 2023
1 parent 5e94b5f commit 4d219d0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ parse_expected_identifier = expected identifier
parse_sugg_escape_identifier = escape `{$ident_name}` to use it as an identifier
parse_sugg_remove_comma = remove this comma
parse_sugg_add_let_for_stmt = you might have meant to introduce a new binding
parse_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token}`
parse_expected_semi_found_keyword_str = expected `;`, found keyword `{$token}`
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,18 @@ pub(crate) struct SuggRemoveComma {
pub span: Span,
}

#[derive(Subdiagnostic)]
#[suggestion(
parse_sugg_add_let_for_stmt,
style = "verbose",
applicability = "maybe-incorrect",
code = "let "
)]
pub(crate) struct SuggAddMissingLetStmt {
#[primary_span]
pub span: Span,
}

#[derive(Subdiagnostic)]
pub(crate) enum ExpectedIdentifierFound {
#[label(parse_expected_identifier_found_reserved_identifier)]
Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::errors::{
IncorrectUseOfAwait, ParenthesesInForHead, ParenthesesInForHeadSugg,
PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst,
StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens,
StructLiteralNeedingParensSugg, SuggEscapeIdentifier, SuggRemoveComma,
StructLiteralNeedingParensSugg, SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma,
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead,
};
Expand All @@ -32,8 +32,8 @@ use rustc_ast::{
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{
pluralize, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed,
FatalError, Handler, IntoDiagnostic, MultiSpan, PResult,
pluralize, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticMessage,
ErrorGuaranteed, FatalError, Handler, IntoDiagnostic, MultiSpan, PResult,
};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::Spanned;
Expand Down Expand Up @@ -1019,12 +1019,8 @@ impl<'a> Parser<'a> {
match self.parse_ty() {
Ok(_) => {
if self.token == token::Eq {
err.span_suggestion_verbose(
prev_span,
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
let sugg = SuggAddMissingLetStmt { span: prev_span };
sugg.add_to_diagnostic(err);
}
}
Err(e) => {
Expand Down

0 comments on commit 4d219d0

Please sign in to comment.