diff options
Diffstat (limited to 'compiler/rustc_parse')
| -rw-r--r-- | compiler/rustc_parse/src/errors.rs | 354 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/lexer/mod.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/lexer/unescape_error_reporting.rs | 19 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/attr.rs | 28 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/attr_wrapper.rs | 13 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 78 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/mod.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/pat.rs | 20 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/stmt.rs | 65 |
10 files changed, 343 insertions, 246 deletions
diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 98fee997427..9b177c5189b 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -9,7 +9,7 @@ use rustc_span::{Span, Symbol}; use crate::parser::TokenDescription; #[derive(Diagnostic)] -#[diag(parser::maybe_report_ambiguous_plus)] +#[diag(parser_maybe_report_ambiguous_plus)] pub(crate) struct AmbiguousPlus { pub sum_ty: String, #[primary_span] @@ -18,7 +18,7 @@ pub(crate) struct AmbiguousPlus { } #[derive(Diagnostic)] -#[diag(parser::maybe_recover_from_bad_type_plus, code = "E0178")] +#[diag(parser_maybe_recover_from_bad_type_plus, code = "E0178")] pub(crate) struct BadTypePlus { pub ty: String, #[primary_span] @@ -30,7 +30,7 @@ pub(crate) struct BadTypePlus { #[derive(Subdiagnostic)] pub(crate) enum BadTypePlusSub { #[suggestion( - parser::add_paren, + parser_add_paren, code = "{sum_with_parens}", applicability = "machine-applicable" )] @@ -39,12 +39,12 @@ pub(crate) enum BadTypePlusSub { #[primary_span] span: Span, }, - #[label(parser::forgot_paren)] + #[label(parser_forgot_paren)] ForgotParen { #[primary_span] span: Span, }, - #[label(parser::expect_path)] + #[label(parser_expect_path)] ExpectPath { #[primary_span] span: Span, @@ -52,7 +52,7 @@ pub(crate) enum BadTypePlusSub { } #[derive(Diagnostic)] -#[diag(parser::maybe_recover_from_bad_qpath_stage_2)] +#[diag(parser_maybe_recover_from_bad_qpath_stage_2)] pub(crate) struct BadQPathStage2 { #[primary_span] #[suggestion(code = "", applicability = "maybe-incorrect")] @@ -61,7 +61,7 @@ pub(crate) struct BadQPathStage2 { } #[derive(Diagnostic)] -#[diag(parser::incorrect_semicolon)] +#[diag(parser_incorrect_semicolon)] pub(crate) struct IncorrectSemicolon<'a> { #[primary_span] #[suggestion_short(code = "", applicability = "machine-applicable")] @@ -72,26 +72,26 @@ pub(crate) struct IncorrectSemicolon<'a> { } #[derive(Diagnostic)] -#[diag(parser::incorrect_use_of_await)] +#[diag(parser_incorrect_use_of_await)] pub(crate) struct IncorrectUseOfAwait { #[primary_span] - #[suggestion(parser::parentheses_suggestion, code = "", applicability = "machine-applicable")] + #[suggestion(parentheses_suggestion, code = "", applicability = "machine-applicable")] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::incorrect_use_of_await)] +#[diag(parser_incorrect_use_of_await)] pub(crate) struct IncorrectAwait { #[primary_span] pub span: Span, - #[suggestion(parser::postfix_suggestion, code = "{expr}.await{question_mark}")] + #[suggestion(postfix_suggestion, code = "{expr}.await{question_mark}")] pub sugg_span: (Span, Applicability), pub expr: String, pub question_mark: &'static str, } #[derive(Diagnostic)] -#[diag(parser::in_in_typo)] +#[diag(parser_in_in_typo)] pub(crate) struct InInTypo { #[primary_span] pub span: Span, @@ -100,7 +100,7 @@ pub(crate) struct InInTypo { } #[derive(Diagnostic)] -#[diag(parser::invalid_variable_declaration)] +#[diag(parser_invalid_variable_declaration)] pub(crate) struct InvalidVariableDeclaration { #[primary_span] pub span: Span, @@ -110,26 +110,22 @@ pub(crate) struct InvalidVariableDeclaration { #[derive(Subdiagnostic)] pub(crate) enum InvalidVariableDeclarationSub { - #[suggestion( - parser::switch_mut_let_order, - applicability = "maybe-incorrect", - code = "let mut" - )] + #[suggestion(parser_switch_mut_let_order, applicability = "maybe-incorrect", code = "let mut")] SwitchMutLetOrder(#[primary_span] Span), #[suggestion( - parser::missing_let_before_mut, + parser_missing_let_before_mut, applicability = "machine-applicable", code = "let mut" )] MissingLet(#[primary_span] Span), - #[suggestion(parser::use_let_not_auto, applicability = "machine-applicable", code = "let")] + #[suggestion(parser_use_let_not_auto, applicability = "machine-applicable", code = "let")] UseLetNotAuto(#[primary_span] Span), - #[suggestion(parser::use_let_not_var, applicability = "machine-applicable", code = "let")] + #[suggestion(parser_use_let_not_var, applicability = "machine-applicable", code = "let")] UseLetNotVar(#[primary_span] Span), } #[derive(Diagnostic)] -#[diag(parser::invalid_comparison_operator)] +#[diag(parser_invalid_comparison_operator)] pub(crate) struct InvalidComparisonOperator { #[primary_span] pub span: Span, @@ -140,23 +136,19 @@ pub(crate) struct InvalidComparisonOperator { #[derive(Subdiagnostic)] pub(crate) enum InvalidComparisonOperatorSub { - #[suggestion_short( - parser::use_instead, - applicability = "machine-applicable", - code = "{correct}" - )] + #[suggestion_short(use_instead, applicability = "machine-applicable", code = "{correct}")] Correctable { #[primary_span] span: Span, invalid: String, correct: String, }, - #[label(parser::spaceship_operator_invalid)] + #[label(spaceship_operator_invalid)] Spaceship(#[primary_span] Span), } #[derive(Diagnostic)] -#[diag(parser::invalid_logical_operator)] +#[diag(parser_invalid_logical_operator)] #[note] pub(crate) struct InvalidLogicalOperator { #[primary_span] @@ -169,13 +161,13 @@ pub(crate) struct InvalidLogicalOperator { #[derive(Subdiagnostic)] pub(crate) enum InvalidLogicalOperatorSub { #[suggestion_short( - parser::use_amp_amp_for_conjunction, + use_amp_amp_for_conjunction, applicability = "machine-applicable", code = "&&" )] Conjunction(#[primary_span] Span), #[suggestion_short( - parser::use_pipe_pipe_for_disjunction, + use_pipe_pipe_for_disjunction, applicability = "machine-applicable", code = "||" )] @@ -183,7 +175,7 @@ pub(crate) enum InvalidLogicalOperatorSub { } #[derive(Diagnostic)] -#[diag(parser::tilde_is_not_unary_operator)] +#[diag(parser_tilde_is_not_unary_operator)] pub(crate) struct TildeAsUnaryOperator( #[primary_span] #[suggestion_short(applicability = "machine-applicable", code = "!")] @@ -191,7 +183,7 @@ pub(crate) struct TildeAsUnaryOperator( ); #[derive(Diagnostic)] -#[diag(parser::unexpected_token_after_not)] +#[diag(parser_unexpected_token_after_not)] pub(crate) struct NotAsNegationOperator { #[primary_span] pub negated: Span, @@ -203,21 +195,21 @@ pub(crate) struct NotAsNegationOperator { #[derive(Subdiagnostic)] pub enum NotAsNegationOperatorSub { #[suggestion_short( - parser::unexpected_token_after_not_default, + parser_unexpected_token_after_not_default, applicability = "machine-applicable", code = "!" )] SuggestNotDefault(#[primary_span] Span), #[suggestion_short( - parser::unexpected_token_after_not_bitwise, + parser_unexpected_token_after_not_bitwise, applicability = "machine-applicable", code = "!" )] SuggestNotBitwise(#[primary_span] Span), #[suggestion_short( - parser::unexpected_token_after_not_logical, + parser_unexpected_token_after_not_logical, applicability = "machine-applicable", code = "!" )] @@ -225,7 +217,7 @@ pub enum NotAsNegationOperatorSub { } #[derive(Diagnostic)] -#[diag(parser::malformed_loop_label)] +#[diag(parser_malformed_loop_label)] pub(crate) struct MalformedLoopLabel { #[primary_span] #[suggestion(applicability = "machine-applicable", code = "{correct_label}")] @@ -234,7 +226,7 @@ pub(crate) struct MalformedLoopLabel { } #[derive(Diagnostic)] -#[diag(parser::lifetime_in_borrow_expression)] +#[diag(parser_lifetime_in_borrow_expression)] pub(crate) struct LifetimeInBorrowExpression { #[primary_span] pub span: Span, @@ -244,27 +236,27 @@ pub(crate) struct LifetimeInBorrowExpression { } #[derive(Diagnostic)] -#[diag(parser::field_expression_with_generic)] +#[diag(parser_field_expression_with_generic)] pub(crate) struct FieldExpressionWithGeneric(#[primary_span] pub Span); #[derive(Diagnostic)] -#[diag(parser::macro_invocation_with_qualified_path)] +#[diag(parser_macro_invocation_with_qualified_path)] pub(crate) struct MacroInvocationWithQualifiedPath(#[primary_span] pub Span); #[derive(Diagnostic)] -#[diag(parser::unexpected_token_after_label)] +#[diag(parser_unexpected_token_after_label)] pub(crate) struct UnexpectedTokenAfterLabel { #[primary_span] - #[label(parser::unexpected_token_after_label)] + #[label(parser_unexpected_token_after_label)] pub span: Span, - #[suggestion_verbose(parser::suggestion_remove_label, code = "")] + #[suggestion_verbose(suggestion_remove_label, code = "")] pub remove_label: Option<Span>, #[subdiagnostic] pub enclose_in_block: Option<UnexpectedTokenAfterLabelSugg>, } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion_enclose_in_block, applicability = "machine-applicable")] +#[multipart_suggestion(suggestion_enclose_in_block, applicability = "machine-applicable")] pub(crate) struct UnexpectedTokenAfterLabelSugg { #[suggestion_part(code = "{{ ")] pub left: Span, @@ -273,7 +265,7 @@ pub(crate) struct UnexpectedTokenAfterLabelSugg { } #[derive(Diagnostic)] -#[diag(parser::require_colon_after_labeled_expression)] +#[diag(parser_require_colon_after_labeled_expression)] #[note] pub(crate) struct RequireColonAfterLabeledExpression { #[primary_span] @@ -285,7 +277,7 @@ pub(crate) struct RequireColonAfterLabeledExpression { } #[derive(Diagnostic)] -#[diag(parser::do_catch_syntax_removed)] +#[diag(parser_do_catch_syntax_removed)] #[note] pub(crate) struct DoCatchSyntaxRemoved { #[primary_span] @@ -294,7 +286,7 @@ pub(crate) struct DoCatchSyntaxRemoved { } #[derive(Diagnostic)] -#[diag(parser::float_literal_requires_integer_part)] +#[diag(parser_float_literal_requires_integer_part)] pub(crate) struct FloatLiteralRequiresIntegerPart { #[primary_span] #[suggestion(applicability = "machine-applicable", code = "{correct}")] @@ -303,7 +295,7 @@ pub(crate) struct FloatLiteralRequiresIntegerPart { } #[derive(Diagnostic)] -#[diag(parser::invalid_int_literal_width)] +#[diag(parser_invalid_int_literal_width)] #[help] pub(crate) struct InvalidIntLiteralWidth { #[primary_span] @@ -312,7 +304,7 @@ pub(crate) struct InvalidIntLiteralWidth { } #[derive(Diagnostic)] -#[diag(parser::invalid_num_literal_base_prefix)] +#[diag(parser_invalid_num_literal_base_prefix)] #[note] pub(crate) struct InvalidNumLiteralBasePrefix { #[primary_span] @@ -322,7 +314,7 @@ pub(crate) struct InvalidNumLiteralBasePrefix { } #[derive(Diagnostic)] -#[diag(parser::invalid_num_literal_suffix)] +#[diag(parser_invalid_num_literal_suffix)] #[help] pub(crate) struct InvalidNumLiteralSuffix { #[primary_span] @@ -332,7 +324,7 @@ pub(crate) struct InvalidNumLiteralSuffix { } #[derive(Diagnostic)] -#[diag(parser::invalid_float_literal_width)] +#[diag(parser_invalid_float_literal_width)] #[help] pub(crate) struct InvalidFloatLiteralWidth { #[primary_span] @@ -341,7 +333,7 @@ pub(crate) struct InvalidFloatLiteralWidth { } #[derive(Diagnostic)] -#[diag(parser::invalid_float_literal_suffix)] +#[diag(parser_invalid_float_literal_suffix)] #[help] pub(crate) struct InvalidFloatLiteralSuffix { #[primary_span] @@ -351,14 +343,14 @@ pub(crate) struct InvalidFloatLiteralSuffix { } #[derive(Diagnostic)] -#[diag(parser::int_literal_too_large)] +#[diag(parser_int_literal_too_large)] pub(crate) struct IntLiteralTooLarge { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::missing_semicolon_before_array)] +#[diag(parser_missing_semicolon_before_array)] pub(crate) struct MissingSemicolonBeforeArray { #[primary_span] pub open_delim: Span, @@ -367,7 +359,7 @@ pub(crate) struct MissingSemicolonBeforeArray { } #[derive(Diagnostic)] -#[diag(parser::invalid_block_macro_segment)] +#[diag(parser_invalid_block_macro_segment)] pub(crate) struct InvalidBlockMacroSegment { #[primary_span] pub span: Span, @@ -376,7 +368,7 @@ pub(crate) struct InvalidBlockMacroSegment { } #[derive(Diagnostic)] -#[diag(parser::if_expression_missing_then_block)] +#[diag(parser_if_expression_missing_then_block)] pub(crate) struct IfExpressionMissingThenBlock { #[primary_span] pub if_span: Span, @@ -386,31 +378,31 @@ pub(crate) struct IfExpressionMissingThenBlock { #[derive(Subdiagnostic)] pub(crate) enum IfExpressionMissingThenBlockSub { - #[help(parser::condition_possibly_unfinished)] + #[help(condition_possibly_unfinished)] UnfinishedCondition(#[primary_span] Span), - #[help(parser::add_then_block)] + #[help(add_then_block)] AddThenBlock(#[primary_span] Span), } #[derive(Diagnostic)] -#[diag(parser::if_expression_missing_condition)] +#[diag(parser_if_expression_missing_condition)] pub(crate) struct IfExpressionMissingCondition { #[primary_span] - #[label(parser::condition_label)] + #[label(condition_label)] pub if_span: Span, - #[label(parser::block_label)] + #[label(block_label)] pub block_span: Span, } #[derive(Diagnostic)] -#[diag(parser::expected_expression_found_let)] +#[diag(parser_expected_expression_found_let)] pub(crate) struct ExpectedExpressionFoundLet { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::expected_else_block)] +#[diag(parser_expected_else_block)] pub(crate) struct ExpectedElseBlock { #[primary_span] pub first_tok_span: Span, @@ -422,15 +414,15 @@ pub(crate) struct ExpectedElseBlock { } #[derive(Diagnostic)] -#[diag(parser::outer_attribute_not_allowed_on_if_else)] +#[diag(parser_outer_attribute_not_allowed_on_if_else)] pub(crate) struct OuterAttributeNotAllowedOnIfElse { #[primary_span] pub last: Span, - #[label(parser::branch_label)] + #[label(branch_label)] pub branch_span: Span, - #[label(parser::ctx_label)] + #[label(ctx_label)] pub ctx_span: Span, pub ctx: String, @@ -439,7 +431,7 @@ pub(crate) struct OuterAttributeNotAllowedOnIfElse { } #[derive(Diagnostic)] -#[diag(parser::missing_in_in_for_loop)] +#[diag(parser_missing_in_in_for_loop)] pub(crate) struct MissingInInForLoop { #[primary_span] pub span: Span, @@ -450,14 +442,14 @@ pub(crate) struct MissingInInForLoop { #[derive(Subdiagnostic)] pub(crate) enum MissingInInForLoopSub { // Has been misleading, at least in the past (closed Issue #48492), thus maybe-incorrect - #[suggestion_short(parser::use_in_not_of, applicability = "maybe-incorrect", code = "in")] + #[suggestion_short(use_in_not_of, applicability = "maybe-incorrect", code = "in")] InNotOf(#[primary_span] Span), - #[suggestion_short(parser::add_in, applicability = "maybe-incorrect", code = " in ")] + #[suggestion_short(add_in, applicability = "maybe-incorrect", code = " in ")] AddIn(#[primary_span] Span), } #[derive(Diagnostic)] -#[diag(parser::missing_comma_after_match_arm)] +#[diag(parser_missing_comma_after_match_arm)] pub(crate) struct MissingCommaAfterMatchArm { #[primary_span] #[suggestion(applicability = "machine-applicable", code = ",")] @@ -465,7 +457,7 @@ pub(crate) struct MissingCommaAfterMatchArm { } #[derive(Diagnostic)] -#[diag(parser::catch_after_try)] +#[diag(parser_catch_after_try)] #[help] pub(crate) struct CatchAfterTry { #[primary_span] @@ -473,7 +465,7 @@ pub(crate) struct CatchAfterTry { } #[derive(Diagnostic)] -#[diag(parser::comma_after_base_struct)] +#[diag(parser_comma_after_base_struct)] #[note] pub(crate) struct CommaAfterBaseStruct { #[primary_span] @@ -483,7 +475,7 @@ pub(crate) struct CommaAfterBaseStruct { } #[derive(Diagnostic)] -#[diag(parser::eq_field_init)] +#[diag(parser_eq_field_init)] pub(crate) struct EqFieldInit { #[primary_span] pub span: Span, @@ -492,16 +484,16 @@ pub(crate) struct EqFieldInit { } #[derive(Diagnostic)] -#[diag(parser::dotdotdot)] +#[diag(parser_dotdotdot)] pub(crate) struct DotDotDot { #[primary_span] - #[suggestion(parser::suggest_exclusive_range, applicability = "maybe-incorrect", code = "..")] - #[suggestion(parser::suggest_inclusive_range, applicability = "maybe-incorrect", code = "..=")] + #[suggestion(suggest_exclusive_range, applicability = "maybe-incorrect", code = "..")] + #[suggestion(suggest_inclusive_range, applicability = "maybe-incorrect", code = "..=")] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::left_arrow_operator)] +#[diag(parser_left_arrow_operator)] pub(crate) struct LeftArrowOperator { #[primary_span] #[suggestion(applicability = "maybe-incorrect", code = "< -")] @@ -509,7 +501,7 @@ pub(crate) struct LeftArrowOperator { } #[derive(Diagnostic)] -#[diag(parser::remove_let)] +#[diag(parser_remove_let)] pub(crate) struct RemoveLet { #[primary_span] #[suggestion(applicability = "machine-applicable", code = "")] @@ -517,7 +509,7 @@ pub(crate) struct RemoveLet { } #[derive(Diagnostic)] -#[diag(parser::use_eq_instead)] +#[diag(parser_use_eq_instead)] pub(crate) struct UseEqInstead { #[primary_span] #[suggestion_short(applicability = "machine-applicable", code = "=")] @@ -525,7 +517,7 @@ pub(crate) struct UseEqInstead { } #[derive(Diagnostic)] -#[diag(parser::use_empty_block_not_semi)] +#[diag(parser_use_empty_block_not_semi)] pub(crate) struct UseEmptyBlockNotSemi { #[primary_span] #[suggestion_hidden(applicability = "machine-applicable", code = "{{}}")] @@ -533,33 +525,33 @@ pub(crate) struct UseEmptyBlockNotSemi { } #[derive(Diagnostic)] -#[diag(parser::comparison_interpreted_as_generic)] +#[diag(parser_comparison_interpreted_as_generic)] pub(crate) struct ComparisonInterpretedAsGeneric { #[primary_span] - #[label(parser::label_comparison)] + #[label(label_comparison)] pub comparison: Span, pub r#type: Path, - #[label(parser::label_args)] + #[label(label_args)] pub args: Span, #[subdiagnostic] pub suggestion: ComparisonOrShiftInterpretedAsGenericSugg, } #[derive(Diagnostic)] -#[diag(parser::shift_interpreted_as_generic)] +#[diag(parser_shift_interpreted_as_generic)] pub(crate) struct ShiftInterpretedAsGeneric { #[primary_span] - #[label(parser::label_comparison)] + #[label(label_comparison)] pub shift: Span, pub r#type: Path, - #[label(parser::label_args)] + #[label(label_args)] pub args: Span, #[subdiagnostic] pub suggestion: ComparisonOrShiftInterpretedAsGenericSugg, } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] +#[multipart_suggestion(suggestion, applicability = "machine-applicable")] pub(crate) struct ComparisonOrShiftInterpretedAsGenericSugg { #[suggestion_part(code = "(")] pub left: Span, @@ -568,7 +560,7 @@ pub(crate) struct ComparisonOrShiftInterpretedAsGenericSugg { } #[derive(Diagnostic)] -#[diag(parser::found_expr_would_be_stmt)] +#[diag(parser_found_expr_would_be_stmt)] pub(crate) struct FoundExprWouldBeStmt { #[primary_span] #[label] @@ -579,23 +571,19 @@ pub(crate) struct FoundExprWouldBeStmt { } #[derive(Diagnostic)] -#[diag(parser::leading_plus_not_supported)] +#[diag(parser_leading_plus_not_supported)] pub(crate) struct LeadingPlusNotSupported { #[primary_span] #[label] pub span: Span, - #[suggestion_verbose( - parser::suggestion_remove_plus, - code = "", - applicability = "machine-applicable" - )] + #[suggestion_verbose(suggestion_remove_plus, code = "", applicability = "machine-applicable")] pub remove_plus: Option<Span>, #[subdiagnostic] pub add_parentheses: Option<ExprParenthesesNeeded>, } #[derive(Diagnostic)] -#[diag(parser::parentheses_with_struct_fields)] +#[diag(parser_parentheses_with_struct_fields)] pub(crate) struct ParenthesesWithStructFields { #[primary_span] pub span: Span, @@ -607,7 +595,7 @@ pub(crate) struct ParenthesesWithStructFields { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion_braces_for_struct, applicability = "maybe-incorrect")] +#[multipart_suggestion(suggestion_braces_for_struct, applicability = "maybe-incorrect")] pub(crate) struct BracesForStructLiteral { #[suggestion_part(code = " {{ ")] pub first: Span, @@ -616,14 +604,14 @@ pub(crate) struct BracesForStructLiteral { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion_no_fields_for_fn, applicability = "maybe-incorrect")] +#[multipart_suggestion(suggestion_no_fields_for_fn, applicability = "maybe-incorrect")] pub(crate) struct NoFieldsForFnCall { #[suggestion_part(code = "")] pub fields: Vec<Span>, } #[derive(Diagnostic)] -#[diag(parser::labeled_loop_in_break)] +#[diag(parser_labeled_loop_in_break)] pub(crate) struct LabeledLoopInBreak { #[primary_span] pub span: Span, @@ -633,7 +621,7 @@ pub(crate) struct LabeledLoopInBreak { #[derive(Subdiagnostic)] #[multipart_suggestion( - parser::sugg_wrap_expression_in_parentheses, + parser_sugg_wrap_expression_in_parentheses, applicability = "machine-applicable" )] pub(crate) struct WrapExpressionInParentheses { @@ -644,7 +632,7 @@ pub(crate) struct WrapExpressionInParentheses { } #[derive(Diagnostic)] -#[diag(parser::array_brackets_instead_of_braces)] +#[diag(parser_array_brackets_instead_of_braces)] pub(crate) struct ArrayBracketsInsteadOfSpaces { #[primary_span] pub span: Span, @@ -653,7 +641,7 @@ pub(crate) struct ArrayBracketsInsteadOfSpaces { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "maybe-incorrect")] +#[multipart_suggestion(suggestion, applicability = "maybe-incorrect")] pub(crate) struct ArrayBracketsInsteadOfSpacesSugg { #[suggestion_part(code = "[")] pub left: Span, @@ -662,12 +650,12 @@ pub(crate) struct ArrayBracketsInsteadOfSpacesSugg { } #[derive(Diagnostic)] -#[diag(parser::match_arm_body_without_braces)] +#[diag(parser_match_arm_body_without_braces)] pub(crate) struct MatchArmBodyWithoutBraces { #[primary_span] - #[label(parser::label_statements)] + #[label(label_statements)] pub statements: Span, - #[label(parser::label_arrow)] + #[label(label_arrow)] pub arrow: Span, pub num_statements: usize, #[subdiagnostic] @@ -676,7 +664,7 @@ pub(crate) struct MatchArmBodyWithoutBraces { #[derive(Subdiagnostic)] pub(crate) enum MatchArmBodyWithoutBracesSugg { - #[multipart_suggestion(parser::suggestion_add_braces, applicability = "machine-applicable")] + #[multipart_suggestion(suggestion_add_braces, applicability = "machine-applicable")] AddBraces { #[suggestion_part(code = "{{ ")] left: Span, @@ -684,7 +672,7 @@ pub(crate) enum MatchArmBodyWithoutBracesSugg { right: Span, }, #[suggestion( - parser::suggestion_use_comma_not_semicolon, + suggestion_use_comma_not_semicolon, code = ",", applicability = "machine-applicable" )] @@ -695,7 +683,7 @@ pub(crate) enum MatchArmBodyWithoutBracesSugg { } #[derive(Diagnostic)] -#[diag(parser::struct_literal_not_allowed_here)] +#[diag(parser_struct_literal_not_allowed_here)] pub(crate) struct StructLiteralNotAllowedHere { #[primary_span] pub span: Span, @@ -704,7 +692,7 @@ pub(crate) struct StructLiteralNotAllowedHere { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] +#[multipart_suggestion(suggestion, applicability = "machine-applicable")] pub(crate) struct StructLiteralNotAllowedHereSugg { #[suggestion_part(code = "(")] pub left: Span, @@ -713,38 +701,38 @@ pub(crate) struct StructLiteralNotAllowedHereSugg { } #[derive(Diagnostic)] -#[diag(parser::invalid_interpolated_expression)] +#[diag(parser_invalid_interpolated_expression)] pub(crate) struct InvalidInterpolatedExpression { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::hexadecimal_float_literal_not_supported)] +#[diag(parser_hexadecimal_float_literal_not_supported)] pub(crate) struct HexadecimalFloatLiteralNotSupported { #[primary_span] - #[label(parser::not_supported)] + #[label(parser_not_supported)] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::octal_float_literal_not_supported)] +#[diag(parser_octal_float_literal_not_supported)] pub(crate) struct OctalFloatLiteralNotSupported { #[primary_span] - #[label(parser::not_supported)] + #[label(parser_not_supported)] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::binary_float_literal_not_supported)] +#[diag(parser_binary_float_literal_not_supported)] pub(crate) struct BinaryFloatLiteralNotSupported { #[primary_span] - #[label(parser::not_supported)] + #[label(parser_not_supported)] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::invalid_literal_suffix)] +#[diag(parser_invalid_literal_suffix)] pub(crate) struct InvalidLiteralSuffix { #[primary_span] #[label] @@ -755,20 +743,20 @@ pub(crate) struct InvalidLiteralSuffix { } #[derive(Diagnostic)] -#[diag(parser::invalid_literal_suffix_on_tuple_index)] +#[diag(parser_invalid_literal_suffix_on_tuple_index)] pub(crate) struct InvalidLiteralSuffixOnTupleIndex { #[primary_span] #[label] pub span: Span, pub suffix: Symbol, - #[help(parser::tuple_exception_line_1)] - #[help(parser::tuple_exception_line_2)] - #[help(parser::tuple_exception_line_3)] + #[help(tuple_exception_line_1)] + #[help(tuple_exception_line_2)] + #[help(tuple_exception_line_3)] pub exception: Option<()>, } #[derive(Diagnostic)] -#[diag(parser::non_string_abi_literal)] +#[diag(parser_non_string_abi_literal)] pub(crate) struct NonStringAbiLiteral { #[primary_span] #[suggestion(code = "\"C\"", applicability = "maybe-incorrect")] @@ -776,21 +764,21 @@ pub(crate) struct NonStringAbiLiteral { } #[derive(Diagnostic)] -#[diag(parser::mismatched_closing_delimiter)] +#[diag(parser_mismatched_closing_delimiter)] pub(crate) struct MismatchedClosingDelimiter { #[primary_span] pub spans: Vec<Span>, pub delimiter: String, - #[label(parser::label_unmatched)] + #[label(label_unmatched)] pub unmatched: Span, - #[label(parser::label_opening_candidate)] + #[label(label_opening_candidate)] pub opening_candidate: Option<Span>, - #[label(parser::label_unclosed)] + #[label(label_unclosed)] pub unclosed: Option<Span>, } #[derive(Diagnostic)] -#[diag(parser::incorrect_visibility_restriction, code = "E0704")] +#[diag(parser_incorrect_visibility_restriction, code = "E0704")] #[help] pub(crate) struct IncorrectVisibilityRestriction { #[primary_span] @@ -800,21 +788,21 @@ pub(crate) struct IncorrectVisibilityRestriction { } #[derive(Diagnostic)] -#[diag(parser::assignment_else_not_allowed)] +#[diag(parser_assignment_else_not_allowed)] pub(crate) struct AssignmentElseNotAllowed { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::expected_statement_after_outer_attr)] +#[diag(parser_expected_statement_after_outer_attr)] pub(crate) struct ExpectedStatementAfterOuterAttr { #[primary_span] pub span: Span, } #[derive(Diagnostic)] -#[diag(parser::doc_comment_does_not_document_anything, code = "E0585")] +#[diag(parser_doc_comment_does_not_document_anything, code = "E0585")] #[help] pub(crate) struct DocCommentDoesNotDocumentAnything { #[primary_span] @@ -824,7 +812,7 @@ pub(crate) struct DocCommentDoesNotDocumentAnything { } #[derive(Diagnostic)] -#[diag(parser::const_let_mutually_exclusive)] +#[diag(parser_const_let_mutually_exclusive)] pub(crate) struct ConstLetMutuallyExclusive { #[primary_span] #[suggestion(code = "const", applicability = "maybe-incorrect")] @@ -832,7 +820,7 @@ pub(crate) struct ConstLetMutuallyExclusive { } #[derive(Diagnostic)] -#[diag(parser::invalid_expression_in_let_else)] +#[diag(parser_invalid_expression_in_let_else)] pub(crate) struct InvalidExpressionInLetElse { #[primary_span] pub span: Span, @@ -842,7 +830,7 @@ pub(crate) struct InvalidExpressionInLetElse { } #[derive(Diagnostic)] -#[diag(parser::invalid_curly_in_let_else)] +#[diag(parser_invalid_curly_in_let_else)] pub(crate) struct InvalidCurlyInLetElse { #[primary_span] pub span: Span, @@ -851,7 +839,7 @@ pub(crate) struct InvalidCurlyInLetElse { } #[derive(Diagnostic)] -#[diag(parser::compound_assignment_expression_in_let)] +#[diag(parser_compound_assignment_expression_in_let)] #[help] pub(crate) struct CompoundAssignmentExpressionInLet { #[primary_span] @@ -860,7 +848,7 @@ pub(crate) struct CompoundAssignmentExpressionInLet { } #[derive(Diagnostic)] -#[diag(parser::suffixed_literal_in_attribute)] +#[diag(parser_suffixed_literal_in_attribute)] #[help] pub(crate) struct SuffixedLiteralInAttribute { #[primary_span] @@ -868,7 +856,7 @@ pub(crate) struct SuffixedLiteralInAttribute { } #[derive(Diagnostic)] -#[diag(parser::invalid_meta_item)] +#[diag(parser_invalid_meta_item)] pub(crate) struct InvalidMetaItem { #[primary_span] pub span: Span, @@ -877,7 +865,7 @@ pub(crate) struct InvalidMetaItem { #[derive(Subdiagnostic)] #[suggestion_verbose( - parser::sugg_escape_to_use_as_identifier, + parser_sugg_escape_to_use_as_identifier, applicability = "maybe-incorrect", code = "r#" )] @@ -888,7 +876,7 @@ pub(crate) struct SuggEscapeToUseAsIdentifier { } #[derive(Subdiagnostic)] -#[suggestion(parser::sugg_remove_comma, applicability = "machine-applicable", code = "")] +#[suggestion(parser_sugg_remove_comma, applicability = "machine-applicable", code = "")] pub(crate) struct SuggRemoveComma { #[primary_span] pub span: Span, @@ -896,15 +884,15 @@ pub(crate) struct SuggRemoveComma { #[derive(Subdiagnostic)] pub(crate) enum ExpectedIdentifierFound { - #[label(parser::expected_identifier_found_reserved_identifier)] + #[label(parser_expected_identifier_found_reserved_identifier)] ReservedIdentifier(#[primary_span] Span), - #[label(parser::expected_identifier_found_keyword)] + #[label(parser_expected_identifier_found_keyword)] Keyword(#[primary_span] Span), - #[label(parser::expected_identifier_found_reserved_keyword)] + #[label(parser_expected_identifier_found_reserved_keyword)] ReservedKeyword(#[primary_span] Span), - #[label(parser::expected_identifier_found_doc_comment)] + #[label(parser_expected_identifier_found_doc_comment)] DocComment(#[primary_span] Span), - #[label(parser::expected_identifier)] + #[label(parser_expected_identifier)] Other(#[primary_span] Span), } @@ -938,18 +926,16 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier { let mut diag = handler.struct_diagnostic(match token_descr { Some(TokenDescription::ReservedIdentifier) => { - fluent::parser::expected_identifier_found_reserved_identifier_str - } - Some(TokenDescription::Keyword) => { - fluent::parser::expected_identifier_found_keyword_str + fluent::parser_expected_identifier_found_reserved_identifier_str } + Some(TokenDescription::Keyword) => fluent::parser_expected_identifier_found_keyword_str, Some(TokenDescription::ReservedKeyword) => { - fluent::parser::expected_identifier_found_reserved_keyword_str + fluent::parser_expected_identifier_found_reserved_keyword_str } Some(TokenDescription::DocComment) => { - fluent::parser::expected_identifier_found_doc_comment_str + fluent::parser_expected_identifier_found_doc_comment_str } - None => fluent::parser::expected_identifier_found_str, + None => fluent::parser_expected_identifier_found_str, }); diag.set_span(self.span); diag.set_arg("token", self.token); @@ -985,22 +971,22 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedSemi { let mut diag = handler.struct_diagnostic(match token_descr { Some(TokenDescription::ReservedIdentifier) => { - fluent::parser::expected_semi_found_reserved_identifier_str + fluent::parser_expected_semi_found_reserved_identifier_str } - Some(TokenDescription::Keyword) => fluent::parser::expected_semi_found_keyword_str, + Some(TokenDescription::Keyword) => fluent::parser_expected_semi_found_keyword_str, Some(TokenDescription::ReservedKeyword) => { - fluent::parser::expected_semi_found_reserved_keyword_str + fluent::parser_expected_semi_found_reserved_keyword_str } Some(TokenDescription::DocComment) => { - fluent::parser::expected_semi_found_doc_comment_str + fluent::parser_expected_semi_found_doc_comment_str } - None => fluent::parser::expected_semi_found_str, + None => fluent::parser_expected_semi_found_str, }); diag.set_span(self.span); diag.set_arg("token", self.token); if let Some(unexpected_token_label) = self.unexpected_token_label { - diag.span_label(unexpected_token_label, fluent::parser::label_unexpected_token); + diag.span_label(unexpected_token_label, fluent::parser_label_unexpected_token); } self.sugg.add_to_diagnostic(&mut diag); @@ -1012,17 +998,17 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedSemi { #[derive(Subdiagnostic)] pub(crate) enum ExpectedSemiSugg { #[suggestion( - parser::sugg_change_this_to_semi, + parser_sugg_change_this_to_semi, code = ";", applicability = "machine-applicable" )] ChangeToSemi(#[primary_span] Span), - #[suggestion_short(parser::sugg_add_semi, code = ";", applicability = "machine-applicable")] + #[suggestion_short(parser_sugg_add_semi, code = ";", applicability = "machine-applicable")] AddSemi(#[primary_span] Span), } #[derive(Diagnostic)] -#[diag(parser::struct_literal_body_without_path)] +#[diag(parser_struct_literal_body_without_path)] pub(crate) struct StructLiteralBodyWithoutPath { #[primary_span] pub span: Span, @@ -1031,7 +1017,7 @@ pub(crate) struct StructLiteralBodyWithoutPath { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "has-placeholders")] +#[multipart_suggestion(suggestion, applicability = "has-placeholders")] pub(crate) struct StructLiteralBodyWithoutPathSugg { #[suggestion_part(code = "{{ SomeStruct ")] pub before: Span, @@ -1040,7 +1026,7 @@ pub(crate) struct StructLiteralBodyWithoutPathSugg { } #[derive(Diagnostic)] -#[diag(parser::unmatched_angle_brackets)] +#[diag(parser_unmatched_angle_brackets)] pub(crate) struct UnmatchedAngleBrackets { #[primary_span] #[suggestion(code = "", applicability = "machine-applicable")] @@ -1049,7 +1035,7 @@ pub(crate) struct UnmatchedAngleBrackets { } #[derive(Diagnostic)] -#[diag(parser::generic_parameters_without_angle_brackets)] +#[diag(parser_generic_parameters_without_angle_brackets)] pub(crate) struct GenericParamsWithoutAngleBrackets { #[primary_span] pub span: Span, @@ -1058,7 +1044,7 @@ pub(crate) struct GenericParamsWithoutAngleBrackets { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] +#[multipart_suggestion(suggestion, applicability = "machine-applicable")] pub(crate) struct GenericParamsWithoutAngleBracketsSugg { #[suggestion_part(code = "<")] pub left: Span, @@ -1067,18 +1053,18 @@ pub(crate) struct GenericParamsWithoutAngleBracketsSugg { } #[derive(Diagnostic)] -#[diag(parser::comparison_operators_cannot_be_chained)] +#[diag(parser_comparison_operators_cannot_be_chained)] pub(crate) struct ComparisonOperatorsCannotBeChained { #[primary_span] pub span: Vec<Span>, #[suggestion_verbose( - parser::sugg_turbofish_syntax, + parser_sugg_turbofish_syntax, code = "::", applicability = "maybe-incorrect" )] pub suggest_turbofish: Option<Span>, - #[help(parser::sugg_turbofish_syntax)] - #[help(parser::sugg_parentheses_for_function_args)] + #[help(parser_sugg_turbofish_syntax)] + #[help(sugg_parentheses_for_function_args)] pub help_turbofish: Option<()>, #[subdiagnostic] pub chaining_sugg: Option<ComparisonOperatorsCannotBeChainedSugg>, @@ -1087,7 +1073,7 @@ pub(crate) struct ComparisonOperatorsCannotBeChained { #[derive(Subdiagnostic)] pub(crate) enum ComparisonOperatorsCannotBeChainedSugg { #[suggestion_verbose( - parser::sugg_split_comparison, + sugg_split_comparison, code = " && {middle_term}", applicability = "maybe-incorrect" )] @@ -1096,7 +1082,7 @@ pub(crate) enum ComparisonOperatorsCannotBeChainedSugg { span: Span, middle_term: String, }, - #[multipart_suggestion(parser::sugg_parenthesize, applicability = "maybe-incorrect")] + #[multipart_suggestion(sugg_parenthesize, applicability = "maybe-incorrect")] Parenthesize { #[suggestion_part(code = "(")] left: Span, @@ -1106,7 +1092,7 @@ pub(crate) enum ComparisonOperatorsCannotBeChainedSugg { } #[derive(Diagnostic)] -#[diag(parser::question_mark_in_type)] +#[diag(parser_question_mark_in_type)] pub(crate) struct QuestionMarkInType { #[primary_span] #[label] @@ -1116,7 +1102,7 @@ pub(crate) struct QuestionMarkInType { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] +#[multipart_suggestion(suggestion, applicability = "machine-applicable")] pub(crate) struct QuestionMarkInTypeSugg { #[suggestion_part(code = "Option<")] pub left: Span, @@ -1125,7 +1111,7 @@ pub(crate) struct QuestionMarkInTypeSugg { } #[derive(Diagnostic)] -#[diag(parser::unexpected_parentheses_in_for_head)] +#[diag(parser_unexpected_parentheses_in_for_head)] pub(crate) struct ParenthesesInForHead { #[primary_span] pub span: Vec<Span>, @@ -1134,7 +1120,7 @@ pub(crate) struct ParenthesesInForHead { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] +#[multipart_suggestion(suggestion, applicability = "machine-applicable")] pub(crate) struct ParenthesesInForHeadSugg { #[suggestion_part(code = "")] pub left: Span, @@ -1143,7 +1129,7 @@ pub(crate) struct ParenthesesInForHeadSugg { } #[derive(Diagnostic)] -#[diag(parser::doc_comment_on_param_type)] +#[diag(parser_doc_comment_on_param_type)] pub(crate) struct DocCommentOnParamType { #[primary_span] #[label] @@ -1151,7 +1137,7 @@ pub(crate) struct DocCommentOnParamType { } #[derive(Diagnostic)] -#[diag(parser::attribute_on_param_type)] +#[diag(parser_attribute_on_param_type)] pub(crate) struct AttributeOnParamType { #[primary_span] #[label] @@ -1159,7 +1145,7 @@ pub(crate) struct AttributeOnParamType { } #[derive(Diagnostic)] -#[diag(parser::pattern_method_param_without_body, code = "E0642")] +#[diag(parser_pattern_method_param_without_body, code = "E0642")] pub(crate) struct PatternMethodParamWithoutBody { #[primary_span] #[suggestion(code = "_", applicability = "machine-applicable")] @@ -1167,7 +1153,7 @@ pub(crate) struct PatternMethodParamWithoutBody { } #[derive(Diagnostic)] -#[diag(parser::self_param_not_first)] +#[diag(parser_self_param_not_first)] pub(crate) struct SelfParamNotFirst { #[primary_span] #[label] @@ -1175,7 +1161,7 @@ pub(crate) struct SelfParamNotFirst { } #[derive(Diagnostic)] -#[diag(parser::const_generic_without_braces)] +#[diag(parser_const_generic_without_braces)] pub(crate) struct ConstGenericWithoutBraces { #[primary_span] pub span: Span, @@ -1184,7 +1170,7 @@ pub(crate) struct ConstGenericWithoutBraces { } #[derive(Subdiagnostic)] -#[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] +#[multipart_suggestion(suggestion, applicability = "machine-applicable")] pub(crate) struct ConstGenericWithoutBracesSugg { #[suggestion_part(code = "{{ ")] pub left: Span, @@ -1193,7 +1179,7 @@ pub(crate) struct ConstGenericWithoutBracesSugg { } #[derive(Diagnostic)] -#[diag(parser::unexpected_const_param_declaration)] +#[diag(parser_unexpected_const_param_declaration)] pub(crate) struct UnexpectedConstParamDeclaration { #[primary_span] #[label] @@ -1204,7 +1190,7 @@ pub(crate) struct UnexpectedConstParamDeclaration { #[derive(Subdiagnostic)] pub(crate) enum UnexpectedConstParamDeclarationSugg { - #[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] + #[multipart_suggestion(suggestion, applicability = "machine-applicable")] AddParam { #[suggestion_part(code = "<{snippet}>")] impl_generics: Span, @@ -1213,7 +1199,7 @@ pub(crate) enum UnexpectedConstParamDeclarationSugg { snippet: String, ident: String, }, - #[multipart_suggestion(parser::suggestion, applicability = "machine-applicable")] + #[multipart_suggestion(suggestion, applicability = "machine-applicable")] AppendParam { #[suggestion_part(code = ", {snippet}")] impl_generics_end: Span, @@ -1225,7 +1211,7 @@ pub(crate) enum UnexpectedConstParamDeclarationSugg { } #[derive(Diagnostic)] -#[diag(parser::unexpected_const_in_generic_param)] +#[diag(parser_unexpected_const_in_generic_param)] pub(crate) struct UnexpectedConstInGenericParam { #[primary_span] pub span: Span, @@ -1234,7 +1220,7 @@ pub(crate) struct UnexpectedConstInGenericParam { } #[derive(Diagnostic)] -#[diag(parser::async_move_order_incorrect)] +#[diag(parser_async_move_order_incorrect)] pub(crate) struct AsyncMoveOrderIncorrect { #[primary_span] #[suggestion_verbose(code = "async move", applicability = "maybe-incorrect")] @@ -1242,7 +1228,7 @@ pub(crate) struct AsyncMoveOrderIncorrect { } #[derive(Diagnostic)] -#[diag(parser::double_colon_in_bound)] +#[diag(parser_double_colon_in_bound)] pub(crate) struct DoubleColonInBound { #[primary_span] pub span: Span, diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index 88540e13ef2..462bce16ad7 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -3,7 +3,9 @@ use rustc_ast::ast::{self, AttrStyle}; use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind}; use rustc_ast::tokenstream::TokenStream; use rustc_ast::util::unicode::contains_text_flow_control_chars; -use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult}; +use rustc_errors::{ + error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult, StashKey, +}; use rustc_lexer::unescape::{self, Mode}; use rustc_lexer::Cursor; use rustc_lexer::{Base, DocStyle, RawStrError}; @@ -203,7 +205,10 @@ impl<'a> StringReader<'a> { // this is necessary. let lifetime_name = self.str_from(start); if starts_with_number { - self.err_span_(start, self.pos, "lifetimes cannot start with a number"); + let span = self.mk_sp(start, self.pos); + let mut diag = self.sess.struct_err("lifetimes cannot start with a number"); + diag.set_span(span); + diag.stash(span, StashKey::LifetimeIsChar); } let ident = Symbol::intern(lifetime_name); token::Lifetime(ident) diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index 77c4fadab45..f075de71426 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -113,11 +113,26 @@ pub(crate) fn emit_unescape_error( } else { ("", "if you meant to write a `str` literal, use double quotes") }; - + let mut escaped = String::with_capacity(lit.len()); + let mut chrs = lit.chars().peekable(); + while let Some(first) = chrs.next() { + match (first, chrs.peek()) { + ('\\', Some('"')) => { + escaped.push('\\'); + escaped.push('"'); + chrs.next(); + } + ('"', _) => { + escaped.push('\\'); + escaped.push('"') + } + (c, _) => escaped.push(c), + }; + } handler.span_suggestion( span_with_quotes, msg, - format!("{}\"{}\"", prefix, lit), + format!("{prefix}\"{escaped}\""), Applicability::MachineApplicable, ); } diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index 58be348883c..9e45656946b 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -55,7 +55,7 @@ impl<'a> Parser<'a> { let span = self.token.span; let mut err = self.sess.span_diagnostic.struct_span_err_with_code( span, - fluent::parser::inner_doc_comment_not_permitted, + fluent::parser_inner_doc_comment_not_permitted, error_code!(E0753), ); if let Some(replacement_span) = self.annotate_following_item_if_applicable( @@ -66,10 +66,10 @@ impl<'a> Parser<'a> { token::CommentKind::Block => OuterAttributeType::DocBlockComment, }, ) { - err.note(fluent::parser::note); + err.note(fluent::note); err.span_suggestion_verbose( replacement_span, - fluent::parser::suggestion, + fluent::suggestion, "", rustc_errors::Applicability::MachineApplicable, ); @@ -173,10 +173,10 @@ impl<'a> Parser<'a> { Ok(Some(item)) => { // FIXME(#100717) err.set_arg("item", item.kind.descr()); - err.span_label(item.span, fluent::parser::label_does_not_annotate_this); + err.span_label(item.span, fluent::label_does_not_annotate_this); err.span_suggestion_verbose( replacement_span, - fluent::parser::sugg_change_inner_to_outer, + fluent::sugg_change_inner_to_outer, match attr_type { OuterAttributeType::Attribute => "", OuterAttributeType::DocBlockComment => "*", @@ -200,27 +200,27 @@ impl<'a> Parser<'a> { Some(InnerAttrForbiddenReason::AfterOuterDocComment { prev_doc_comment_span }) => { let mut diag = self.struct_span_err( attr_sp, - fluent::parser::inner_attr_not_permitted_after_outer_doc_comment, + fluent::parser_inner_attr_not_permitted_after_outer_doc_comment, ); - diag.span_label(attr_sp, fluent::parser::label_attr) - .span_label(prev_doc_comment_span, fluent::parser::label_prev_doc_comment); + diag.span_label(attr_sp, fluent::label_attr) + .span_label(prev_doc_comment_span, fluent::label_prev_doc_comment); diag } Some(InnerAttrForbiddenReason::AfterOuterAttribute { prev_outer_attr_sp }) => { let mut diag = self.struct_span_err( attr_sp, - fluent::parser::inner_attr_not_permitted_after_outer_attr, + fluent::parser_inner_attr_not_permitted_after_outer_attr, ); - diag.span_label(attr_sp, fluent::parser::label_attr) - .span_label(prev_outer_attr_sp, fluent::parser::label_prev_attr); + diag.span_label(attr_sp, fluent::label_attr) + .span_label(prev_outer_attr_sp, fluent::label_prev_attr); diag } Some(InnerAttrForbiddenReason::InCodeBlock) | None => { - self.struct_span_err(attr_sp, fluent::parser::inner_attr_not_permitted) + self.struct_span_err(attr_sp, fluent::parser_inner_attr_not_permitted) } }; - diag.note(fluent::parser::inner_attr_explanation); + diag.note(fluent::parser_inner_attr_explanation); if self .annotate_following_item_if_applicable( &mut diag, @@ -229,7 +229,7 @@ impl<'a> Parser<'a> { ) .is_some() { - diag.note(fluent::parser::outer_attr_explanation); + diag.note(fluent::parser_outer_attr_explanation); }; diag.emit(); } diff --git a/compiler/rustc_parse/src/parser/attr_wrapper.rs b/compiler/rustc_parse/src/parser/attr_wrapper.rs index 81c051b8f35..1b16ecb5ec2 100644 --- a/compiler/rustc_parse/src/parser/attr_wrapper.rs +++ b/compiler/rustc_parse/src/parser/attr_wrapper.rs @@ -273,16 +273,23 @@ impl<'a> Parser<'a> { let cursor_snapshot_next_calls = cursor_snapshot.num_next_calls; let mut end_pos = self.token_cursor.num_next_calls; + let mut captured_trailing = false; + // Capture a trailing token if requested by the callback 'f' match trailing { TrailingToken::None => {} + TrailingToken::Gt => { + assert_eq!(self.token.kind, token::Gt); + } TrailingToken::Semi => { assert_eq!(self.token.kind, token::Semi); end_pos += 1; + captured_trailing = true; } TrailingToken::MaybeComma => { if self.token.kind == token::Comma { end_pos += 1; + captured_trailing = true; } } } @@ -292,11 +299,7 @@ impl<'a> Parser<'a> { // was not actually bumped past it. When the `LazyAttrTokenStream` gets converted // into an `AttrTokenStream`, we will create the proper token. if self.token_cursor.break_last_token { - assert_eq!( - trailing, - TrailingToken::None, - "Cannot set `break_last_token` and have trailing token" - ); + assert!(!captured_trailing, "Cannot set break_last_token and have trailing token"); end_pos += 1; } diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index d654d84cdd5..887a4a6de33 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -926,7 +926,7 @@ impl<'a> Parser<'a> { if self.eat(&token::Gt) { e.span_suggestion_verbose( binop.span.shrink_to_lo(), - fluent::parser::sugg_turbofish_syntax, + fluent::parser_sugg_turbofish_syntax, "::", Applicability::MaybeIncorrect, ) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 5b466cec8e1..ca216b1cd10 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -42,8 +42,10 @@ use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits}; use rustc_ast::{ClosureBinder, StmtKind}; use rustc_ast_pretty::pprust; -use rustc_errors::IntoDiagnostic; -use rustc_errors::{Applicability, Diagnostic, PResult}; +use rustc_errors::{ + Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, PResult, + StashKey, +}; use rustc_session::errors::ExprParenthesesNeeded; use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP; use rustc_session::lint::BuiltinLintDiagnostics; @@ -1513,11 +1515,11 @@ impl<'a> Parser<'a> { /// Parse `'label: $expr`. The label is already parsed. fn parse_labeled_expr( &mut self, - label: Label, + label_: Label, mut consume_colon: bool, ) -> PResult<'a, P<Expr>> { - let lo = label.ident.span; - let label = Some(label); + let lo = label_.ident.span; + let label = Some(label_); let ate_colon = self.eat(&token::Colon); let expr = if self.eat_keyword(kw::While) { self.parse_while_expr(label, lo) @@ -1530,6 +1532,19 @@ impl<'a> Parser<'a> { { self.parse_block_expr(label, lo, BlockCheckMode::Default) } else if !ate_colon + && (matches!(self.token.kind, token::CloseDelim(_) | token::Comma) + || self.token.is_op()) + { + let lit = self.recover_unclosed_char(label_.ident, |self_| { + self_.sess.create_err(UnexpectedTokenAfterLabel { + span: self_.token.span, + remove_label: None, + enclose_in_block: None, + }) + }); + consume_colon = false; + Ok(self.mk_expr(lo, ExprKind::Lit(lit))) + } else if !ate_colon && (self.check_noexpect(&TokenKind::Comma) || self.check_noexpect(&TokenKind::Gt)) { // We're probably inside of a `Path<'a>` that needs a turbofish @@ -1603,6 +1618,39 @@ impl<'a> Parser<'a> { Ok(expr) } + /// Emit an error when a char is parsed as a lifetime because of a missing quote + pub(super) fn recover_unclosed_char( + &mut self, + lifetime: Ident, + err: impl FnOnce(&mut Self) -> DiagnosticBuilder<'a, ErrorGuaranteed>, + ) -> ast::Lit { + if let Some(mut diag) = + self.sess.span_diagnostic.steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar) + { + diag.span_suggestion_verbose( + lifetime.span.shrink_to_hi(), + "add `'` to close the char literal", + "'", + Applicability::MaybeIncorrect, + ) + .emit(); + } else { + err(self) + .span_suggestion_verbose( + lifetime.span.shrink_to_hi(), + "add `'` to close the char literal", + "'", + Applicability::MaybeIncorrect, + ) + .emit(); + } + ast::Lit { + token_lit: token::Lit::new(token::LitKind::Char, lifetime.name, None), + kind: ast::LitKind::Char(lifetime.name.as_str().chars().next().unwrap_or('_')), + span: lifetime.span, + } + } + /// Recover on the syntax `do catch { ... }` suggesting `try { ... }` instead. fn recover_do_catch(&mut self) -> PResult<'a, P<Expr>> { let lo = self.token.span; @@ -1728,7 +1776,7 @@ impl<'a> Parser<'a> { } pub(super) fn parse_lit(&mut self) -> PResult<'a, Lit> { - self.parse_opt_lit().ok_or_else(|| { + self.parse_opt_lit().ok_or(()).or_else(|()| { if let token::Interpolated(inner) = &self.token.kind { let expr = match inner.as_ref() { token::NtExpr(expr) => Some(expr), @@ -1740,12 +1788,22 @@ impl<'a> Parser<'a> { let mut err = InvalidInterpolatedExpression { span: self.token.span } .into_diagnostic(&self.sess.span_diagnostic); err.downgrade_to_delayed_bug(); - return err; + return Err(err); } } } - let msg = format!("unexpected token: {}", super::token_descr(&self.token)); - self.struct_span_err(self.token.span, &msg) + let token = self.token.clone(); + let err = |self_: &mut Self| { + let msg = format!("unexpected token: {}", super::token_descr(&token)); + self_.struct_span_err(token.span, &msg) + }; + // On an error path, eagerly consider a lifetime to be an unclosed character lit + if self.token.is_lifetime() { + let lt = self.expect_lifetime(); + Ok(self.recover_unclosed_char(lt.ident, err)) + } else { + Err(err(self)) + } }) } @@ -3084,6 +3142,8 @@ impl<'a> Parser<'a> { && this.token.kind == token::Semi { TrailingToken::Semi + } else if this.token.kind == token::Gt { + TrailingToken::Gt } else { // FIXME - pass this through from the place where we know // we need a comma, rather than assuming that `#[attr] expr,` diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index b934e087608..89c24920f85 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -79,6 +79,7 @@ pub enum ForceCollect { pub enum TrailingToken { None, Semi, + Gt, /// If the trailing token is a comma, then capture it /// Otherwise, ignore the trailing token MaybeComma, diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 56efec422d6..52c11b4e35f 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -402,6 +402,25 @@ impl<'a> Parser<'a> { } else { PatKind::Path(qself, path) } + } else if matches!(self.token.kind, token::Lifetime(_)) + // In pattern position, we're totally fine with using "next token isn't colon" + // as a heuristic. We could probably just always try to recover if it's a lifetime, + // because we never have `'a: label {}` in a pattern position anyways, but it does + // keep us from suggesting something like `let 'a: Ty = ..` => `let 'a': Ty = ..` + && !self.look_ahead(1, |token| matches!(token.kind, token::Colon)) + { + // Recover a `'a` as a `'a'` literal + let lt = self.expect_lifetime(); + let lit = self.recover_unclosed_char(lt.ident, |self_| { + let expected = expected.unwrap_or("pattern"); + let msg = + format!("expected {}, found {}", expected, super::token_descr(&self_.token)); + + let mut err = self_.struct_span_err(self_.token.span, &msg); + err.span_label(self_.token.span, format!("expected {}", expected)); + err + }); + PatKind::Lit(self.mk_expr(lo, ExprKind::Lit(lit))) } else { // Try to parse everything else as literal with optional minus match self.parse_literal_maybe_minus() { @@ -799,6 +818,7 @@ impl<'a> Parser<'a> { || t.kind == token::Dot // e.g. `.5` for recovery; || t.can_begin_literal_maybe_minus() // e.g. `42`. || t.is_whole_expr() + || t.is_lifetime() // recover `'a` instead of `'a'` }) } diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index a61e77b7c3b..12753c6785c 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -553,39 +553,46 @@ impl<'a> Parser<'a> { match stmt.kind { // Expression without semicolon. StmtKind::Expr(ref mut expr) - if self.token != token::Eof && classify::expr_requires_semi_to_be_stmt(expr) => - { + if self.token != token::Eof && classify::expr_requires_semi_to_be_stmt(expr) => { // Just check for errors and recover; do not eat semicolon yet. - if let Err(mut e) = - self.expect_one_of(&[], &[token::Semi, token::CloseDelim(Delimiter::Brace)]) - { - if let TokenKind::DocComment(..) = self.token.kind { - if let Ok(snippet) = self.span_to_snippet(self.token.span) { - let sp = self.token.span; - let marker = &snippet[..3]; - let (comment_marker, doc_comment_marker) = marker.split_at(2); - - e.span_suggestion( - sp.with_hi(sp.lo() + BytePos(marker.len() as u32)), - &format!( - "add a space before `{}` to use a regular comment", - doc_comment_marker, - ), - format!("{} {}", comment_marker, doc_comment_marker), - Applicability::MaybeIncorrect, - ); + // `expect_one_of` returns PResult<'a, bool /* recovered */> + let replace_with_err = + match self.expect_one_of(&[], &[token::Semi, token::CloseDelim(Delimiter::Brace)]) { + // Recover from parser, skip type error to avoid extra errors. + Ok(true) => true, + Err(mut e) => { + if let TokenKind::DocComment(..) = self.token.kind && + let Ok(snippet) = self.span_to_snippet(self.token.span) { + let sp = self.token.span; + let marker = &snippet[..3]; + let (comment_marker, doc_comment_marker) = marker.split_at(2); + + e.span_suggestion( + sp.with_hi(sp.lo() + BytePos(marker.len() as u32)), + &format!( + "add a space before `{}` to use a regular comment", + doc_comment_marker, + ), + format!("{} {}", comment_marker, doc_comment_marker), + Applicability::MaybeIncorrect, + ); } - } - if let Err(mut e) = - self.check_mistyped_turbofish_with_multiple_type_params(e, expr) - { - if recover.no() { - return Err(e); + + if let Err(mut e) = + self.check_mistyped_turbofish_with_multiple_type_params(e, expr) + { + if recover.no() { + return Err(e); + } + e.emit(); + self.recover_stmt(); } - e.emit(); - self.recover_stmt(); + true } - // Don't complain about type errors in body tail after parse error (#57383). + _ => false + }; + if replace_with_err { + // We already emitted an error, so don't emit another type error let sp = expr.span.to(self.prev_token.span); *expr = self.mk_expr_err(sp); } |
