Skip to content

Commit 29f5ec3

Browse files
authoredApr 28, 2023
Rollup merge of #110873 - clubby789:migrate-rustc-parse-trivial, r=compiler-errors
Migrate trivially translatable `rustc_parse` diagnostics cc #100717 Migrate diagnostics in `rustc_parse` which are emitted in a single statement. I worked on this by expanding the lint introduced in #108760, although that isn't included here as there is much more work to be done to satisfy it
2 parents cf911ac + 1ce9d72 commit 29f5ec3

17 files changed

+443
-231
lines changed
 

‎compiler/rustc_ast/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2976,7 +2976,7 @@ pub enum ItemKind {
29762976
}
29772977

29782978
impl ItemKind {
2979-
pub fn article(&self) -> &str {
2979+
pub fn article(&self) -> &'static str {
29802980
use ItemKind::*;
29812981
match self {
29822982
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
@@ -2985,7 +2985,7 @@ impl ItemKind {
29852985
}
29862986
}
29872987

2988-
pub fn descr(&self) -> &str {
2988+
pub fn descr(&self) -> &'static str {
29892989
match self {
29902990
ItemKind::ExternCrate(..) => "extern crate",
29912991
ItemKind::Use(..) => "`use` import",

‎compiler/rustc_parse/messages.ftl

+72
Original file line numberDiff line numberDiff line change
@@ -772,3 +772,75 @@ parse_const_bounds_missing_tilde = const bounds must start with `~`
772772
.suggestion = add `~`
773773
774774
parse_underscore_literal_suffix = underscore literal suffix is not allowed
775+
776+
parse_expect_label_found_ident = expected a label, found an identifier
777+
.suggestion = labels start with a tick
778+
779+
parse_inappropriate_default = {$article} {$descr} cannot be `default`
780+
.label = `default` because of this
781+
.note = only associated `fn`, `const`, and `type` items can be `default`
782+
783+
parse_recover_import_as_use = expected item, found {$token_name}
784+
.suggestion = items are imported using the `use` keyword
785+
786+
parse_single_colon_import_path = expected `::`, found `:`
787+
.suggestion = use double colon
788+
.note = import paths are delimited using `::`
789+
790+
parse_bad_item_kind = {$descr} is not supported in {$ctx}
791+
.help = consider moving the {$descr} out to a nearby module scope
792+
793+
parse_single_colon_struct_type = found single colon in a struct field type path
794+
.suggestion = write a path separator here
795+
796+
parse_equals_struct_default = default values on `struct` fields aren't supported
797+
.suggestion = remove this unsupported default value
798+
799+
parse_macro_rules_missing_bang = expected `!` after `macro_rules`
800+
.suggestion = add a `!`
801+
802+
parse_macro_name_remove_bang = macro names aren't followed by a `!`
803+
.suggestion = remove the `!`
804+
805+
parse_macro_rules_visibility = can't qualify macro_rules invocation with `{$vis}`
806+
.suggestion = try exporting the macro
807+
808+
parse_macro_invocation_visibility = can't qualify macro invocation with `pub`
809+
.suggestion = remove the visibility
810+
.help = try adjusting the macro to put `{$vis}` inside the invocation
811+
812+
parse_nested_adt = `{$kw_str}` definition cannot be nested inside `{$keyword}`
813+
.suggestion = consider creating a new `{$kw_str}` definition instead of nesting
814+
815+
parse_function_body_equals_expr = function body cannot be `= expression;`
816+
.suggestion = surround the expression with `{"{"}` and `{"}"}` instead of `=` and `;`
817+
818+
parse_box_not_pat = expected pattern, found {$descr}
819+
.note = `box` is a reserved keyword
820+
.suggestion = escape `box` to use it as an identifier
821+
822+
parse_unmatched_angle = unmatched angle {$plural ->
823+
[true] brackets
824+
*[false] bracket
825+
}
826+
.suggestion = remove extra angle {$plural ->
827+
[true] brackets
828+
*[false] bracket
829+
}
830+
831+
parse_missing_plus_in_bounds = expected `+` between lifetime and {$sym}
832+
.suggestion = add `+`
833+
834+
parse_incorrect_braces_trait_bounds = incorrect braces around trait bounds
835+
.suggestion = remove the parentheses
836+
837+
parse_kw_bad_case = keyword `{$kw}` is written in the wrong case
838+
.suggestion = write it in the correct case
839+
840+
parse_meta_bad_delim = wrong meta list delimiters
841+
parse_cfg_attr_bad_delim = wrong `cfg_attr` delimiters
842+
parse_meta_bad_delim_suggestion = the delimiters should be `(` and `)`
843+
844+
parse_malformed_cfg_attr = malformed `cfg_attr` attribute input
845+
.suggestion = missing condition and attribute
846+
.note = for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>

‎compiler/rustc_parse/src/errors.rs

+224
Original file line numberDiff line numberDiff line change
@@ -2421,3 +2421,227 @@ pub(crate) struct UnderscoreLiteralSuffix {
24212421
#[primary_span]
24222422
pub span: Span,
24232423
}
2424+
2425+
#[derive(Diagnostic)]
2426+
#[diag(parse_expect_label_found_ident)]
2427+
pub(crate) struct ExpectedLabelFoundIdent {
2428+
#[primary_span]
2429+
pub span: Span,
2430+
#[suggestion(code = "'", applicability = "machine-applicable", style = "short")]
2431+
pub start: Span,
2432+
}
2433+
2434+
#[derive(Diagnostic)]
2435+
#[diag(parse_inappropriate_default)]
2436+
#[note]
2437+
pub(crate) struct InappropriateDefault {
2438+
#[primary_span]
2439+
#[label]
2440+
pub span: Span,
2441+
pub article: &'static str,
2442+
pub descr: &'static str,
2443+
}
2444+
2445+
#[derive(Diagnostic)]
2446+
#[diag(parse_recover_import_as_use)]
2447+
pub(crate) struct RecoverImportAsUse {
2448+
#[primary_span]
2449+
#[suggestion(code = "use", applicability = "machine-applicable", style = "short")]
2450+
pub span: Span,
2451+
pub token_name: String,
2452+
}
2453+
2454+
#[derive(Diagnostic)]
2455+
#[diag(parse_single_colon_import_path)]
2456+
#[note]
2457+
pub(crate) struct SingleColonImportPath {
2458+
#[primary_span]
2459+
#[suggestion(code = "::", applicability = "machine-applicable", style = "short")]
2460+
pub span: Span,
2461+
}
2462+
2463+
#[derive(Diagnostic)]
2464+
#[diag(parse_bad_item_kind)]
2465+
#[help]
2466+
pub(crate) struct BadItemKind {
2467+
#[primary_span]
2468+
pub span: Span,
2469+
pub descr: &'static str,
2470+
pub ctx: &'static str,
2471+
}
2472+
2473+
#[derive(Diagnostic)]
2474+
#[diag(parse_single_colon_struct_type)]
2475+
pub(crate) struct SingleColonStructType {
2476+
#[primary_span]
2477+
#[suggestion(code = "::", applicability = "maybe-incorrect", style = "verbose")]
2478+
pub span: Span,
2479+
}
2480+
2481+
#[derive(Diagnostic)]
2482+
#[diag(parse_equals_struct_default)]
2483+
pub(crate) struct EqualsStructDefault {
2484+
#[primary_span]
2485+
#[suggestion(code = "", applicability = "machine-applicable")]
2486+
pub span: Span,
2487+
}
2488+
2489+
#[derive(Diagnostic)]
2490+
#[diag(parse_macro_rules_missing_bang)]
2491+
pub(crate) struct MacroRulesMissingBang {
2492+
#[primary_span]
2493+
pub span: Span,
2494+
#[suggestion(code = "!", applicability = "machine-applicable", style = "verbose")]
2495+
pub hi: Span,
2496+
}
2497+
2498+
#[derive(Diagnostic)]
2499+
#[diag(parse_macro_name_remove_bang)]
2500+
pub(crate) struct MacroNameRemoveBang {
2501+
#[primary_span]
2502+
#[suggestion(code = "", applicability = "machine-applicable")]
2503+
pub span: Span,
2504+
}
2505+
2506+
#[derive(Diagnostic)]
2507+
#[diag(parse_macro_rules_visibility)]
2508+
pub(crate) struct MacroRulesVisibility<'a> {
2509+
#[primary_span]
2510+
#[suggestion(code = "#[macro_export]", applicability = "maybe-incorrect")]
2511+
pub span: Span,
2512+
pub vis: &'a str,
2513+
}
2514+
2515+
#[derive(Diagnostic)]
2516+
#[diag(parse_macro_invocation_visibility)]
2517+
#[help]
2518+
pub(crate) struct MacroInvocationVisibility<'a> {
2519+
#[primary_span]
2520+
#[suggestion(code = "", applicability = "machine-applicable")]
2521+
pub span: Span,
2522+
pub vis: &'a str,
2523+
}
2524+
2525+
#[derive(Diagnostic)]
2526+
#[diag(parse_nested_adt)]
2527+
pub(crate) struct NestedAdt<'a> {
2528+
#[primary_span]
2529+
pub span: Span,
2530+
#[suggestion(code = "", applicability = "maybe-incorrect")]
2531+
pub item: Span,
2532+
pub keyword: &'a str,
2533+
pub kw_str: Cow<'a, str>,
2534+
}
2535+
2536+
#[derive(Diagnostic)]
2537+
#[diag(parse_function_body_equals_expr)]
2538+
pub(crate) struct FunctionBodyEqualsExpr {
2539+
#[primary_span]
2540+
pub span: Span,
2541+
#[subdiagnostic]
2542+
pub sugg: FunctionBodyEqualsExprSugg,
2543+
}
2544+
2545+
#[derive(Subdiagnostic)]
2546+
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
2547+
pub(crate) struct FunctionBodyEqualsExprSugg {
2548+
#[suggestion_part(code = "{{")]
2549+
pub eq: Span,
2550+
#[suggestion_part(code = " }}")]
2551+
pub semi: Span,
2552+
}
2553+
2554+
#[derive(Diagnostic)]
2555+
#[diag(parse_box_not_pat)]
2556+
pub(crate) struct BoxNotPat {
2557+
#[primary_span]
2558+
pub span: Span,
2559+
#[note]
2560+
pub kw: Span,
2561+
#[suggestion(code = "r#", applicability = "maybe-incorrect", style = "verbose")]
2562+
pub lo: Span,
2563+
pub descr: String,
2564+
}
2565+
2566+
#[derive(Diagnostic)]
2567+
#[diag(parse_unmatched_angle)]
2568+
pub(crate) struct UnmatchedAngle {
2569+
#[primary_span]
2570+
#[suggestion(code = "", applicability = "machine-applicable")]
2571+
pub span: Span,
2572+
pub plural: bool,
2573+
}
2574+
2575+
#[derive(Diagnostic)]
2576+
#[diag(parse_missing_plus_in_bounds)]
2577+
pub(crate) struct MissingPlusBounds {
2578+
#[primary_span]
2579+
pub span: Span,
2580+
#[suggestion(code = " +", applicability = "maybe-incorrect", style = "verbose")]
2581+
pub hi: Span,
2582+
pub sym: Symbol,
2583+
}
2584+
2585+
#[derive(Diagnostic)]
2586+
#[diag(parse_incorrect_braces_trait_bounds)]
2587+
pub(crate) struct IncorrectBracesTraitBounds {
2588+
#[primary_span]
2589+
pub span: Vec<Span>,
2590+
#[subdiagnostic]
2591+
pub sugg: IncorrectBracesTraitBoundsSugg,
2592+
}
2593+
2594+
#[derive(Subdiagnostic)]
2595+
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
2596+
pub(crate) struct IncorrectBracesTraitBoundsSugg {
2597+
#[suggestion_part(code = " ")]
2598+
pub l: Span,
2599+
#[suggestion_part(code = "")]
2600+
pub r: Span,
2601+
}
2602+
2603+
#[derive(Diagnostic)]
2604+
#[diag(parse_kw_bad_case)]
2605+
pub(crate) struct KwBadCase<'a> {
2606+
#[primary_span]
2607+
#[suggestion(code = "{kw}", applicability = "machine-applicable")]
2608+
pub span: Span,
2609+
pub kw: &'a str,
2610+
}
2611+
2612+
#[derive(Diagnostic)]
2613+
#[diag(parse_meta_bad_delim)]
2614+
pub(crate) struct MetaBadDelim {
2615+
#[primary_span]
2616+
pub span: Span,
2617+
#[subdiagnostic]
2618+
pub sugg: MetaBadDelimSugg,
2619+
}
2620+
2621+
#[derive(Diagnostic)]
2622+
#[diag(parse_cfg_attr_bad_delim)]
2623+
pub(crate) struct CfgAttrBadDelim {
2624+
#[primary_span]
2625+
pub span: Span,
2626+
#[subdiagnostic]
2627+
pub sugg: MetaBadDelimSugg,
2628+
}
2629+
2630+
#[derive(Subdiagnostic)]
2631+
#[multipart_suggestion(parse_meta_bad_delim_suggestion, applicability = "machine-applicable")]
2632+
pub(crate) struct MetaBadDelimSugg {
2633+
#[suggestion_part(code = "(")]
2634+
pub open: Span,
2635+
#[suggestion_part(code = ")")]
2636+
pub close: Span,
2637+
}
2638+
2639+
#[derive(Diagnostic)]
2640+
#[diag(parse_malformed_cfg_attr)]
2641+
#[note]
2642+
pub(crate) struct MalformedCfgAttr {
2643+
#[primary_span]
2644+
#[suggestion(code = "{sugg}")]
2645+
pub span: Span,
2646+
pub sugg: &'static str,
2647+
}

‎compiler/rustc_parse/src/lib.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_ast::tokenstream::TokenStream;
1818
use rustc_ast::{AttrItem, Attribute, MetaItem};
1919
use rustc_ast_pretty::pprust;
2020
use rustc_data_structures::sync::Lrc;
21-
use rustc_errors::{Applicability, Diagnostic, FatalError, Level, PResult};
21+
use rustc_errors::{Diagnostic, FatalError, Level, PResult};
2222
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
2323
use rustc_fluent_macro::fluent_messages;
2424
use rustc_session::parse::ParseSess;
@@ -243,8 +243,7 @@ pub fn parse_cfg_attr(
243243
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, ref tokens })
244244
if !tokens.is_empty() =>
245245
{
246-
let msg = "wrong `cfg_attr` delimiters";
247-
crate::validate_attr::check_meta_bad_delim(parse_sess, dspan, delim, msg);
246+
crate::validate_attr::check_cfg_attr_bad_delim(parse_sess, dspan, delim);
248247
match parse_in(parse_sess, tokens.clone(), "`cfg_attr` input", |p| p.parse_cfg_attr()) {
249248
Ok(r) => return Some(r),
250249
Err(mut e) => {
@@ -265,15 +264,5 @@ const CFG_ATTR_NOTE_REF: &str = "for more information, visit \
265264
#the-cfg_attr-attribute>";
266265

267266
fn error_malformed_cfg_attr_missing(span: Span, parse_sess: &ParseSess) {
268-
parse_sess
269-
.span_diagnostic
270-
.struct_span_err(span, "malformed `cfg_attr` attribute input")
271-
.span_suggestion(
272-
span,
273-
"missing condition and attribute",
274-
CFG_ATTR_GRAMMAR_HELP,
275-
Applicability::HasPlaceholders,
276-
)
277-
.note(CFG_ATTR_NOTE_REF)
278-
.emit();
267+
parse_sess.emit_err(errors::MalformedCfgAttr { span, sugg: CFG_ATTR_GRAMMAR_HELP });
279268
}

‎compiler/rustc_parse/src/parser/expr.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -3151,14 +3151,10 @@ impl<'a> Parser<'a> {
31513151
let label = format!("'{}", ident.name);
31523152
let ident = Ident { name: Symbol::intern(&label), span: ident.span };
31533153

3154-
self.struct_span_err(ident.span, "expected a label, found an identifier")
3155-
.span_suggestion(
3156-
ident.span,
3157-
"labels start with a tick",
3158-
label,
3159-
Applicability::MachineApplicable,
3160-
)
3161-
.emit();
3154+
self.sess.emit_err(errors::ExpectedLabelFoundIdent {
3155+
span: ident.span,
3156+
start: ident.span.shrink_to_lo(),
3157+
});
31623158

31633159
Label { ident }
31643160
}

0 commit comments

Comments
 (0)
Please sign in to comment.