Skip to content

Commit 00f9546

Browse files
committed
Migrate even more diagnostics in rustc_parse to diagnostic structs
1 parent 760c435 commit 00f9546

File tree

4 files changed

+395
-213
lines changed

4 files changed

+395
-213
lines changed

Diff for: compiler/rustc_error_messages/locales/en-US/parser.ftl

+53
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,56 @@ parser_expected_semi_found_str = expected `;`, found `{$token_str}`
316316
parser_sugg_change_this_to_semi = change this to `;`
317317
parser_sugg_add_semi = add `;` here
318318
parser_label_unexpected_token = unexpected token
319+
320+
parser_unmatched_angle_brackets = {$num_extra_brackets ->
321+
[one] unmatched angle bracket
322+
*[other] unmatched angle brackets
323+
}
324+
.suggestion = {$num_extra_brackets ->
325+
[one] remove extra angle bracket
326+
*[other] remove extra angle brackets
327+
}
328+
329+
parser_generic_parameters_without_angle_brackets = generic parameters without surrounding angle brackets
330+
.suggestion = surround the type parameters with angle brackets
331+
332+
parser_comparison_operators_cannot_be_chained = comparison operators cannot be chained
333+
.sugg_parentheses_for_function_args = or use `(...)` if you meant to specify fn arguments
334+
.sugg_split_comparison = split the comparison into two
335+
.sugg_parenthesize = parenthesize the comparison
336+
parser_sugg_turbofish_syntax = use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
337+
338+
parser_question_mark_in_type = invalid `?` in type
339+
.label = `?` is only allowed on expressions, not types
340+
.suggestion = if you meant to express that the type might not contain a value, use the `Option` wrapper type
341+
342+
parser_unexpected_parentheses_in_for_head = unexpected parentheses surrounding `for` loop head
343+
.suggestion = remove parentheses in `for` loop
344+
345+
parser_doc_comment_on_param_type = documentation comments cannot be applied to a function parameter's type
346+
.label = doc comments are not allowed here
347+
348+
parser_attribute_on_param_type = attributes cannot be applied to a function parameter's type
349+
.label = attributes are not allowed here
350+
351+
parser_pattern_method_param_without_body = patterns aren't allowed in methods without bodies
352+
.suggestion = give this argument a name or use an underscore to ignore it
353+
354+
parser_self_param_not_first = unexpected `self` parameter in function
355+
.label = must be the first parameter of an associated function
356+
357+
parser_const_generic_without_braces = expressions must be enclosed in braces to be used as const generic arguments
358+
.suggestion = enclose the `const` expression in braces
359+
360+
parser_unexpected_const_param_declaration = unexpected `const` parameter declaration
361+
.label = expected a `const` expression, not a parameter declaration
362+
.suggestion = `const` parameters must be declared for the `impl`
363+
364+
parser_unexpected_const_in_generic_param = expected lifetime, type, or constant, found keyword `const`
365+
.suggestion = the `const` keyword is only needed in the definition of the type
366+
367+
parser_async_move_order_incorrect = the order of `move` and `async` is incorrect
368+
.suggestion = try switching the order
369+
370+
parser_double_colon_in_bound = expected `:` followed by trait or lifetime
371+
.suggestion = use single colon

Diff for: compiler/rustc_parse/src/errors.rs

