Skip to content

Commit 773b9b6

Browse files
committed
Streamline NamedMatch.
This will make things nicer as more variants are added to `ParseNtResult`.
1 parent 65b33f2 commit 773b9b6

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ pub(crate) use ParseResult::*;
7575

7676
use crate::mbe::{macro_rules::Tracker, KleeneOp, TokenTree};
7777

78-
use rustc_ast::token::{self, DocComment, Nonterminal, NonterminalKind, Token};
78+
use rustc_ast::token::{self, DocComment, NonterminalKind, Token};
7979
use rustc_ast_pretty::pprust;
8080
use rustc_data_structures::fx::FxHashMap;
81-
use rustc_data_structures::sync::Lrc;
8281
use rustc_errors::ErrorGuaranteed;
8382
use rustc_lint_defs::pluralize;
8483
use rustc_parse::parser::{ParseNtResult, Parser};
@@ -392,12 +391,7 @@ pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize {
392391
#[derive(Debug, Clone)]
393392
pub(crate) enum NamedMatch {
394393
MatchedSeq(Vec<NamedMatch>),
395-
396-
// A metavar match of type `tt`.
397-
MatchedTokenTree(rustc_ast::tokenstream::TokenTree),
398-
399-
// A metavar match of any type other than `tt`.
400-
MatchedNonterminal(Lrc<Nonterminal>),
394+
MatchedSingle(ParseNtResult),
401395
}
402396

403397
/// Performs a token equality check, ignoring syntax context (that is, an unhygienic comparison)
@@ -691,11 +685,7 @@ impl TtParser {
691685
}
692686
Ok(nt) => nt,
693687
};
694-
let m = match nt {
695-
ParseNtResult::Nt(nt) => MatchedNonterminal(Lrc::new(nt)),
696-
ParseNtResult::Tt(tt) => MatchedTokenTree(tt),
697-
};
698-
mp.push_match(next_metavar, seq_depth, m);
688+
mp.push_match(next_metavar, seq_depth, MatchedSingle(nt));
699689
mp.idx += 1;
700690
} else {
701691
unreachable!()

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::mbe;
55
use crate::mbe::diagnostics::{annotate_doc_comment, parse_failure_msg};
66
use crate::mbe::macro_check;
77
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, Success, TtParser};
8-
use crate::mbe::macro_parser::{MatchedSeq, MatchedTokenTree, MatcherLoc};
8+
use crate::mbe::macro_parser::{MatcherLoc, NamedMatch::*};
99
use crate::mbe::transcribe::transcribe;
1010

