Skip to content

Migrate more of rustc_parse to SessionDiagnostic #101619

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

Merged
merged 16 commits into from
Sep 28, 2022
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
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3438,6 +3438,8 @@ version = "0.0.0"
dependencies = [
"annotate-snippets",
"atty",
"rustc_ast",
"rustc_ast_pretty",
"rustc_data_structures",
"rustc_error_messages",
"rustc_hir",
Expand Down
208 changes: 208 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/parser.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ parser_field_expression_with_generic = field expressions cannot have generic arg
parser_macro_invocation_with_qualified_path = macros cannot use qualified paths

parser_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
.suggestion_remove_label = consider removing the label
.suggestion_enclose_in_block = consider enclosing expression in a block

parser_require_colon_after_labeled_expression = labeled expression must be followed by `:`
.note = labels are used before loops and blocks, allowing e.g., `break 'label` to them
Expand Down Expand Up @@ -161,3 +163,209 @@ parser_use_eq_instead = unexpected `==`

parser_use_empty_block_not_semi = expected { "`{}`" }, found `;`
.suggestion = try using { "`{}`" } instead

parser_comparison_interpreted_as_generic =
`<` is interpreted as a start of generic arguments for `{$type}`, not a comparison
.label_args = interpreted as generic arguments
.label_comparison = not interpreted as comparison
.suggestion = try comparing the cast value

parser_shift_interpreted_as_generic =
`<<` is interpreted as a start of generic arguments for `{$type}`, not a shift
.label_args = interpreted as generic arguments
.label_comparison = not interpreted as shift
.suggestion = try shifting the cast value

parser_found_expr_would_be_stmt = expected expression, found `{$token}`
.label = expected expression

parser_leading_plus_not_supported = leading `+` is not supported
.label = unexpected `+`
.suggestion_remove_plus = try removing the `+`

parser_parentheses_with_struct_fields = invalid `struct` delimiters or `fn` call arguments
.suggestion_braces_for_struct = if `{$type}` is a struct, use braces as delimiters
.suggestion_no_fields_for_fn = if `{$type}` is a function, use the arguments directly

parser_labeled_loop_in_break = parentheses are required around this expression to avoid confusion with a labeled break expression

parser_sugg_wrap_expression_in_parentheses = wrap the expression in parentheses

parser_array_brackets_instead_of_braces = this is a block expression, not an array
.suggestion = to make an array, use square brackets instead of curly braces

parser_match_arm_body_without_braces = `match` arm body without braces
.label_statements = {$num_statements ->
[one] this statement is not surrounded by a body
*[other] these statements are not surrounded by a body
}
.label_arrow = while parsing the `match` arm starting here
.suggestion_add_braces = surround the {$num_statements ->
[one] statement
*[other] statements
} with a body
.suggestion_use_comma_not_semicolon = use a comma to end a `match` arm expression

parser_struct_literal_not_allowed_here = struct literals are not allowed here
.suggestion = surround the struct literal with parentheses

parser_invalid_interpolated_expression = invalid interpolated expression

parser_hexadecimal_float_literal_not_supported = hexadecimal float literal is not supported
parser_octal_float_literal_not_supported = octal float literal is not supported
parser_binary_float_literal_not_supported = binary float literal is not supported
parser_not_supported = not supported

parser_invalid_literal_suffix = suffixes on {$kind} literals are invalid
.label = invalid suffix `{$suffix}`

parser_invalid_literal_suffix_on_tuple_index = suffixes on a tuple index are invalid
.label = invalid suffix `{$suffix}`
.tuple_exception_line_1 = `{$suffix}` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
.tuple_exception_line_2 = on proc macros, you'll want to use `syn::Index::from` or `proc_macro::Literal::*_unsuffixed` for code that will desugar to tuple field access
.tuple_exception_line_3 = see issue #60210 <https://github.com./rust-lang/rust/issues/60210> for more information

parser_non_string_abi_literal = non-string ABI literal
.suggestion = specify the ABI with a string literal

parser_mismatched_closing_delimiter = mismatched closing delimiter: `{$delimiter}`
.label_unmatched = mismatched closing delimiter
.label_opening_candidate = closing delimiter possibly meant for this
.label_unclosed = unclosed delimiter

parser_incorrect_visibility_restriction = incorrect visibility restriction
.help = some possible visibility restrictions are:
`pub(crate)`: visible only on the current crate
`pub(super)`: visible only in the current module's parent
`pub(in path::to::module)`: visible only on the specified path
.suggestion = make this visible only to module `{$inner_str}` with `in`

parser_assignment_else_not_allowed = <assignment> ... else {"{"} ... {"}"} is not allowed

parser_expected_statement_after_outer_attr = expected statement after outer attribute

parser_doc_comment_does_not_document_anything = found a documentation comment that doesn't document anything
.help = doc comments must come before what they document, maybe a comment was intended with `//`?
.suggestion = missing comma here

parser_const_let_mutually_exclusive = `const` and `let` are mutually exclusive
.suggestion = remove `let`

parser_invalid_expression_in_let_else = a `{$operator}` expression cannot be directly assigned in `let...else`
parser_invalid_curly_in_let_else = right curly brace `{"}"}` before `else` in a `let...else` statement not allowed

parser_compound_assignment_expression_in_let = can't reassign to an uninitialized variable
.suggestion = initialize the variable
.help = if you meant to overwrite, remove the `let` binding

parser_suffixed_literal_in_attribute = suffixed literals are not allowed in attributes
.help = instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), use an unsuffixed version (`1`, `1.0`, etc.)