+211
Original file line numberDiff line numberDiff line change
@@ -1032,3 +1032,214 @@ pub(crate) struct StructLiteralBodyWithoutPathSugg {
10321032
#[suggestion_part(code = " }}")]
10331033
pub after: Span,
10341034
}
1035+
1036+
#[derive(Diagnostic)]
1037+
#[diag(parser::unmatched_angle_brackets)]
1038+
pub(crate) struct UnmatchedAngleBrackets {
1039+
#[primary_span]
1040+
#[suggestion(code = "", applicability = "machine-applicable")]
1041+
pub span: Span,
1042+
pub num_extra_brackets: usize,
1043+
}
1044+
1045+
#[derive(Diagnostic)]
1046+
#[diag(parser::generic_parameters_without_angle_brackets)]
1047+
pub(crate) struct GenericParamsWithoutAngleBrackets {
1048+
#[primary_span]
1049+
pub span: Span,
1050+
#[subdiagnostic]
1051+
pub sugg: GenericParamsWithoutAngleBracketsSugg,
1052+
}
1053+
1054+
#[derive(Subdiagnostic)]
1055+
#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")]
1056+
pub(crate) struct GenericParamsWithoutAngleBracketsSugg {
1057+
#[suggestion_part(code = "<")]
1058+
pub left: Span,
1059+
#[suggestion_part(code = ">")]
1060+
pub right: Span,
1061+
}
1062+
1063+
#[derive(Diagnostic)]
1064+
#[diag(parser::comparison_operators_cannot_be_chained)]
1065+
pub(crate) struct ComparisonOperatorsCannotBeChained {
1066+
#[primary_span]
1067+
pub span: Vec<Span>,
1068+
#[suggestion_verbose(
1069+
parser::sugg_turbofish_syntax,
1070+
code = "::",
1071+
applicability = "maybe-incorrect"
1072+
)]
1073+
pub suggest_turbofish: Option<Span>,
1074+
#[help(parser::sugg_turbofish_syntax)]
1075+
#[help(parser::sugg_parentheses_for_function_args)]
1076+
pub help_turbofish: Option<()>,
1077+
#[subdiagnostic]
1078+
pub chaining_sugg: Option<ComparisonOperatorsCannotBeChainedSugg>,
1079+
}
1080+
1081+
#[derive(Subdiagnostic)]
1082+
pub(crate) enum ComparisonOperatorsCannotBeChainedSugg {
1083+
#[suggestion_verbose(
1084+
parser::sugg_split_comparison,
1085+
code = " && {middle_term}",
1086+
applicability = "maybe-incorrect"
1087+
)]
1088+
SplitComparison {
1089+
#[primary_span]
1090+
span: Span,
1091+
middle_term: String,
1092+
},
1093+
#[multipart_suggestion(parser::sugg_parenthesize, applicability = "maybe-incorrect")]
1094+
Parenthesize {
1095+
#[suggestion_part(code = "(")]
1096+
left: Span,
1097+
#[suggestion_part(code = ")")]
1098+
right: Span,
1099+
},
1100+
}
1101+
1102+
#[derive(Diagnostic)]
1103+
#[diag(parser::question_mark_in_type)]
1104+
pub(crate) struct QuestionMarkInType {
1105+
#[primary_span]
1106+
#[label]
1107+
pub span: Span,
1108+
#[subdiagnostic]
1109+
pub sugg: QuestionMarkInTypeSugg,
1110+
}
1111+
1112+
#[derive(Subdiagnostic)]
1113+
#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")]
1114+
pub(crate) struct QuestionMarkInTypeSugg {
1115+
#[suggestion_part(code = "Option<")]
1116+
pub left: Span,
1117+
#[suggestion_part(code = ">")]
1118+
pub right: Span,
1119+
}
1120+
1121+
#[derive(Diagnostic)]
1122+
#[diag(parser::unexpected_parentheses_in_for_head)]
1123+
pub(crate) struct ParenthesesInForHead {
1124+
#[primary_span]
1125+
pub span: Vec<Span>,
1126+
#[subdiagnostic]
1127+
pub sugg: ParenthesesInForHeadSugg,
1128+
}
1129+
1130+
#[derive(Subdiagnostic)]
1131+
#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")]
1132+
pub(crate) struct ParenthesesInForHeadSugg {
1133+
#[suggestion_part(code = "")]
1134+
pub left: Span,
1135+
#[suggestion_part(code = "")]
1136+
pub right: Span,
1137+
}
1138+
1139+
#[derive(Diagnostic)]
1140+
#[diag(parser::doc_comment_on_param_type)]
1141+
pub(crate) struct DocCommentOnParamType {
1142+
#[primary_span]
1143+
#[label]
1144+
pub span: Span,
1145+
}
1146+
1147+
#[derive(Diagnostic)]
1148+
#[diag(parser::attribute_on_param_type)]
1149+
pub(crate) struct AttributeOnParamType {
1150+
#[primary_span]
1151+
#[label]
1152+
pub span: Span,
1153+
}
1154+
1155+
#[derive(Diagnostic)]
1156+
#[diag(parser::pattern_method_param_without_body, code = "E0642")]
1157+
pub(crate) struct PatternMethodParamWithoutBody {
1158+
#[primary_span]
1159+
#[suggestion(code = "_", applicability = "machine-applicable")]
1160+
pub span: Span,
1161+
}
1162+
1163+
#[derive(Diagnostic)]
1164+
#[diag(parser::self_param_not_first)]
1165+
pub(crate) struct SelfParamNotFirst {
1166+
#[primary_span]
1167+
#[label]
1168+
pub span: Span,
1169+
}
1170+
1171+
#[derive(Diagnostic)]
1172+
#[diag(parser::const_generic_without_braces)]
1173+
pub(crate) struct ConstGenericWithoutBraces {
1174+
#[primary_span]
1175+
pub span: Span,
1176+
#[subdiagnostic]
1177+
pub sugg: ConstGenericWithoutBracesSugg,
1178+
}
1179+
1180+
#[derive(Subdiagnostic)]
1181+
#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")]
1182+
pub(crate) struct ConstGenericWithoutBracesSugg {
1183+
#[suggestion_part(code = "{{ ")]
1184+
pub left: Span,
1185+
#[suggestion_part(code = " }}")]
1186+
pub right: Span,
1187+
}
1188+
1189+
#[derive(Diagnostic)]
1190+
#[diag(parser::unexpected_const_param_declaration)]
1191+
pub(crate) struct UnexpectedConstParamDeclaration {
1192+
#[primary_span]
1193+
#[label]
1194+
pub span: Span,
1195+
#[subdiagnostic]
1196+
pub sugg: Option<UnexpectedConstParamDeclarationSugg>,
1197+
}
1198+
1199+
#[derive(Subdiagnostic)]
1200+
pub(crate) enum UnexpectedConstParamDeclarationSugg {
1201+
#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")]
1202+
AddParam {
1203+
#[suggestion_part(code = "<{snippet}>")]
1204+
impl_generics: Span,
1205+
#[suggestion_part(code = "{ident}")]
1206+
incorrect_decl: Span,
1207+
snippet: String,
1208+
ident: String,
1209+
},
1210+
#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")]
1211+
AppendParam {
1212+
#[suggestion_part(code = ", {snippet}")]
1213+
impl_generics_end: Span,
1214+
#[suggestion_part(code = "{ident}")]
1215+
incorrect_decl: Span,
1216+
snippet: String,
1217+
ident: String,
1218+
},
1219+
}
1220+
1221+
#[derive(Diagnostic)]
1222+
#[diag(parser::unexpected_const_in_generic_param)]
1223+
pub(crate) struct UnexpectedConstInGenericParam {
1224+
#[primary_span]
1225+
pub span: Span,
1226+
#[suggestion_verbose(code = "", applicability = "maybe-incorrect")]
1227+
pub to_remove: Option<Span>,
1228+
}
1229+
1230+
#[derive(Diagnostic)]
1231+
#[diag(parser::async_move_order_incorrect)]
1232+
pub(crate) struct AsyncMoveOrderIncorrect {
1233+
#[primary_span]
1234+
#[suggestion_verbose(code = "async move", applicability = "maybe-incorrect")]
1235+
pub span: Span,
1236+
}
1237+
1238+
#[derive(Diagnostic)]
1239+
#[diag(parser::double_colon_in_bound)]
1240+
pub(crate) struct DoubleColonInBound {
1241+
#[primary_span]
1242+
pub span: Span,
1243+
#[suggestion(code = ": ", applicability = "machine-applicable")]
1244+
pub between: Span,
1245+
}

0 commit comments

Comments
 (0)