1111
use rustc_ast as ast;
@@ -21,7 +21,7 @@ use rustc_lint_defs::builtin::{
2121
RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
2222
};
2323
use rustc_lint_defs::BuiltinLintDiagnostics;
24-
use rustc_parse::parser::{Parser, Recovery};
24+
use rustc_parse::parser::{ParseNtResult, Parser, Recovery};
2525
use rustc_session::parse::ParseSess;
2626
use rustc_session::Session;
2727
use rustc_span::edition::Edition;
@@ -500,7 +500,7 @@ pub fn compile_declarative_macro(
500500
MatchedSeq(s) => s
501501
.iter()
502502
.map(|m| {
503-
if let MatchedTokenTree(tt) = m {
503+
if let MatchedSingle(ParseNtResult::Tt(tt)) = m {
504504
let tt = mbe::quoted::parse(
505505
&TokenStream::new(vec![tt.clone()]),
506506
true,
@@ -524,7 +524,7 @@ pub fn compile_declarative_macro(
524524
MatchedSeq(s) => s
525525
.iter()
526526
.map(|m| {
527-
if let MatchedTokenTree(tt) = m {
527+
if let MatchedSingle(ParseNtResult::Tt(tt)) = m {
528528
return mbe::quoted::parse(
529529
&TokenStream::new(vec![tt.clone()]),
530530
false,

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ use crate::errors::{
33
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce,
44
NoSyntaxVarsExprRepeat, VarStillRepeating,
55
};
6-
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
6+
use crate::mbe::macro_parser::{NamedMatch, NamedMatch::*};
77
use crate::mbe::{self, MetaVarExpr};
88
use rustc_ast::mut_visit::{self, MutVisitor};
99
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_errors::{pluralize, PResult};
1313
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
14+
use rustc_parse::parser::ParseNtResult;
1415
use rustc_span::hygiene::{LocalExpnId, Transparency};
1516
use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent};
1617
use rustc_span::Span;
@@ -217,12 +218,12 @@ pub(super) fn transcribe<'a>(
217218
let ident = MacroRulesNormalizedIdent::new(original_ident);
218219
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
219220
match cur_matched {
220-
MatchedTokenTree(tt) => {
221+
MatchedSingle(ParseNtResult::Tt(tt)) => {
221222
// `tt`s are emitted into the output stream directly as "raw tokens",
222223
// without wrapping them into groups.
223224
result.push(tt.clone());
224225
}
225-
MatchedNonterminal(nt) => {
226+
MatchedSingle(ParseNtResult::Nt(nt)) => {
226227
// Other variables are emitted into the output stream as groups with
227228
// `Delimiter::Invisible` to maintain parsing priorities.
228229
// `Interpolated` is currently used for such groups in rustc parser.
@@ -298,7 +299,7 @@ fn lookup_cur_matched<'a>(
298299
interpolations.get(&ident).map(|mut matched| {
299300
for &(idx, _) in repeats {
300301
match matched {
301-
MatchedTokenTree(_) | MatchedNonterminal(_) => break,
302+
MatchedSingle(_) => break,
302303
MatchedSeq(ads) => matched = ads.get(idx).unwrap(),
303304
}
304305
}
@@ -388,7 +389,7 @@ fn lockstep_iter_size(
388389
let name = MacroRulesNormalizedIdent::new(*name);
389390
match lookup_cur_matched(name, interpolations, repeats) {
390391
Some(matched) => match matched {
391-
MatchedTokenTree(_) | MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
392+
MatchedSingle(_) => LockstepIterSize::Unconstrained,
392393
MatchedSeq(ads) => LockstepIterSize::Constraint(ads.len(), name),
393394
},
394395
_ => LockstepIterSize::Unconstrained,
@@ -437,7 +438,7 @@ fn count_repetitions<'a>(
437438
sp: &DelimSpan,
438439
) -> PResult<'a, usize> {
439440
match matched {
440-
MatchedTokenTree(_) | MatchedNonterminal(_) => {
441+
MatchedSingle(_) => {
441442
if declared_lhs_depth == 0 {
442443
return Err(cx.create_err(CountRepetitionMisplaced { span: sp.entire() }));
443444
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_ast::{Async, AttrArgs, AttrArgsEq, Expr, ExprKind, Mutability, StrLit}
2929
use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind};
3030
use rustc_ast_pretty::pprust;
3131
use rustc_data_structures::fx::FxHashMap;
32+
use rustc_data_structures::sync::Lrc;
3233
use rustc_errors::PResult;
3334
use rustc_errors::{
3435
Applicability, DiagnosticBuilder, ErrorGuaranteed, FatalError, IntoDiagnostic, MultiSpan,
@@ -1506,8 +1507,14 @@ pub enum FlatToken {
15061507
Empty,
15071508
}
15081509

1509-
#[derive(Debug)]
1510+
// Metavar captures of various kinds.
1511+
//
1512+
// njn: I'm worried about the `Clone` here when new variants are added for all
1513+
// the metavar kinds... do they need to be Lrc<> instead of P<>? Or should
1514+
// `MatchedSingle` wrap its `ParseNtResult` in Lrc?
1515+
#[derive(Clone, Debug)]
15101516
pub enum ParseNtResult {
1511-
Nt(Nonterminal),
15121517
Tt(TokenTree),
1518+
1519+
Nt(Lrc<Nonterminal>),
15131520
}

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_ast::ptr::P;
22
use rustc_ast::token::{self, Delimiter, Nonterminal::*, NonterminalKind, Token};
33
use rustc_ast::HasTokens;
44
use rustc_ast_pretty::pprust;
5+
use rustc_data_structures::sync::Lrc;
56
use rustc_errors::IntoDiagnostic;
67
use rustc_errors::PResult;
78
use rustc_span::symbol::{kw, Ident};
@@ -197,7 +198,7 @@ impl<'a> Parser<'a> {
197198
);
198199
}
199200

200-
Ok(ParseNtResult::Nt(nt))
201+
Ok(ParseNtResult::Nt(Lrc::new(nt)))
201202
}
202203
}
203204

0 commit comments

Comments
 (0)