parser_invalid_meta_item = expected unsuffixed literal or identifier, found `{$token}`

parser_label_inner_attr_does_not_annotate_this = the inner attribute doesn't annotate this {$item}
parser_sugg_change_inner_attr_to_outer = to annotate the {$item}, change the attribute from inner to outer style

parser_inner_attr_not_permitted_after_outer_doc_comment = an inner attribute is not permitted following an outer doc comment
.label_attr = not permitted following an outer doc comment
.label_prev_doc_comment = previous doc comment
.label_does_not_annotate_this = {parser_label_inner_attr_does_not_annotate_this}
.sugg_change_inner_to_outer = {parser_sugg_change_inner_attr_to_outer}

parser_inner_attr_not_permitted_after_outer_attr = an inner attribute is not permitted following an outer attribute
.label_attr = not permitted following an outer attribute
.label_prev_attr = previous outer attribute
.label_does_not_annotate_this = {parser_label_inner_attr_does_not_annotate_this}
.sugg_change_inner_to_outer = {parser_sugg_change_inner_attr_to_outer}

parser_inner_attr_not_permitted = an inner attribute is not permitted in this context
.label_does_not_annotate_this = {parser_label_inner_attr_does_not_annotate_this}
.sugg_change_inner_to_outer = {parser_sugg_change_inner_attr_to_outer}

parser_inner_attr_explanation = inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
parser_outer_attr_explanation = outer attributes, like `#[test]`, annotate the item following them

parser_inner_doc_comment_not_permitted = expected outer doc comment
.note = inner doc comments like this (starting with `//!` or `/*!`) can only appear before items
.suggestion = you might have meant to write a regular comment
.label_does_not_annotate_this = the inner doc comment doesn't annotate this {$item}
.sugg_change_inner_to_outer = to annotate the {$item}, change the doc comment from inner to outer style

parser_expected_identifier_found_reserved_identifier_str = expected identifier, found reserved identifier `{$token}`
parser_expected_identifier_found_keyword_str = expected identifier, found keyword `{$token}`
parser_expected_identifier_found_reserved_keyword_str = expected identifier, found reserved keyword `{$token}`
parser_expected_identifier_found_doc_comment_str = expected identifier, found doc comment `{$token}`
parser_expected_identifier_found_str = expected identifier, found `{$token}`

parser_expected_identifier_found_reserved_identifier = expected identifier, found reserved identifier
parser_expected_identifier_found_keyword = expected identifier, found keyword
parser_expected_identifier_found_reserved_keyword = expected identifier, found reserved keyword
parser_expected_identifier_found_doc_comment = expected identifier, found doc comment
parser_expected_identifier = expected identifier

parser_sugg_escape_to_use_as_identifier = escape `{$ident_name}` to use it as an identifier

parser_sugg_remove_comma = remove this comma

