Skip to content

Commit d7c6457

Browse files
committed
Implement IntoDiagnosticArg for rustc_ast::token::Token(Kind)
1 parent 37fdcb4 commit d7c6457

File tree

7 files changed

+73
-66
lines changed

7 files changed

+73
-66
lines changed

compiler/rustc_error_messages/locales/en-US/parser.ftl

+10-10
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ parser_inner_doc_comment_not_permitted = expected outer doc comment
291291
.label_does_not_annotate_this = the inner doc comment doesn't annotate this {$item}
292292
.sugg_change_inner_to_outer = to annotate the {$item}, change the doc comment from inner to outer style
293293
294-
parser_expected_identifier_found_reserved_identifier_str = expected identifier, found reserved identifier `{$token_str}`
295-
parser_expected_identifier_found_keyword_str = expected identifier, found keyword `{$token_str}`
296-
parser_expected_identifier_found_reserved_keyword_str = expected identifier, found reserved keyword `{$token_str}`
297-
parser_expected_identifier_found_doc_comment_str = expected identifier, found doc comment `{$token_str}`
298-
parser_expected_identifier_found_str = expected identifier, found `{$token_str}`
294+
parser_expected_identifier_found_reserved_identifier_str = expected identifier, found reserved identifier `{$token}`
295+
parser_expected_identifier_found_keyword_str = expected identifier, found keyword `{$token}`
296+
parser_expected_identifier_found_reserved_keyword_str = expected identifier, found reserved keyword `{$token}`
297+
parser_expected_identifier_found_doc_comment_str = expected identifier, found doc comment `{$token}`
298+
parser_expected_identifier_found_str = expected identifier, found `{$token}`
299299
300300
parser_expected_identifier_found_reserved_identifier = expected identifier, found reserved identifier
301301
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
307307
308308
parser_sugg_remove_comma = remove this comma
309309
310-
parser_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token_str}`
311-
parser_expected_semi_found_keyword_str = expected `;`, found keyword `{$token_str}`
312-
parser_expected_semi_found_reserved_keyword_str = expected `;`, found reserved keyword `{$token_str}`
313-
parser_expected_semi_found_doc_comment_str = expected `;`, found doc comment `{$token_str}`
314-
parser_expected_semi_found_str = expected `;`, found `{$token_str}`
310+
parser_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token}`
311+
parser_expected_semi_found_keyword_str = expected `;`, found keyword `{$token}`
312+
parser_expected_semi_found_reserved_keyword_str = expected `;`, found reserved keyword `{$token}`
313+
parser_expected_semi_found_doc_comment_str = expected `;`, found doc comment `{$token}`
314+
parser_expected_semi_found_str = expected `;`, found `{$token}`
315315
316316
parser_sugg_change_this_to_semi = change this to `;`
317317
parser_sugg_add_semi = add `;` here

compiler/rustc_errors/src/diagnostic.rs

+12
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ impl IntoDiagnosticArg for ast::Path {
183183
}
184184
}
185185

186+
impl IntoDiagnosticArg for ast::token::Token {
187+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
188+
DiagnosticArgValue::Str(pprust::token_to_string(&self))
189+
}
190+
}
191+
192+
impl IntoDiagnosticArg for ast::token::TokenKind {
193+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
194+
DiagnosticArgValue::Str(pprust::token_kind_to_string(&self))
195+
}
196+
}
197+
186198
/// Trait implemented by error types. This should not be implemented manually. Instead, use
187199
/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic].
188200
#[cfg_attr(bootstrap, rustc_diagnostic_item = "AddSubdiagnostic")]

compiler/rustc_parse/src/errors.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use rustc_ast::token::Token;
12
use rustc_ast::Path;
23
use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic};
34
use rustc_macros::{Diagnostic, Subdiagnostic};
45
use rustc_session::errors::ExprParenthesesNeeded;
56
use rustc_span::symbol::Ident;
67
use rustc_span::{Span, Symbol};
78

