Skip to content

Commit 93f7de1

Browse files
committed
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.
1 parent 9ea252d commit 93f7de1

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

compiler/rustc_parse/src/errors.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::borrow::Cow;
44
use std::path::PathBuf;
55

6-
use rustc_ast::token::Token;
6+
use rustc_ast::token::{self, InvisibleOrigin, MetaVarKind, Token};
77
use rustc_ast::util::parser::ExprPrecedence;
88
use rustc_ast::{Path, Visibility};
99
use rustc_errors::codes::*;
@@ -17,7 +17,6 @@ use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
1717
use rustc_span::{Ident, Span, Symbol};
1818

1919
use crate::fluent_generated as fluent;
20-
use crate::parser::TokenDescription;
2120

2221
#[derive(Diagnostic)]
2322
#[diag(parse_maybe_report_ambiguous_plus)]
@@ -3716,3 +3715,32 @@ pub(crate) struct MisspelledKw {
37163715
pub span: Span,
37173716
pub is_incorrect_case: bool,
37183717
}
3718+
3719+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3720+
pub(super) enum TokenDescription {
3721+
ReservedIdentifier,
3722+
Keyword,
3723+
ReservedKeyword,
3724+
DocComment,
3725+
3726+
// Expanded metavariables are wrapped in invisible delimiters which aren't
3727+
// pretty-printed. In error messages we must handle these specially
3728+
// otherwise we get confusing things in messages like "expected `(`, found
3729+
// ``". It's better to say e.g. "expected `(`, found type metavariable".
3730+
MetaVar(MetaVarKind),
3731+
}
3732+
3733+
impl TokenDescription {
3734+
pub(super) fn from_token(token: &Token) -> Option<Self> {
3735+
match token.kind {
3736+
_ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
3737+
_ if token.is_used_keyword() => Some(TokenDescription::Keyword),
3738+
_ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
3739+
token::DocComment(..) => Some(TokenDescription::DocComment),
3740+
token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => {
3741+
Some(TokenDescription::MetaVar(kind))
3742+
}
3743+
_ => None,
3744+
}
3745+
}
3746+
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use token_type::TokenTypeSet;
4949
pub use token_type::{ExpKeywordPair, ExpTokenPair, TokenType};
5050
use tracing::debug;
5151

52-
use crate::errors::{self, IncorrectVisibilityRestriction, NonStringAbiLiteral};
52+
use crate::errors::{self, IncorrectVisibilityRestriction, NonStringAbiLiteral, TokenDescription};
5353
use crate::exp;
5454

5555
#[cfg(test)]
@@ -297,35 +297,6 @@ impl From<bool> for Trailing {
297297
}
298298
}
299299

300-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
301-
pub(super) enum TokenDescription {
302-
ReservedIdentifier,
303-
Keyword,
304-
ReservedKeyword,
305-
DocComment,
306-
307-
// Expanded metavariables are wrapped in invisible delimiters which aren't
308-
// pretty-printed. In error messages we must handle these specially
309-
// otherwise we get confusing things in messages like "expected `(`, found
310-
// ``". It's better to say e.g. "expected `(`, found type metavariable".
311-
MetaVar(MetaVarKind),
312-
}
313-
314-
impl TokenDescription {
315-
pub(super) fn from_token(token: &Token) -> Option<Self> {
316-
match token.kind {
317-
_ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
318-
_ if token.is_used_keyword() => Some(TokenDescription::Keyword),
319-
_ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
320-
token::DocComment(..) => Some(TokenDescription::DocComment),
321-
token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => {
322-
Some(TokenDescription::MetaVar(kind))
323-
}
324-
_ => None,
325-
}
326-
}
327-
}
328-
329300
pub fn token_descr(token: &Token) -> String {
330301
let s = pprust::token_to_string(token).to_string();
331302

0 commit comments

Comments
 (0)