Implement IntoDiagnosticArg for rustc_ast::token::Token(Kind)
This commit is contained in:
parent
37fdcb4b36
commit
d7c64574e0
7 changed files with 73 additions and 66 deletions
|
@ -4,7 +4,6 @@ use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle
|
|||
use rustc_ast as ast;
|
||||
use rustc_ast::attr;
|
||||
use rustc_ast::token::{self, Delimiter, Nonterminal};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_errors::{error_code, fluent, Diagnostic, IntoDiagnostic, PResult};
|
||||
use rustc_span::{sym, BytePos, Span};
|
||||
use std::convert::TryInto;
|
||||
|
@ -414,8 +413,7 @@ impl<'a> Parser<'a> {
|
|||
Err(err) => err.cancel(),
|
||||
}
|
||||
|
||||
let token = pprust::token_to_string(&self.token).to_string();
|
||||
Err(InvalidMetaItem { span: self.token.span, token }
|
||||
Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }
|
||||
.into_diagnostic(&self.sess.span_diagnostic))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -326,7 +326,7 @@ impl<'a> Parser<'a> {
|
|||
|
||||
let err = ExpectedIdentifier {
|
||||
span: self.token.span,
|
||||
token_descr: super::token_descr_struct(&self.token),
|
||||
token: self.token.clone(),
|
||||
suggest_raw,
|
||||
suggest_remove_comma,
|
||||
};
|
||||
|
@ -426,7 +426,7 @@ impl<'a> Parser<'a> {
|
|||
// let y = 42;
|
||||
self.sess.emit_err(ExpectedSemi {
|
||||
span: self.token.span,
|
||||
token_descr: super::token_descr_struct(&self.token),
|
||||
token: self.token.clone(),
|
||||
unexpected_token_label: None,
|
||||
sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
|
||||
});
|
||||
|
@ -451,7 +451,7 @@ impl<'a> Parser<'a> {
|
|||
let span = self.prev_token.span.shrink_to_hi();
|
||||
self.sess.emit_err(ExpectedSemi {
|
||||
span,
|
||||
token_descr: super::token_descr_struct(&self.token),
|
||||
token: self.token.clone(),
|
||||
unexpected_token_label: Some(self.token.span),
|
||||
sugg: ExpectedSemiSugg::AddSemi(span),
|
||||
});
|
||||
|
|
|
@ -430,8 +430,7 @@ impl<'a> Parser<'a> {
|
|||
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
|
||||
self.sess.emit_err(FoundExprWouldBeStmt {
|
||||
span: self.token.span,
|
||||
// FIXME(#100717)
|
||||
token: pprust::token_to_string(&self.token).to_string(),
|
||||
token: self.token.clone(),
|
||||
suggestion: ExprParenthesesNeeded::surrounding(lhs.span),
|
||||
});
|
||||
}
|
||||
|
|
|
@ -411,40 +411,33 @@ pub enum FollowedByType {
|
|||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TokenDescriptionKind {
|
||||
pub enum TokenDescription {
|
||||
ReservedIdentifier,
|
||||
Keyword,
|
||||
ReservedKeyword,
|
||||
DocComment,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct TokenDescription {
|
||||
pub kind: Option<TokenDescriptionKind>,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub(super) fn token_descr_struct(token: &Token) -> TokenDescription {
|
||||
let kind = match token.kind {
|
||||
_ 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 }
|
||||
impl TokenDescription {
|
||||
pub fn from_token(token: &Token) -> Option<Self> {
|
||||
match token.kind {
|
||||
_ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
|
||||
_ if token.is_used_keyword() => Some(TokenDescription::Keyword),
|
||||
_ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
|
||||
token::DocComment(..) => Some(TokenDescription::DocComment),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
TokenDescriptionKind::ReservedIdentifier => "reserved identifier",
|
||||
TokenDescriptionKind::Keyword => "keyword",
|
||||
TokenDescriptionKind::ReservedKeyword => "reserved keyword",
|
||||
TokenDescriptionKind::DocComment => "doc comment",
|
||||
let kind = TokenDescription::from_token(token).map(|kind| match kind {
|
||||
TokenDescription::ReservedIdentifier => "reserved identifier",
|
||||
TokenDescription::Keyword => "keyword",
|
||||
TokenDescription::ReservedKeyword => "reserved keyword",
|
||||
TokenDescription::DocComment => "doc comment",
|
||||
});
|
||||
|
||||
if let Some(kind) = kind { format!("{} `{}`", kind, name) } else { format!("`{}`", name) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue