Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate trivially translatable rustc_parse diagnostics #110873

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2976,7 +2976,7 @@ pub enum ItemKind {
}

impl ItemKind {
pub fn article(&self) -> &str {
pub fn article(&self) -> &'static str {
use ItemKind::*;
match self {
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
Expand All @@ -2985,7 +2985,7 @@ impl ItemKind {
}
}

pub fn descr(&self) -> &str {
pub fn descr(&self) -> &'static str {
match self {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "`use` import",
Expand Down
72 changes: 72 additions & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,75 @@ parse_const_bounds_missing_tilde = const bounds must start with `~`
.suggestion = add `~`

parse_underscore_literal_suffix = underscore literal suffix is not allowed

parse_expect_label_found_ident = expected a label, found an identifier
.suggestion = labels start with a tick

parse_inappropriate_default = {$article} {$descr} cannot be `default`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... article + descr is gonna be hard to translate. but oh well.

.label = `default` because of this
.note = only associated `fn`, `const`, and `type` items can be `default`

parse_recover_import_as_use = expected item, found {$token_name}
.suggestion = items are imported using the `use` keyword

parse_single_colon_import_path = expected `::`, found `:`
.suggestion = use double colon
.note = import paths are delimited using `::`

parse_bad_item_kind = {$descr} is not supported in {$ctx}
.help = consider moving the {$descr} out to a nearby module scope

parse_single_colon_struct_type = found single colon in a struct field type path
.suggestion = write a path separator here

parse_equals_struct_default = default values on `struct` fields aren't supported
.suggestion = remove this unsupported default value

parse_macro_rules_missing_bang = expected `!` after `macro_rules`
.suggestion = add a `!`

parse_macro_name_remove_bang = macro names aren't followed by a `!`
.suggestion = remove the `!`

parse_macro_rules_visibility = can't qualify macro_rules invocation with `{$vis}`
.suggestion = try exporting the macro

parse_macro_invocation_visibility = can't qualify macro invocation with `pub`
.suggestion = remove the visibility
.help = try adjusting the macro to put `{$vis}` inside the invocation

parse_nested_adt = `{$kw_str}` definition cannot be nested inside `{$keyword}`
.suggestion = consider creating a new `{$kw_str}` definition instead of nesting

parse_function_body_equals_expr = function body cannot be `= expression;`
.suggestion = surround the expression with `{"{"}` and `{"}"}` instead of `=` and `;`

parse_box_not_pat = expected pattern, found {$descr}
.note = `box` is a reserved keyword
.suggestion = escape `box` to use it as an identifier

parse_unmatched_angle = unmatched angle {$plural ->
[true] brackets
*[false] bracket
}
.suggestion = remove extra angle {$plural ->
[true] brackets
*[false] bracket
}

parse_missing_plus_in_bounds = expected `+` between lifetime and {$sym}
.suggestion = add `+`

parse_incorrect_braces_trait_bounds = incorrect braces around trait bounds
.suggestion = remove the parentheses

parse_kw_bad_case = keyword `{$kw}` is written in the wrong case
.suggestion = write it in the correct case

parse_meta_bad_delim = wrong meta list delimiters
parse_cfg_attr_bad_delim = wrong `cfg_attr` delimiters
parse_meta_bad_delim_suggestion = the delimiters should be `(` and `)`

parse_malformed_cfg_attr = malformed `cfg_attr` attribute input
.suggestion = missing condition and attribute
.note = for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
224 changes: 224 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2421,3 +2421,227 @@ pub(crate) struct UnderscoreLiteralSuffix {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_expect_label_found_ident)]
pub(crate) struct ExpectedLabelFoundIdent {
#[primary_span]
pub span: Span,
#[suggestion(code = "'", applicability = "machine-applicable", style = "short")]
pub start: Span,
}

#[derive(Diagnostic)]
#[diag(parse_inappropriate_default)]
#[note]
pub(crate) struct InappropriateDefault {
#[primary_span]
#[label]
pub span: Span,
pub article: &'static str,
pub descr: &'static str,
}

#[derive(Diagnostic)]
#[diag(parse_recover_import_as_use)]
pub(crate) struct RecoverImportAsUse {
#[primary_span]
#[suggestion(code = "use", applicability = "machine-applicable", style = "short")]
pub span: Span,
pub token_name: String,
}

#[derive(Diagnostic)]
#[diag(parse_single_colon_import_path)]
#[note]
pub(crate) struct SingleColonImportPath {
#[primary_span]
#[suggestion(code = "::", applicability = "machine-applicable", style = "short")]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_bad_item_kind)]
#[help]
pub(crate) struct BadItemKind {
#[primary_span]
pub span: Span,
pub descr: &'static str,
pub ctx: &'static str,
}

#[derive(Diagnostic)]
#[diag(parse_single_colon_struct_type)]
pub(crate) struct SingleColonStructType {
#[primary_span]
#[suggestion(code = "::", applicability = "maybe-incorrect", style = "verbose")]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_equals_struct_default)]
pub(crate) struct EqualsStructDefault {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_macro_rules_missing_bang)]
pub(crate) struct MacroRulesMissingBang {
#[primary_span]
pub span: Span,
#[suggestion(code = "!", applicability = "machine-applicable", style = "verbose")]
pub hi: Span,
}

#[derive(Diagnostic)]
#[diag(parse_macro_name_remove_bang)]
pub(crate) struct MacroNameRemoveBang {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_macro_rules_visibility)]
pub(crate) struct MacroRulesVisibility<'a> {
#[primary_span]
#[suggestion(code = "#[macro_export]", applicability = "maybe-incorrect")]
pub span: Span,
pub vis: &'a str,
}

#[derive(Diagnostic)]
#[diag(parse_macro_invocation_visibility)]
#[help]
pub(crate) struct MacroInvocationVisibility<'a> {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
pub span: Span,
pub vis: &'a str,
}

#[derive(Diagnostic)]
#[diag(parse_nested_adt)]
pub(crate) struct NestedAdt<'a> {
#[primary_span]
pub span: Span,
#[suggestion(code = "", applicability = "maybe-incorrect")]
pub item: Span,
pub keyword: &'a str,
pub kw_str: Cow<'a, str>,
}