parser_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token}`
parser_expected_semi_found_keyword_str = expected `;`, found keyword `{$token}`
parser_expected_semi_found_reserved_keyword_str = expected `;`, found reserved keyword `{$token}`
parser_expected_semi_found_doc_comment_str = expected `;`, found doc comment `{$token}`
parser_expected_semi_found_str = expected `;`, found `{$token}`

parser_sugg_change_this_to_semi = change this to `;`
parser_sugg_add_semi = add `;` here
parser_label_unexpected_token = unexpected token

parser_unmatched_angle_brackets = {$num_extra_brackets ->
[one] unmatched angle bracket
*[other] unmatched angle brackets
}
.suggestion = {$num_extra_brackets ->
[one] remove extra angle bracket
*[other] remove extra angle brackets
}

parser_generic_parameters_without_angle_brackets = generic parameters without surrounding angle brackets
.suggestion = surround the type parameters with angle brackets

parser_comparison_operators_cannot_be_chained = comparison operators cannot be chained
.sugg_parentheses_for_function_args = or use `(...)` if you meant to specify fn arguments
.sugg_split_comparison = split the comparison into two
.sugg_parenthesize = parenthesize the comparison
parser_sugg_turbofish_syntax = use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments

parser_question_mark_in_type = invalid `?` in type
.label = `?` is only allowed on expressions, not types
.suggestion = if you meant to express that the type might not contain a value, use the `Option` wrapper type

parser_unexpected_parentheses_in_for_head = unexpected parentheses surrounding `for` loop head
.suggestion = remove parentheses in `for` loop

parser_doc_comment_on_param_type = documentation comments cannot be applied to a function parameter's type
.label = doc comments are not allowed here

parser_attribute_on_param_type = attributes cannot be applied to a function parameter's type
.label = attributes are not allowed here

parser_pattern_method_param_without_body = patterns aren't allowed in methods without bodies
.suggestion = give this argument a name or use an underscore to ignore it

parser_self_param_not_first = unexpected `self` parameter in function
.label = must be the first parameter of an associated function

parser_const_generic_without_braces = expressions must be enclosed in braces to be used as const generic arguments
.suggestion = enclose the `const` expression in braces

parser_unexpected_const_param_declaration = unexpected `const` parameter declaration
.label = expected a `const` expression, not a parameter declaration
.suggestion = `const` parameters must be declared for the `impl`

parser_unexpected_const_in_generic_param = expected lifetime, type, or constant, found keyword `const`
.suggestion = the `const` keyword is only needed in the definition of the type

parser_async_move_order_incorrect = the order of `move` and `async` is incorrect
.suggestion = try switching the order

parser_double_colon_in_bound = expected `:` followed by trait or lifetime
.suggestion = use single colon
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/session.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has
session_crate_name_empty = crate name must not be empty

session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`

session_expr_parentheses_needed = parentheses are required to parse this as an expression
2 changes: 2 additions & 0 deletions compiler/rustc_errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ doctest = false

[dependencies]
tracing = "0.1"
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_error_messages = { path = "../rustc_error_messages" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
CodeSuggestion, DiagnosticMessage, EmissionGuarantee, Level, LintDiagnosticBuilder, MultiSpan,
SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
};
use rustc_ast as ast;
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashMap;
use rustc_error_messages::FluentValue;
use rustc_hir as hir;
Expand Down Expand Up @@ -175,6 +177,24 @@ impl IntoDiagnosticArg for hir::ConstContext {
}
}

impl IntoDiagnosticArg for ast::Path {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
}
}

impl IntoDiagnosticArg for ast::token::Token {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(pprust::token_to_string(&self))
}
}

impl IntoDiagnosticArg for ast::token::TokenKind {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
}
}

/// Trait implemented by error types. This should not be implemented manually. Instead, use
/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
#[cfg_attr(bootstrap, rustc_diagnostic_item = "AddSubdiagnostic")]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
if should_generate_set_arg(&field) {
let diag = &self.parent.diag;
let ident = field.ident.as_ref().unwrap();
// strip `r#` prefix, if present
let ident = format_ident!("{}", ident);
return quote! {
#diag.set_arg(
stringify!(#ident),
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_macros/src/diagnostics/subdiagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ impl<'a> SubdiagnosticDeriveBuilder<'a> {

let diag = &self.diag;
let ident = ast.ident.as_ref().unwrap();
// strip `r#` prefix, if present
let ident = format_ident!("{}", ident);

quote! {
#diag.set_arg(
stringify!(#ident),
Expand Down
Loading