1
Fork 0

Implement IntoDiagnosticArg for rustc_ast::token::Token(Kind)

This commit is contained in:
Xiretza 2022-09-22 18:39:17 +02:00
parent 37fdcb4b36
commit d7c64574e0
7 changed files with 73 additions and 66 deletions

View file

@ -291,11 +291,11 @@ parser_inner_doc_comment_not_permitted = expected outer doc comment
.label_does_not_annotate_this = the inner doc comment doesn't annotate this {$item} .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 .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_str}` parser_expected_identifier_found_reserved_identifier_str = expected identifier, found reserved identifier `{$token}`
parser_expected_identifier_found_keyword_str = expected identifier, found keyword `{$token_str}` parser_expected_identifier_found_keyword_str = expected identifier, found keyword `{$token}`
parser_expected_identifier_found_reserved_keyword_str = expected identifier, found reserved keyword `{$token_str}` 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_str}` parser_expected_identifier_found_doc_comment_str = expected identifier, found doc comment `{$token}`
parser_expected_identifier_found_str = expected identifier, found `{$token_str}` parser_expected_identifier_found_str = expected identifier, found `{$token}`
parser_expected_identifier_found_reserved_identifier = expected identifier, found reserved identifier parser_expected_identifier_found_reserved_identifier = expected identifier, found reserved identifier
parser_expected_identifier_found_keyword = expected identifier, found keyword parser_expected_identifier_found_keyword = expected identifier, found keyword
@ -307,11 +307,11 @@ parser_sugg_escape_to_use_as_identifier = escape `{$ident_name}` to use it as an
parser_sugg_remove_comma = remove this comma parser_sugg_remove_comma = remove this comma
parser_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token_str}` parser_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token}`
parser_expected_semi_found_keyword_str = expected `;`, found keyword `{$token_str}` parser_expected_semi_found_keyword_str = expected `;`, found keyword `{$token}`
parser_expected_semi_found_reserved_keyword_str = expected `;`, found reserved keyword `{$token_str}` parser_expected_semi_found_reserved_keyword_str = expected `;`, found reserved keyword `{$token}`
parser_expected_semi_found_doc_comment_str = expected `;`, found doc comment `{$token_str}` parser_expected_semi_found_doc_comment_str = expected `;`, found doc comment `{$token}`
parser_expected_semi_found_str = expected `;`, found `{$token_str}` parser_expected_semi_found_str = expected `;`, found `{$token}`
parser_sugg_change_this_to_semi = change this to `;` parser_sugg_change_this_to_semi = change this to `;`
parser_sugg_add_semi = add `;` here parser_sugg_add_semi = add `;` here

View file

@ -183,6 +183,18 @@ impl IntoDiagnosticArg for ast::Path {
} }
} }
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 /// Trait implemented by error types. This should not be implemented manually. Instead, use
/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic]. /// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
#[cfg_attr(bootstrap, rustc_diagnostic_item = "AddSubdiagnostic")] #[cfg_attr(bootstrap, rustc_diagnostic_item = "AddSubdiagnostic")]

View file

@ -1,3 +1,4 @@
use rustc_ast::token::Token;
use rustc_ast::Path; use rustc_ast::Path;
use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic}; use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic};
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
@ -5,7 +6,7 @@ use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::symbol::Ident; use rustc_span::symbol::Ident;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
use crate::parser::{TokenDescription, TokenDescriptionKind}; use crate::parser::TokenDescription;
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(parser::maybe_report_ambiguous_plus)] #[diag(parser::maybe_report_ambiguous_plus)]
@ -572,7 +573,7 @@ pub(crate) struct FoundExprWouldBeStmt {
#[primary_span] #[primary_span]
#[label] #[label]
pub span: Span, pub span: Span,
pub token: String, pub token: Token,
#[subdiagnostic] #[subdiagnostic]
pub suggestion: ExprParenthesesNeeded, pub suggestion: ExprParenthesesNeeded,
} }
@ -871,7 +872,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
pub(crate) struct InvalidMetaItem { pub(crate) struct InvalidMetaItem {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
pub token: String, pub token: Token,
} }
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]
@ -908,14 +909,14 @@ pub(crate) enum ExpectedIdentifierFound {
} }
impl ExpectedIdentifierFound { impl ExpectedIdentifierFound {
pub fn new(token_descr_kind: Option<TokenDescriptionKind>, span: Span) -> Self { pub fn new(token_descr: Option<TokenDescription>, span: Span) -> Self {
(match token_descr_kind { (match token_descr {
Some(TokenDescriptionKind::ReservedIdentifier) => { Some(TokenDescription::ReservedIdentifier) => {
ExpectedIdentifierFound::ReservedIdentifier ExpectedIdentifierFound::ReservedIdentifier
} }
Some(TokenDescriptionKind::Keyword) => ExpectedIdentifierFound::Keyword, Some(TokenDescription::Keyword) => ExpectedIdentifierFound::Keyword,
Some(TokenDescriptionKind::ReservedKeyword) => ExpectedIdentifierFound::ReservedKeyword, Some(TokenDescription::ReservedKeyword) => ExpectedIdentifierFound::ReservedKeyword,
Some(TokenDescriptionKind::DocComment) => ExpectedIdentifierFound::DocComment, Some(TokenDescription::DocComment) => ExpectedIdentifierFound::DocComment,
None => ExpectedIdentifierFound::Other, None => ExpectedIdentifierFound::Other,
})(span) })(span)
} }
@ -923,7 +924,7 @@ impl ExpectedIdentifierFound {
pub(crate) struct ExpectedIdentifier { pub(crate) struct ExpectedIdentifier {
pub span: Span, pub span: Span,
pub token_descr: TokenDescription, pub token: Token,
pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>, pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>,
pub suggest_remove_comma: Option<SuggRemoveComma>, pub suggest_remove_comma: Option<SuggRemoveComma>,
} }
@ -933,29 +934,31 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
self, self,
handler: &'a rustc_errors::Handler, handler: &'a rustc_errors::Handler,
) -> rustc_errors::DiagnosticBuilder<'a, G> { ) -> rustc_errors::DiagnosticBuilder<'a, G> {
let mut diag = handler.struct_diagnostic(match self.token_descr.kind { let token_descr = super::parser::TokenDescription::from_token(&self.token);
Some(TokenDescriptionKind::ReservedIdentifier) => {
let mut diag = handler.struct_diagnostic(match token_descr {
Some(TokenDescription::ReservedIdentifier) => {
fluent::parser::expected_identifier_found_reserved_identifier_str fluent::parser::expected_identifier_found_reserved_identifier_str
} }
Some(TokenDescriptionKind::Keyword) => { Some(TokenDescription::Keyword) => {
fluent::parser::expected_identifier_found_keyword_str fluent::parser::expected_identifier_found_keyword_str
} }
Some(TokenDescriptionKind::ReservedKeyword) => { Some(TokenDescription::ReservedKeyword) => {
fluent::parser::expected_identifier_found_reserved_keyword_str fluent::parser::expected_identifier_found_reserved_keyword_str
} }
Some(TokenDescriptionKind::DocComment) => { 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_span(self.span);
diag.set_arg("token_str", self.token_descr.name); diag.set_arg("token", self.token);
if let Some(sugg) = self.suggest_raw { if let Some(sugg) = self.suggest_raw {
sugg.add_to_diagnostic(&mut diag); sugg.add_to_diagnostic(&mut diag);
} }
ExpectedIdentifierFound::new(self.token_descr.kind, self.span).add_to_diagnostic(&mut diag); ExpectedIdentifierFound::new(token_descr, self.span).add_to_diagnostic(&mut diag);
if let Some(sugg) = self.suggest_remove_comma { if let Some(sugg) = self.suggest_remove_comma {
sugg.add_to_diagnostic(&mut diag); sugg.add_to_diagnostic(&mut diag);
@ -967,7 +970,7 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
pub(crate) struct ExpectedSemi { pub(crate) struct ExpectedSemi {
pub span: Span, pub span: Span,
pub token_descr: TokenDescription, pub token: Token,
pub unexpected_token_label: Option<Span>, pub unexpected_token_label: Option<Span>,
pub sugg: ExpectedSemiSugg, pub sugg: ExpectedSemiSugg,
@ -978,21 +981,23 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedSemi {
self, self,
handler: &'a rustc_errors::Handler, handler: &'a rustc_errors::Handler,
) -> rustc_errors::DiagnosticBuilder<'a, G> { ) -> rustc_errors::DiagnosticBuilder<'a, G> {
let mut diag = handler.struct_diagnostic(match self.token_descr.kind { let token_descr = super::parser::TokenDescription::from_token(&self.token);
Some(TokenDescriptionKind::ReservedIdentifier) => {
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(TokenDescriptionKind::Keyword) => fluent::parser::expected_semi_found_keyword_str, Some(TokenDescription::Keyword) => fluent::parser::expected_semi_found_keyword_str,
Some(TokenDescriptionKind::ReservedKeyword) => { Some(TokenDescription::ReservedKeyword) => {
fluent::parser::expected_semi_found_reserved_keyword_str fluent::parser::expected_semi_found_reserved_keyword_str
} }
Some(TokenDescriptionKind::DocComment) => { 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_span(self.span);
diag.set_arg("token_str", self.token_descr.name); diag.set_arg("token", self.token);
if let Some(unexpected_token_label) = self.unexpected_token_label { 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);

View file

@ -4,7 +4,6 @@ use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::attr; use rustc_ast::attr;
use rustc_ast::token::{self, Delimiter, Nonterminal}; use rustc_ast::token::{self, Delimiter, Nonterminal};
use rustc_ast_pretty::pprust;
use rustc_errors::{error_code, fluent, Diagnostic, IntoDiagnostic, PResult}; use rustc_errors::{error_code, fluent, Diagnostic, IntoDiagnostic, PResult};
use rustc_span::{sym, BytePos, Span}; use rustc_span::{sym, BytePos, Span};
use std::convert::TryInto; use std::convert::TryInto;
@ -414,8 +413,7 @@ impl<'a> Parser<'a> {
Err(err) => err.cancel(), Err(err) => err.cancel(),
} }
let token = pprust::token_to_string(&self.token).to_string(); Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }
Err(InvalidMetaItem { span: self.token.span, token }
.into_diagnostic(&self.sess.span_diagnostic)) .into_diagnostic(&self.sess.span_diagnostic))
} }
} }

View file

@ -326,7 +326,7 @@ impl<'a> Parser<'a> {
let err = ExpectedIdentifier { let err = ExpectedIdentifier {
span: self.token.span, span: self.token.span,
token_descr: super::token_descr_struct(&self.token), token: self.token.clone(),
suggest_raw, suggest_raw,
suggest_remove_comma, suggest_remove_comma,
}; };
@ -426,7 +426,7 @@ impl<'a> Parser<'a> {
// let y = 42; // let y = 42;
self.sess.emit_err(ExpectedSemi { self.sess.emit_err(ExpectedSemi {
span: self.token.span, span: self.token.span,
token_descr: super::token_descr_struct(&self.token), token: self.token.clone(),
unexpected_token_label: None, unexpected_token_label: None,
sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span), sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
}); });
@ -451,7 +451,7 @@ impl<'a> Parser<'a> {
let span = self.prev_token.span.shrink_to_hi(); let span = self.prev_token.span.shrink_to_hi();
self.sess.emit_err(ExpectedSemi { self.sess.emit_err(ExpectedSemi {
span, span,
token_descr: super::token_descr_struct(&self.token), token: self.token.clone(),
unexpected_token_label: Some(self.token.span), unexpected_token_label: Some(self.token.span),
sugg: ExpectedSemiSugg::AddSemi(span), sugg: ExpectedSemiSugg::AddSemi(span),
}); });

View file

@ -430,8 +430,7 @@ impl<'a> Parser<'a> {
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) { fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
self.sess.emit_err(FoundExprWouldBeStmt { self.sess.emit_err(FoundExprWouldBeStmt {
span: self.token.span, span: self.token.span,
// FIXME(#100717) token: self.token.clone(),
token: pprust::token_to_string(&self.token).to_string(),
suggestion: ExprParenthesesNeeded::surrounding(lhs.span), suggestion: ExprParenthesesNeeded::surrounding(lhs.span),
}); });
} }

View file

@ -411,40 +411,33 @@ pub enum FollowedByType {
} }
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
pub enum TokenDescriptionKind { pub enum TokenDescription {
ReservedIdentifier, ReservedIdentifier,
Keyword, Keyword,
ReservedKeyword, ReservedKeyword,
DocComment, DocComment,
} }
#[derive(Clone, PartialEq, Eq)] impl TokenDescription {
pub struct TokenDescription { pub fn from_token(token: &Token) -> Option<Self> {
pub kind: Option<TokenDescriptionKind>, match token.kind {
pub name: String, _ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
} _ if token.is_used_keyword() => Some(TokenDescription::Keyword),
_ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
pub(super) fn token_descr_struct(token: &Token) -> TokenDescription { token::DocComment(..) => Some(TokenDescription::DocComment),
let kind = match token.kind { _ => None,
_ if token.is_special_ident() => Some(TokenDescriptionKind::ReservedIdentifier), }
_ if token.is_used_keyword() => Some(TokenDescriptionKind::Keyword), }
_ if token.is_unused_keyword() => Some(TokenDescriptionKind::ReservedKeyword),
token::DocComment(..) => Some(TokenDescriptionKind::DocComment),
_ => None,
};
let name = pprust::token_to_string(token).to_string();
TokenDescription { kind, name }
} }
pub(super) fn token_descr(token: &Token) -> String { pub(super) fn token_descr(token: &Token) -> String {
let TokenDescription { kind, name } = token_descr_struct(token); let name = pprust::token_to_string(token).to_string();
let kind = kind.map(|kind| match kind { let kind = TokenDescription::from_token(token).map(|kind| match kind {
TokenDescriptionKind::ReservedIdentifier => "reserved identifier", TokenDescription::ReservedIdentifier => "reserved identifier",
TokenDescriptionKind::Keyword => "keyword", TokenDescription::Keyword => "keyword",
TokenDescriptionKind::ReservedKeyword => "reserved keyword", TokenDescription::ReservedKeyword => "reserved keyword",
TokenDescriptionKind::DocComment => "doc comment", TokenDescription::DocComment => "doc comment",
}); });
if let Some(kind) = kind { format!("{} `{}`", kind, name) } else { format!("`{}`", name) } if let Some(kind) = kind { format!("{} `{}`", kind, name) } else { format!("`{}`", name) }