Skip to content

Commit 5cf5d9b

Browse files
authored
Unrolled build for #146802
Rollup merge of #146802 - joshtriplett:mbe-simplifications, r=petrochenkov mbe: Simplifications and refactoring A few simplifications and refactors in advance of other work. Macro metavariable expressions were using `Ident::as_str` and doing string comparisons; I converted them to use symbols. I factored out a function for transcribing a `ParseNtResult`, which will help separate the evaluation and transcription of future macro metavariable expressions.
2 parents 4056082 + a54111a commit 5cf5d9b

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

compiler/rustc_expand/src/mbe/metavar_expr.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast_pretty::pprust;
55
use rustc_errors::{Applicability, PResult};
66
use rustc_macros::{Decodable, Encodable};
77
use rustc_session::parse::ParseSess;
8-
use rustc_span::{Ident, Span, Symbol};
8+
use rustc_span::{Ident, Span, Symbol, sym};
99

1010
use crate::errors;
1111

@@ -69,15 +69,15 @@ impl MetaVarExpr {
6969
}
7070

7171
let mut iter = args.iter();
72-
let rslt = match ident.as_str() {
73-
"concat" => parse_concat(&mut iter, psess, outer_span, ident.span)?,
74-
"count" => parse_count(&mut iter, psess, ident.span)?,
75-
"ignore" => {
72+
let rslt = match ident.name {
73+
sym::concat => parse_concat(&mut iter, psess, outer_span, ident.span)?,
74+
sym::count => parse_count(&mut iter, psess, ident.span)?,
75+
sym::ignore => {
7676
eat_dollar(&mut iter, psess, ident.span)?;
7777
MetaVarExpr::Ignore(parse_ident(&mut iter, psess, ident.span)?)
7878
}
79-
"index" => MetaVarExpr::Index(parse_depth(&mut iter, psess, ident.span)?),
80-
"len" => MetaVarExpr::Len(parse_depth(&mut iter, psess, ident.span)?),
79+
sym::index => MetaVarExpr::Index(parse_depth(&mut iter, psess, ident.span)?),
80+
sym::len => MetaVarExpr::Len(parse_depth(&mut iter, psess, ident.span)?),
8181
_ => {
8282
let err = errors::MveUnrecognizedExpr {
8383
span: ident.span,
@@ -119,14 +119,13 @@ fn check_trailing_tokens<'psess>(
119119
}
120120

121121
// `None` for max indicates the arg count must be exact, `Some` indicates a range is accepted.
122-
let (min_or_exact_args, max_args) = match ident.as_str() {
123-
"concat" => panic!("concat takes unlimited tokens but didn't eat them all"),
124-
"ignore" => (1, None),
122+
let (min_or_exact_args, max_args) = match ident.name {
123+
sym::concat => panic!("concat takes unlimited tokens but didn't eat them all"),
124+
sym::ignore => (1, None),
125125
// 1 or 2 args
126-
"count" => (1, Some(2)),
126+
sym::count => (1, Some(2)),
127127
// 0 or 1 arg
128-
"index" => (0, Some(1)),
129-
"len" => (0, Some(1)),
128+
sym::index | sym::len => (0, Some(1)),
130129
other => unreachable!("unknown MVEs should be rejected earlier (got `{other}`)"),
131130
};
132131

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,19 @@ fn transcribe_metavar<'tx>(
375375
return Ok(());
376376
};
377377

378+
let MatchedSingle(pnr) = cur_matched else {
379+
// We were unable to descend far enough. This is an error.
380+
return Err(dcx.create_err(MacroVarStillRepeating { span: sp, ident }));
381+
};
382+
383+
transcribe_pnr(tscx, sp, pnr)
384+
}
385+
386+
fn transcribe_pnr<'tx>(
387+
tscx: &mut TranscrCtx<'tx, '_>,
388+
mut sp: Span,
389+
pnr: &ParseNtResult,
390+
) -> PResult<'tx, ()> {
378391
// We wrap the tokens in invisible delimiters, unless they are already wrapped
379392
// in invisible delimiters with the same `MetaVarKind`. Because some proc
380393
// macros can't handle multiple layers of invisible delimiters of the same
@@ -404,33 +417,33 @@ fn transcribe_metavar<'tx>(
404417
)
405418
};
406419

