|
1 | | -use rustc_errors::Applicability; |
| 1 | +use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic}; |
2 | 2 | use rustc_macros::{Diagnostic, Subdiagnostic}; |
3 | 3 | use rustc_session::errors::ExprParenthesesNeeded; |
4 | 4 | use rustc_span::symbol::Ident; |
5 | 5 | use rustc_span::{Span, Symbol}; |
6 | 6 |
|
| 7 | +use crate::parser::{TokenDescription, TokenDescriptionKind}; |
| 8 | + |
7 | 9 | #[derive(Diagnostic)] |
8 | 10 | #[diag(parser::maybe_report_ambiguous_plus)] |
9 | 11 | pub(crate) struct AmbiguousPlus { |
@@ -870,3 +872,94 @@ pub(crate) struct InvalidMetaItem { |
870 | 872 | pub span: Span, |
871 | 873 | pub token: String, |
872 | 874 | } |
| 875 | + |
| 876 | +#[derive(Subdiagnostic)] |
| 877 | +#[suggestion_verbose( |
| 878 | + parser::sugg_escape_to_use_as_identifier, |
| 879 | + applicability = "maybe-incorrect", |
| 880 | + code = "r#" |
| 881 | +)] |
| 882 | +pub(crate) struct SuggEscapeToUseAsIdentifier { |
| 883 | + #[primary_span] |
| 884 | + pub span: Span, |
| 885 | + pub ident_name: String, |
| 886 | +} |
| 887 | + |
| 888 | +#[derive(Subdiagnostic)] |
| 889 | +#[suggestion(parser::sugg_remove_comma, applicability = "machine-applicable", code = "")] |
| 890 | +pub(crate) struct SuggRemoveComma { |
| 891 | + #[primary_span] |
| 892 | + pub span: Span, |
| 893 | +} |
| 894 | + |
| 895 | +#[derive(Subdiagnostic)] |
| 896 | +pub(crate) enum ExpectedIdentifierFound { |
| 897 | + #[label(parser::expected_identifier_found_reserved_identifier)] |
| 898 | + ReservedIdentifier(#[primary_span] Span), |
| 899 | + #[label(parser::expected_identifier_found_keyword)] |
| 900 | + Keyword(#[primary_span] Span), |
| 901 | + #[label(parser::expected_identifier_found_reserved_keyword)] |
| 902 | + ReservedKeyword(#[primary_span] Span), |
| 903 | + #[label(parser::expected_identifier_found_doc_comment)] |
| 904 | + DocComment(#[primary_span] Span), |
| 905 | + #[label(parser::expected_identifier)] |
| 906 | + Other(#[primary_span] Span), |
| 907 | +} |
| 908 | + |
| 909 | +impl ExpectedIdentifierFound { |
| 910 | + pub fn new(token_descr_kind: Option<TokenDescriptionKind>, span: Span) -> Self { |
| 911 | + (match token_descr_kind { |
| 912 | + Some(TokenDescriptionKind::ReservedIdentifier) => { |
| 913 | + ExpectedIdentifierFound::ReservedIdentifier |
| 914 | + } |
| 915 | + Some(TokenDescriptionKind::Keyword) => ExpectedIdentifierFound::Keyword, |
| 916 | + Some(TokenDescriptionKind::ReservedKeyword) => ExpectedIdentifierFound::ReservedKeyword, |
| 917 | + Some(TokenDescriptionKind::DocComment) => ExpectedIdentifierFound::DocComment, |
| 918 | + None => ExpectedIdentifierFound::Other, |
| 919 | + })(span) |
| 920 | + } |
| 921 | +} |
| 922 | + |
| 923 | +pub(crate) struct ExpectedIdentifier { |
| 924 | + pub span: Span, |
| 925 | + pub token_descr: TokenDescription, |
| 926 | + pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>, |
| 927 | + pub suggest_remove_comma: Option<SuggRemoveComma>, |
| 928 | +} |
| 929 | + |
| 930 | +impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier { |
| 931 | + fn into_diagnostic( |
| 932 | + self, |
| 933 | + handler: &'a rustc_errors::Handler, |
| 934 | + ) -> rustc_errors::DiagnosticBuilder<'a, G> { |
| 935 | + let mut diag = handler.struct_diagnostic(match self.token_descr.kind { |
| 936 | + Some(TokenDescriptionKind::ReservedIdentifier) => { |
| 937 | + fluent::parser::expected_identifier_found_reserved_identifier_str |
| 938 | + } |
| 939 | + Some(TokenDescriptionKind::Keyword) => { |
| 940 | + fluent::parser::expected_identifier_found_keyword_str |
| 941 | + } |
| 942 | + Some(TokenDescriptionKind::ReservedKeyword) => { |
| 943 | + fluent::parser::expected_identifier_found_reserved_keyword_str |
| 944 | + } |
| 945 | + Some(TokenDescriptionKind::DocComment) => { |
| 946 | + fluent::parser::expected_identifier_found_doc_comment_str |
| 947 | + } |
| 948 | + None => fluent::parser::expected_identifier_found_str, |
| 949 | + }); |
| 950 | + diag.set_span(self.span); |
| 951 | + diag.set_arg("token_str", self.token_descr.name); |
| 952 | + |
| 953 | + if let Some(sugg) = self.suggest_raw { |
| 954 | + sugg.add_to_diagnostic(&mut diag); |
| 955 | + } |
| 956 | + |
| 957 | + ExpectedIdentifierFound::new(self.token_descr.kind, self.span).add_to_diagnostic(&mut diag); |
| 958 | + |
| 959 | + if let Some(sugg) = self.suggest_remove_comma { |
| 960 | + sugg.add_to_diagnostic(&mut diag); |
| 961 | + } |
| 962 | + |
| 963 | + diag |
| 964 | + } |
| 965 | +} |
0 commit comments