8-
use crate::parser::{TokenDescription, TokenDescriptionKind};
9+
use crate::parser::TokenDescription;
910

1011
#[derive(Diagnostic)]
1112
#[diag(parser::maybe_report_ambiguous_plus)]
@@ -572,7 +573,7 @@ pub(crate) struct FoundExprWouldBeStmt {
572573
#[primary_span]
573574
#[label]
574575
pub span: Span,
575-
pub token: String,
576+
pub token: Token,
576577
#[subdiagnostic]
577578
pub suggestion: ExprParenthesesNeeded,
578579
}
@@ -871,7 +872,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
871872
pub(crate) struct InvalidMetaItem {
872873
#[primary_span]
873874
pub span: Span,
874-
pub token: String,
875+
pub token: Token,
875876
}
876877

877878
#[derive(Subdiagnostic)]
@@ -908,22 +909,22 @@ pub(crate) enum ExpectedIdentifierFound {
908909
}
909910

910911
impl ExpectedIdentifierFound {
911-
pub fn new(token_descr_kind: Option<TokenDescriptionKind>, span: Span) -> Self {
912-
(match token_descr_kind {
913-
Some(TokenDescriptionKind::ReservedIdentifier) => {
912+
pub fn new(token_descr: Option<TokenDescription>, span: Span) -> Self {
913+
(match token_descr {
914+
Some(TokenDescription::ReservedIdentifier) => {
914915
ExpectedIdentifierFound::ReservedIdentifier
915916
}
916-
Some(TokenDescriptionKind::Keyword) => ExpectedIdentifierFound::Keyword,
917-
Some(TokenDescriptionKind::ReservedKeyword) => ExpectedIdentifierFound::ReservedKeyword,
918-
Some(TokenDescriptionKind::DocComment) => ExpectedIdentifierFound::DocComment,
917+
Some(TokenDescription::Keyword) => ExpectedIdentifierFound::Keyword,
918+
Some(TokenDescription::ReservedKeyword) => ExpectedIdentifierFound::ReservedKeyword,
919+
Some(TokenDescription::DocComment) => ExpectedIdentifierFound::DocComment,
919920
None => ExpectedIdentifierFound::Other,
920921
})(span)
921922
}
922923
}
923924

924925
pub(crate) struct ExpectedIdentifier {
925926
pub span: Span,
926-
pub token_descr: TokenDescription,
927+
pub token: Token,
927928
pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>,
928929
pub suggest_remove_comma: Option<SuggRemoveComma>,
929930
}
@@ -933,29 +934,31 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
933934
self,
934935
handler: &'a rustc_errors::Handler,
935936
) -> rustc_errors::DiagnosticBuilder<'a, G> {
936-
let mut diag = handler.struct_diagnostic(match self.token_descr.kind {
937-
Some(TokenDescriptionKind::ReservedIdentifier) => {
937+
let token_descr = super::parser::TokenDescription::from_token(&self.token);
938+
939+
let mut diag = handler.struct_diagnostic(match token_descr {
940+
Some(TokenDescription::ReservedIdentifier) => {
938941
fluent::parser::expected_identifier_found_reserved_identifier_str
939942
}
940-
Some(TokenDescriptionKind::Keyword) => {
943+
Some(TokenDescription::Keyword) => {
941944
fluent::parser::expected_identifier_found_keyword_str
942945
}
943-
Some(TokenDescriptionKind::ReservedKeyword) => {
946+
Some(TokenDescription::ReservedKeyword) => {
944947
fluent::parser::expected_identifier_found_reserved_keyword_str
945948
}
946-
Some(TokenDescriptionKind::DocComment) => {
949+
Some(TokenDescription::DocComment) => {
947950
fluent::parser::expected_identifier_found_doc_comment_str
948951
}
949952
None => fluent::parser::expected_identifier_found_str,
950953
});
951954
diag.set_span(self.span);
952-
diag.set_arg("token_str", self.token_descr.name);
955+
diag.set_arg("token", self.token);
953956

954957
if let Some(sugg) = self.suggest_raw {
955958
sugg.add_to_diagnostic(&mut diag);
956959
}
957960

958-
ExpectedIdentifierFound::new(self.token_descr.kind, self.span).add_to_diagnostic(&mut diag);
961+
ExpectedIdentifierFound::new(token_descr, self.span).add_to_diagnostic(&mut diag);
959962

960963
if let Some(sugg) = self.suggest_remove_comma {
961964
sugg.add_to_diagnostic(&mut diag);
@@ -967,7 +970,7 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
967970

968971
pub(crate) struct ExpectedSemi {
969972
pub span: Span,
970-
pub token_descr: TokenDescription,
973+
pub token: Token,
971974

972975
pub unexpected_token_label: Option<Span>,
973976
pub sugg: ExpectedSemiSugg,
@@ -978,21 +981,23 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedSemi {
978981
self,
979982
handler: &'a rustc_errors::Handler,
980983
) -> rustc_errors::DiagnosticBuilder<'a, G> {
981-
let mut diag = handler.struct_diagnostic(match self.token_descr.kind {
982-
Some(TokenDescriptionKind::ReservedIdentifier) => {
984+
let token_descr = super::parser::TokenDescription::from_token(&self.token);
985+
986+
let mut diag = handler.struct_diagnostic(match token_descr {
987+
Some(TokenDescription::ReservedIdentifier) => {
983988
fluent::parser::expected_semi_found_reserved_identifier_str
984989
}
985-
Some(TokenDescriptionKind::Keyword) => fluent::parser::expected_semi_found_keyword_str,
986-
Some(TokenDescriptionKind::ReservedKeyword) => {
990+
Some(TokenDescription::Keyword) => fluent::parser::expected_semi_found_keyword_str,
991+
Some(TokenDescription::ReservedKeyword) => {
987992
fluent::parser::expected_semi_found_reserved_keyword_str
988993
}
989-
Some(TokenDescriptionKind::DocComment) => {
994+
Some(TokenDescription::DocComment) => {
990995
fluent::parser::expected_semi_found_doc_comment_str
991996
}
992997
None => fluent::parser::expected_semi_found_str,
993998
});
994999
diag.set_span(self.span);
995-
diag.set_arg("token_str", self.token_descr.name);
1000+
diag.set_arg("token", self.token);
9961001

9971002
if let Some(unexpected_token_label) = self.unexpected_token_label {
9981003
diag.span_label(unexpected_token_label, fluent::parser::label_unexpected_token);

compiler/rustc_parse/src/parser/attr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle
44
use rustc_ast as ast;
55
use rustc_ast::attr;
66
use rustc_ast::token::{self, Delimiter, Nonterminal};
7-
use rustc_ast_pretty::pprust;
87
use rustc_errors::{error_code, fluent, Diagnostic, IntoDiagnostic, PResult};
98
use rustc_span::{sym, BytePos, Span};
109
use std::convert::TryInto;
@@ -414,8 +413,7 @@ impl<'a> Parser<'a> {
414413
Err(err) => err.cancel(),
415414
}
416415

417-
let token = pprust::token_to_string(&self.token).to_string();
418-
Err(InvalidMetaItem { span: self.token.span, token }
416+
Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }
419417
.into_diagnostic(&self.sess.span_diagnostic))
420418
}
421419
}

compiler/rustc_parse/src/parser/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl<'a> Parser<'a> {
326326

327327
let err = ExpectedIdentifier {
328328
span: self.token.span,
329-
token_descr: super::token_descr_struct(&self.token),
329+
token: self.token.clone(),
330330
suggest_raw,
331331
suggest_remove_comma,
332332
};
@@ -426,7 +426,7 @@ impl<'a> Parser<'a> {
426426
// let y = 42;
427427
self.sess.emit_err(ExpectedSemi {
428428
span: self.token.span,
429-
token_descr: super::token_descr_struct(&self.token),
429+
token: self.token.clone(),
430430
unexpected_token_label: None,
431431
sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
432432
});
@@ -451,7 +451,7 @@ impl<'a> Parser<'a> {
451451
let span = self.prev_token.span.shrink_to_hi();
452452
self.sess.emit_err(ExpectedSemi {
453453
span,
454-
token_descr: super::token_descr_struct(&self.token),
454+
token: self.token.clone(),
455455
unexpected_token_label: Some(self.token.span),
456456
sugg: ExpectedSemiSugg::AddSemi(span),
457457
});

compiler/rustc_parse/src/parser/expr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,7 @@ impl<'a> Parser<'a> {
430430
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
431431
self.sess.emit_err(FoundExprWouldBeStmt {
432432
span: self.token.span,
433-
// FIXME(#100717)
434-
token: pprust::token_to_string(&self.token).to_string(),
433+
token: self.token.clone(),
435434
suggestion: ExprParenthesesNeeded::surrounding(lhs.span),
436435
});
437436
}

compiler/rustc_parse/src/parser/mod.rs

+17-24
Original file line numberDiff line numberDiff line change
@@ -411,40 +411,33 @@ pub enum FollowedByType {
411411
}
412412

413413
#[derive(Clone, Copy, PartialEq, Eq)]
414-
pub enum TokenDescriptionKind {
414+
pub enum TokenDescription {
415415
ReservedIdentifier,
416416
Keyword,
417417
ReservedKeyword,
418418
DocComment,
419419
}
420420

421-
#[derive(Clone, PartialEq, Eq)]
422-
pub struct TokenDescription {
423-
pub kind: Option<TokenDescriptionKind>,
424-
pub name: String,
425-
}
426-
427-
pub(super) fn token_descr_struct(token: &Token) -> TokenDescription {
428-
let kind = match token.kind {
429-
_ if token.is_special_ident() => Some(TokenDescriptionKind::ReservedIdentifier),
430-
_ if token.is_used_keyword() => Some(TokenDescriptionKind::Keyword),
431-
_ if token.is_unused_keyword() => Some(TokenDescriptionKind::ReservedKeyword),
432-
token::DocComment(..) => Some(TokenDescriptionKind::DocComment),
433-
_ => None,
434-
};
435-
let name = pprust::token_to_string(token).to_string();
436-
437-
TokenDescription { kind, name }
421+
impl TokenDescription {
422+
pub fn from_token(token: &Token) -> Option<Self> {
423+
match token.kind {
424+
_ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
425+
_ if token.is_used_keyword() => Some(TokenDescription::Keyword),
426+
_ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
427+
token::DocComment(..) => Some(TokenDescription::DocComment),
428+
_ => None,
429+
}
430+
}
438431
}
439432

440433
pub(super) fn token_descr(token: &Token) -> String {
441-
let TokenDescription { kind, name } = token_descr_struct(token);
434+
let name = pprust::token_to_string(token).to_string();
442435

443-
let kind = kind.map(|kind| match kind {
444-
TokenDescriptionKind::ReservedIdentifier => "reserved identifier",
445-
TokenDescriptionKind::Keyword => "keyword",
446-
TokenDescriptionKind::ReservedKeyword => "reserved keyword",
447-
TokenDescriptionKind::DocComment => "doc comment",
436+
let kind = TokenDescription::from_token(token).map(|kind| match kind {
437+
TokenDescription::ReservedIdentifier => "reserved identifier",
438+
TokenDescription::Keyword => "keyword",
439+
TokenDescription::ReservedKeyword => "reserved keyword",
440+
TokenDescription::DocComment => "doc comment",
448441
});
449442

450443
if let Some(kind) = kind { format!("{} `{}`", kind, name) } else { format!("`{}`", name) }

0 commit comments

Comments
 (0)