407-
let tt = match cur_matched {
408-
MatchedSingle(ParseNtResult::Tt(tt)) => {
420+
let tt = match pnr {
421+
ParseNtResult::Tt(tt) => {
409422
// `tt`s are emitted into the output stream directly as "raw tokens",
410423
// without wrapping them into groups. Other variables are emitted into
411424
// the output stream as groups with `Delimiter::Invisible` to maintain
412425
// parsing priorities.
413426
maybe_use_metavar_location(tscx.psess, &tscx.stack, sp, tt, &mut tscx.marker)
414427
}
415-
MatchedSingle(ParseNtResult::Ident(ident, is_raw)) => {
428+
ParseNtResult::Ident(ident, is_raw) => {
416429
tscx.marker.mark_span(&mut sp);
417430
with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
418431
let kind = token::NtIdent(*ident, *is_raw);
419432
TokenTree::token_alone(kind, sp)
420433
}
421-
MatchedSingle(ParseNtResult::Lifetime(ident, is_raw)) => {
434+
ParseNtResult::Lifetime(ident, is_raw) => {
422435
tscx.marker.mark_span(&mut sp);
423436
with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
424437
let kind = token::NtLifetime(*ident, *is_raw);
425438
TokenTree::token_alone(kind, sp)
426439
}
427-
MatchedSingle(ParseNtResult::Item(item)) => {
440+
ParseNtResult::Item(item) => {
428441
mk_delimited(item.span, MetaVarKind::Item, TokenStream::from_ast(item))
429442
}
430-
MatchedSingle(ParseNtResult::Block(block)) => {
443+
ParseNtResult::Block(block) => {
431444
mk_delimited(block.span, MetaVarKind::Block, TokenStream::from_ast(block))
432445
}
433-
MatchedSingle(ParseNtResult::Stmt(stmt)) => {
446+
ParseNtResult::Stmt(stmt) => {
434447
let stream = if let StmtKind::Empty = stmt.kind {
435448
// FIXME: Properly collect tokens for empty statements.
436449
TokenStream::token_alone(token::Semi, stmt.span)
@@ -439,10 +452,10 @@ fn transcribe_metavar<'tx>(
439452
};
440453
mk_delimited(stmt.span, MetaVarKind::Stmt, stream)
441454
}
442-
MatchedSingle(ParseNtResult::Pat(pat, pat_kind)) => {
455+
ParseNtResult::Pat(pat, pat_kind) => {
443456
mk_delimited(pat.span, MetaVarKind::Pat(*pat_kind), TokenStream::from_ast(pat))
444457
}
445-
MatchedSingle(ParseNtResult::Expr(expr, kind)) => {
458+
ParseNtResult::Expr(expr, kind) => {
446459
let (can_begin_literal_maybe_minus, can_begin_string_literal) = match &expr.kind {
447460
ExprKind::Lit(_) => (true, true),
448461
ExprKind::Unary(UnOp::Neg, e) if matches!(&e.kind, ExprKind::Lit(_)) => {
@@ -460,31 +473,27 @@ fn transcribe_metavar<'tx>(
460473
TokenStream::from_ast(expr),
461474
)
462475
}
463-
MatchedSingle(ParseNtResult::Literal(lit)) => {
476+
ParseNtResult::Literal(lit) => {
464477
mk_delimited(lit.span, MetaVarKind::Literal, TokenStream::from_ast(lit))
465478
}
466-
MatchedSingle(ParseNtResult::Ty(ty)) => {
479+
ParseNtResult::Ty(ty) => {
467480
let is_path = matches!(&ty.kind, TyKind::Path(None, _path));
468481
mk_delimited(ty.span, MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
469482
}
470-
MatchedSingle(ParseNtResult::Meta(attr_item)) => {
483+
ParseNtResult::Meta(attr_item) => {
471484
let has_meta_form = attr_item.meta_kind().is_some();
472485
mk_delimited(
473486
attr_item.span(),
474487
MetaVarKind::Meta { has_meta_form },
475488
TokenStream::from_ast(attr_item),
476489
)
477490
}
478-
MatchedSingle(ParseNtResult::Path(path)) => {
491+
ParseNtResult::Path(path) => {
479492
mk_delimited(path.span, MetaVarKind::Path, TokenStream::from_ast(path))
480493
}
481-
MatchedSingle(ParseNtResult::Vis(vis)) => {
494+
ParseNtResult::Vis(vis) => {
482495
mk_delimited(vis.span, MetaVarKind::Vis, TokenStream::from_ast(vis))
483496
}
484-
MatchedSeq(..) => {
485-
// We were unable to descend far enough. This is an error.
486-
return Err(dcx.create_err(MacroVarStillRepeating { span: sp, ident }));
487-
}
488497
};
489498

490499
tscx.result.push(tt);

0 commit comments

Comments
 (0)