#[derive(Diagnostic)]
#[diag(parse_function_body_equals_expr)]
pub(crate) struct FunctionBodyEqualsExpr {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub sugg: FunctionBodyEqualsExprSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
pub(crate) struct FunctionBodyEqualsExprSugg {
#[suggestion_part(code = "{{")]
pub eq: Span,
#[suggestion_part(code = " }}")]
pub semi: Span,
}

#[derive(Diagnostic)]
#[diag(parse_box_not_pat)]
pub(crate) struct BoxNotPat {
#[primary_span]
pub span: Span,
#[note]
pub kw: Span,
#[suggestion(code = "r#", applicability = "maybe-incorrect", style = "verbose")]
pub lo: Span,
pub descr: String,
}

#[derive(Diagnostic)]
#[diag(parse_unmatched_angle)]
pub(crate) struct UnmatchedAngle {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
pub span: Span,
pub plural: bool,
}

#[derive(Diagnostic)]
#[diag(parse_missing_plus_in_bounds)]
pub(crate) struct MissingPlusBounds {
#[primary_span]
pub span: Span,
#[suggestion(code = " +", applicability = "maybe-incorrect", style = "verbose")]
pub hi: Span,
pub sym: Symbol,
}

#[derive(Diagnostic)]
#[diag(parse_incorrect_braces_trait_bounds)]
pub(crate) struct IncorrectBracesTraitBounds {
#[primary_span]
pub span: Vec<Span>,
#[subdiagnostic]
pub sugg: IncorrectBracesTraitBoundsSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
pub(crate) struct IncorrectBracesTraitBoundsSugg {
#[suggestion_part(code = " ")]
pub l: Span,
#[suggestion_part(code = "")]
pub r: Span,
}

#[derive(Diagnostic)]
#[diag(parse_kw_bad_case)]
pub(crate) struct KwBadCase<'a> {
#[primary_span]
#[suggestion(code = "{kw}", applicability = "machine-applicable")]
pub span: Span,
pub kw: &'a str,
}

#[derive(Diagnostic)]
#[diag(parse_meta_bad_delim)]
pub(crate) struct MetaBadDelim {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub sugg: MetaBadDelimSugg,
}

#[derive(Diagnostic)]
#[diag(parse_cfg_attr_bad_delim)]
pub(crate) struct CfgAttrBadDelim {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub sugg: MetaBadDelimSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(parse_meta_bad_delim_suggestion, applicability = "machine-applicable")]
pub(crate) struct MetaBadDelimSugg {
#[suggestion_part(code = "(")]
pub open: Span,
#[suggestion_part(code = ")")]
pub close: Span,
}

#[derive(Diagnostic)]
#[diag(parse_malformed_cfg_attr)]
#[note]
pub(crate) struct MalformedCfgAttr {
#[primary_span]
#[suggestion(code = "{sugg}")]
pub span: Span,
pub sugg: &'static str,
}
17 changes: 3 additions & 14 deletions compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{AttrItem, Attribute, MetaItem};
use rustc_ast_pretty::pprust;
use rustc_data_structures::sync::Lrc;
use rustc_errors::{Applicability, Diagnostic, FatalError, Level, PResult};
use rustc_errors::{Diagnostic, FatalError, Level, PResult};
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
use rustc_fluent_macro::fluent_messages;
use rustc_session::parse::ParseSess;
Expand Down Expand Up @@ -243,8 +243,7 @@ pub fn parse_cfg_attr(
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, ref tokens })
if !tokens.is_empty() =>
{
let msg = "wrong `cfg_attr` delimiters";
crate::validate_attr::check_meta_bad_delim(parse_sess, dspan, delim, msg);
crate::validate_attr::check_cfg_attr_bad_delim(parse_sess, dspan, delim);
match parse_in(parse_sess, tokens.clone(), "`cfg_attr` input", |p| p.parse_cfg_attr()) {
Ok(r) => return Some(r),
Err(mut e) => {
Expand All @@ -265,15 +264,5 @@ const CFG_ATTR_NOTE_REF: &str = "for more information, visit \
#the-cfg_attr-attribute>";

fn error_malformed_cfg_attr_missing(span: Span, parse_sess: &ParseSess) {
parse_sess
.span_diagnostic
.struct_span_err(span, "malformed `cfg_attr` attribute input")
.span_suggestion(
span,
"missing condition and attribute",
CFG_ATTR_GRAMMAR_HELP,
Applicability::HasPlaceholders,
)
.note(CFG_ATTR_NOTE_REF)
.emit();
parse_sess.emit_err(errors::MalformedCfgAttr { span, sugg: CFG_ATTR_GRAMMAR_HELP });
}
12 changes: 4 additions & 8 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3151,14 +3151,10 @@ impl<'a> Parser<'a> {
let label = format!("'{}", ident.name);
let ident = Ident { name: Symbol::intern(&label), span: ident.span };

self.struct_span_err(ident.span, "expected a label, found an identifier")
.span_suggestion(
ident.span,
"labels start with a tick",
label,
Applicability::MachineApplicable,
)
.emit();
self.sess.emit_err(errors::ExpectedLabelFoundIdent {
span: ident.span,
start: ident.span.shrink_to_lo(),
});

Label { ident }
}
Expand Down
Loading