Skip to content

Commit 7fdffe1

Browse files
committed
Use NonterminalKind for MetaVarDecl
This is more type safe and allows us to remove a few dead branches
1 parent 2595d75 commit 7fdffe1

File tree

5 files changed

+95
-134
lines changed

5 files changed

+95
-134
lines changed

src/librustc_ast/token.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ pub enum Nonterminal {
760760
#[cfg(target_arch = "x86_64")]
761761
rustc_data_structures::static_assert_size!(Nonterminal, 40);
762762

763-
#[derive(Copy, Clone)]
763+
#[derive(Debug, Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
764764
pub enum NonterminalKind {
765765
Item,
766766
Block,
@@ -796,6 +796,29 @@ impl NonterminalKind {
796796
_ => return None,
797797
})
798798
}
799+
fn symbol(self) -> Symbol {
800+
match self {
801+
NonterminalKind::Item => sym::item,
802+
NonterminalKind::Block => sym::block,
803+
NonterminalKind::Stmt => sym::stmt,
804+
NonterminalKind::Pat => sym::pat,
805+
NonterminalKind::Expr => sym::expr,
806+
NonterminalKind::Ty => sym::ty,
807+
NonterminalKind::Ident => sym::ident,
808+
NonterminalKind::Lifetime => sym::lifetime,
809+
NonterminalKind::Literal => sym::literal,
810+
NonterminalKind::Meta => sym::meta,
811+
NonterminalKind::Path => sym::path,
812+
NonterminalKind::Vis => sym::vis,
813+
NonterminalKind::TT => sym::tt,
814+
}
815+
}
816+
}
817+
818+
impl fmt::Display for NonterminalKind {
819+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
820+
write!(f, "{}", self.symbol())
821+
}
799822
}
800823

801824
impl Nonterminal {

src/librustc_expand/mbe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ crate mod macro_rules;
99
crate mod quoted;
1010
crate mod transcribe;
1111

12-
use rustc_ast::token::{self, Token, TokenKind};
12+
use rustc_ast::token::{self, NonterminalKind, Token, TokenKind};
1313
use rustc_ast::tokenstream::DelimSpan;
1414

1515
use rustc_span::symbol::Ident;
@@ -84,7 +84,7 @@ enum TokenTree {
8484
/// e.g., `$var`
8585
MetaVar(Span, Ident),
8686
/// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
87-
MetaVarDecl(Span, Ident /* name to bind */, Ident /* kind of nonterminal */),
87+
MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
8888
}
8989

9090
impl TokenTree {

src/librustc_expand/mbe/macro_parser.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ use TokenTreeOrTokenTreeSlice::*;
7676

7777
use crate::mbe::{self, TokenTree};
7878

79-
use rustc_ast::token::{self, DocComment, Nonterminal, NonterminalKind, Token};
79+
use rustc_ast::token::{self, DocComment, Nonterminal, Token};
8080
use rustc_parse::parser::Parser;
8181
use rustc_session::parse::ParseSess;
82-
use rustc_span::symbol::{kw, MacroRulesNormalizedIdent};
82+
use rustc_span::symbol::MacroRulesNormalizedIdent;
8383

8484
use smallvec::{smallvec, SmallVec};
8585

@@ -378,7 +378,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
378378
n_rec(sess, next_m, res.by_ref(), ret_val)?;
379379
}
380380
}
381-
TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => {
381+
TokenTree::MetaVarDecl(span, _, None) => {
382382
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
383383
return Err((span, "missing fragment specifier".to_string()));
384384
}
@@ -561,18 +561,17 @@ fn inner_parse_loop<'root, 'tt>(
561561
}
562562

563563
// We need to match a metavar (but the identifier is invalid)... this is an error
564-
TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => {
564+
TokenTree::MetaVarDecl(span, _, None) => {
565565
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
566566
return Error(span, "missing fragment specifier".to_string());
567567
}
568568
}
569569

570570
// We need to match a metavar with a valid ident... call out to the black-box
571571
// parser by adding an item to `bb_items`.
572-
TokenTree::MetaVarDecl(_, _, id) => {
572+
TokenTree::MetaVarDecl(_, _, Some(kind)) => {
573573
// Built-in nonterminals never start with these tokens,
574574
// so we can eliminate them from consideration.
575-
let kind = NonterminalKind::from_symbol(id.name).unwrap();
576575
if Parser::nonterminal_may_begin_with(kind, token) {
577576
bb_items.push(item);
578577
}
@@ -703,7 +702,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
703702
let nts = bb_items
704703
.iter()
705704
.map(|item| match item.top_elts.get_tt(item.idx) {
706-
TokenTree::MetaVarDecl(_, bind, name) => format!("{} ('{}')", name, bind),
705+
TokenTree::MetaVarDecl(_, bind, Some(kind)) => format!("{} ('{}')", kind, bind),
707706
_ => panic!(),
708707
})
709708
.collect::<Vec<String>>()
@@ -733,17 +732,13 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
733732
assert_eq!(bb_items.len(), 1);
734733

735734
let mut item = bb_items.pop().unwrap();
736-
if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) {
735+
if let TokenTree::MetaVarDecl(span, _, Some(kind)) = item.top_elts.get_tt(item.idx) {
737736
let match_cur = item.match_cur;
738-
let kind = NonterminalKind::from_symbol(ident.name).unwrap();
739737
let nt = match parser.to_mut().parse_nonterminal(kind) {
740738
Err(mut err) => {
741739
err.span_label(
742740
span,
743-
format!(
744-
"while parsing argument for this `{}` macro fragment",
745-
ident.name
746-
),
741+
format!("while parsing argument for this `{}` macro fragment", kind),
747742
)
748743
.emit();
749744
return ErrorReported;

0 commit comments

Comments
 (0)