Move TokenDescription.

From the `parser` module to the `errors` module, which is where most of
its uses are.

This means the `errors` module no longer depends on the `parser` module,
removing a cyclic dependency between the two modules.
This commit is contained in:
Nicholas Nethercote
2026-01-21 15:47:00 +11:00
parent 506ea790ca
commit b33fdb2c70
2 changed files with 31 additions and 32 deletions
+30 -2
View File
@@ -3,7 +3,7 @@
use std::borrow::Cow;
use std::path::PathBuf;
use rustc_ast::token::Token;
use rustc_ast::token::{self, InvisibleOrigin, MetaVarKind, Token};
use rustc_ast::util::parser::ExprPrecedence;
use rustc_ast::{Path, Visibility};
use rustc_errors::codes::*;
@@ -17,7 +17,6 @@
use rustc_span::{Ident, Span, Symbol};
use crate::fluent_generated as fluent;
use crate::parser::TokenDescription;
#[derive(Diagnostic)]
#[diag(parse_maybe_report_ambiguous_plus)]
@@ -3735,3 +3734,32 @@ pub(crate) struct MisspelledKw {
pub span: Span,
pub is_incorrect_case: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum TokenDescription {
ReservedIdentifier,
Keyword,
ReservedKeyword,
DocComment,
// Expanded metavariables are wrapped in invisible delimiters which aren't
// pretty-printed. In error messages we must handle these specially
// otherwise we get confusing things in messages like "expected `(`, found
// ``". It's better to say e.g. "expected `(`, found type metavariable".
MetaVar(MetaVarKind),
}
impl TokenDescription {
pub(super) 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),
token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => {
Some(TokenDescription::MetaVar(kind))
}
_ => None,
}
}
}
+1 -30
View File
@@ -49,7 +49,7 @@
pub use token_type::{ExpKeywordPair, ExpTokenPair, TokenType};
use tracing::debug;
use crate::errors::{self, IncorrectVisibilityRestriction, NonStringAbiLiteral};
use crate::errors::{self, IncorrectVisibilityRestriction, NonStringAbiLiteral, TokenDescription};
use crate::exp;
#[cfg(test)]
@@ -307,35 +307,6 @@ fn from(b: bool) -> Trailing {
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum TokenDescription {
ReservedIdentifier,
Keyword,
ReservedKeyword,
DocComment,
// Expanded metavariables are wrapped in invisible delimiters which aren't
// pretty-printed. In error messages we must handle these specially
// otherwise we get confusing things in messages like "expected `(`, found
// ``". It's better to say e.g. "expected `(`, found type metavariable".
MetaVar(MetaVarKind),
}
impl TokenDescription {
pub(super) 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),
token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => {
Some(TokenDescription::MetaVar(kind))
}
_ => None,
}
}
}
pub fn token_descr(token: &Token) -> String {
let s = pprust::token_to_string(token).to_string();