Skip to main content

rustc_expand/mbe/
transcribe.rs

1use std::mem;
2
3use rustc_ast::token::{
4    self, Delimiter, IdentKind, InvisibleOrigin, Lit, LitKind, MetaVarKind, Token, TokenKind,
5};
6use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
7use rustc_ast::{ExprKind, StmtKind, TyKind, UnOp};
8use rustc_data_structures::fx::FxHashMap;
9use rustc_errors::{Diag, DiagCtxtHandle, PResult, listify, pluralize};
10use rustc_parse::lexer::nfc_normalize;
11use rustc_parse::parser::ParseNtResult;
12use rustc_session::parse::ParseSess;
13use rustc_span::hygiene::{LocalExpnId, Transparency};
14use rustc_span::{
15    BytePos, Ident, MacroRulesNormalizedIdent, Span, Symbol, SyntaxContext, kw, sym,
16    with_metavar_spans,
17};
18use smallvec::{SmallVec, smallvec};
19
20use crate::diagnostics::{
21    ConcatInvalidIdent, CountRepetitionMisplaced, InvalidIdentReason, MacroVarStillRepeating,
22    MetaVarsDifSeqMatchers, MustRepeatOnce, MveUnrecognizedVar, NoRepeatableVar,
23    NoSyntaxVarsExprRepeat, VarNoTypo, VarTypoSuggestionRepeatable, VarTypoSuggestionUnrepeatable,
24    VarTypoSuggestionUnrepeatableLabel,
25};
26use crate::mbe::macro_parser::NamedMatch;
27use crate::mbe::macro_parser::NamedMatch::*;
28use crate::mbe::metavar_expr::{MetaVarExprConcatElem, validate_ident_kind};
29use crate::mbe::{self, KleeneOp, MetaVarExpr};
30
31/// Context needed to perform transcription of metavariable expressions.
32struct TranscrCtx<'psess, 'itp> {
33    psess: &'psess ParseSess,
34
35    /// Map from metavars to matched tokens
36    interp: &'itp FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
37
38    /// Allow marking spans.
39    marker: Marker,
40
41    /// The stack of things yet to be completely expanded.
42    ///
43    /// We descend into the RHS (`src`), expanding things as we go. This stack contains the things
44    /// we have yet to expand/are still expanding. We start the stack off with the whole RHS. The
45    /// choice of spacing values doesn't matter.
46    stack: SmallVec<[Frame<'itp>; 1]>,
47
48    /// A stack of where we are in the repeat expansion.
49    ///
50    /// As we descend in the RHS, we will need to be able to match nested sequences of matchers.
51    /// `repeats` keeps track of where we are in matching at each level, with the last element
52    /// being the most deeply nested sequence. This is used as a stack.
53    repeats: Vec<(usize, usize)>,
54
55    /// The resulting token stream from the `TokenTree` we just finished processing.
56    ///
57    /// At the end, this will contain the full result of transcription, but at arbitrary points
58    /// during `transcribe`, `result` will contain subsets of the final result.
59    ///
60    /// Specifically, as we descend into each TokenTree, we will push the existing results onto the
61    /// `result_stack` and clear `results`. We will then produce the results of transcribing the
62    /// TokenTree into `results`. Then, as we unwind back out of the `TokenTree`, we will pop the
63    /// `result_stack` and append `results` too it to produce the new `results` up to that point.
64    ///
65    /// Thus, if we try to pop the `result_stack` and it is empty, we have reached the top-level
66    /// again, and we are done transcribing.
67    result: Vec<TokenTree>,
68
69    /// The in-progress `result` lives at the top of this stack. Each entered `TokenTree` adds a
70    /// new entry.
71    result_stack: Vec<Vec<TokenTree>>,
72}
73
74impl<'psess> TranscrCtx<'psess, '_> {
75    /// Span marked with the correct expansion and transparency.
76    fn visited_dspan(&mut self, dspan: DelimSpan) -> Span {
77        let mut span = dspan.entire();
78        self.marker.mark_span(&mut span);
79        span
80    }
81}
82
83/// A Marker adds the given mark to the syntax context.
84struct Marker {
85    expand_id: LocalExpnId,
86    transparency: Transparency,
87    // Most macro bodies have only one context. Keep that entry inline and
88    // allocate the map only for additional contexts in generated macros.
89    cache: Option<(SyntaxContext, SyntaxContext)>,
90    fallback_cache: FxHashMap<SyntaxContext, SyntaxContext>,
91}
92
93impl Marker {
94    /// Mark a span with the stored expansion ID and transparency.
95    fn mark_span(&mut self, span: &mut Span) {
96        // `apply_mark` is a relatively expensive operation, both due to taking hygiene lock, and
97        // by itself. All tokens in a macro body typically have the same syntactic context, unless
98        // it's some advanced case with macro-generated macros. So if we cache the marked version
99        // of that context once, we'll typically have a 100% cache hit rate after that.
100        *span = span.map_ctxt(|ctxt| match self.cache {
101            Some((original, marked)) if original == ctxt => marked,
102            None => {
103                let marked = ctxt.apply_mark(self.expand_id.to_expn_id(), self.transparency);
104                self.cache = Some((ctxt, marked));
105                marked
106            }
107            _ => *self
108                .fallback_cache
109                .entry(ctxt)
110                .or_insert_with(|| ctxt.apply_mark(self.expand_id.to_expn_id(), self.transparency)),
111        });
112    }
113}
114
115/// An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`).
116struct Frame<'a> {
117    tts: &'a [mbe::TokenTree],
118    idx: usize,
119    kind: FrameKind,
120}
121
122enum FrameKind {
123    Delimited { delim: Delimiter, span: DelimSpan, spacing: DelimSpacing },
124    Sequence { sep: Option<Token>, kleene_op: KleeneOp },
125}
126
127impl<'a> Frame<'a> {
128    fn new_delimited(src: &'a mbe::Delimited, span: DelimSpan, spacing: DelimSpacing) -> Frame<'a> {
129        Frame {
130            tts: &src.tts,
131            idx: 0,
132            kind: FrameKind::Delimited { delim: src.delim, span, spacing },
133        }
134    }
135
136    fn new_sequence(
137        src: &'a mbe::SequenceRepetition,
138        sep: Option<Token>,
139        kleene_op: KleeneOp,
140    ) -> Frame<'a> {
141        Frame { tts: &src.tts, idx: 0, kind: FrameKind::Sequence { sep, kleene_op } }
142    }
143}
144
145impl<'a> Iterator for Frame<'a> {
146    type Item = &'a mbe::TokenTree;
147
148    fn next(&mut self) -> Option<&'a mbe::TokenTree> {
149        let res = self.tts.get(self.idx);
150        self.idx += 1;
151        res
152    }
153}
154
155/// This can do Macro-By-Example transcription.
156/// - `interp` is a map of meta-variables to the tokens (non-terminals) they matched in the
157///   invocation. We are assuming we already know there is a match.
158/// - `src` is the RHS of the MBE, that is, the "example" we are filling in.
159///
160/// For example,
161///
162/// ```rust
163/// macro_rules! foo {
164///     ($id:ident) => { println!("{}", stringify!($id)); }
165/// }
166///
167/// foo!(bar);
168/// ```
169///
170/// `interp` would contain `$id => bar` and `src` would contain `println!("{}", stringify!($id));`.
171///
172/// `transcribe` would return a `TokenStream` containing `println!("{}", stringify!(bar));`.
173///
174/// Along the way, we do some additional error checking.
175pub(super) fn transcribe<'a>(
176    psess: &'a ParseSess,
177    interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
178    src: &mbe::Delimited,
179    src_span: DelimSpan,
180    transparency: Transparency,
181    expand_id: LocalExpnId,
182) -> PResult<'a, TokenStream> {
183    // Nothing for us to transcribe...
184    if src.tts.is_empty() {
185        return Ok(TokenStream::default());
186    }
187
188    let mut tscx = TranscrCtx {
189        psess,
190        interp,
191        marker: Marker { expand_id, transparency, cache: None, fallback_cache: Default::default() },
192        repeats: Vec::new(),
193        stack: {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(Frame::new_delimited(src, src_span,
                DelimSpacing::new(Spacing::Alone, Spacing::Alone)));
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [Frame::new_delimited(src, src_span,
                                DelimSpacing::new(Spacing::Alone, Spacing::Alone))])))
    }
}smallvec![Frame::new_delimited(
194            src,
195            src_span,
196            DelimSpacing::new(Spacing::Alone, Spacing::Alone)
197        )],
198        result: Vec::new(),
199        result_stack: Vec::new(),
200    };
201
202    loop {
203        // Look at the last frame on the stack.
204        // If it still has a TokenTree we have not looked at yet, use that tree.
205        let Some(tree) = tscx.stack.last_mut().unwrap().next() else {
206            // This else-case never produces a value for `tree` (it `continue`s or `return`s).
207
208            // Otherwise, if we have just reached the end of a sequence and we can keep repeating,
209            // go back to the beginning of the sequence.
210            let frame = tscx.stack.last_mut().unwrap();
211            if let FrameKind::Sequence { sep, .. } = &frame.kind {
212                let (repeat_idx, repeat_len) = tscx.repeats.last_mut().unwrap();
213                *repeat_idx += 1;
214                if repeat_idx < repeat_len {
215                    frame.idx = 0;
216                    if let Some(sep) = sep {
217                        tscx.result.push(TokenTree::Token(*sep, Spacing::Alone));
218                    }
219                    continue;
220                }
221            }
222
223            // We are done with the top of the stack. Pop it. Depending on what it was, we do
224            // different things. Note that the outermost item must be the delimited, wrapped RHS
225            // that was passed in originally to `transcribe`.
226            match tscx.stack.pop().unwrap().kind {
227                // Done with a sequence. Pop from repeats.
228                FrameKind::Sequence { .. } => {
229                    tscx.repeats.pop();
230                }
231
232                // We are done processing a Delimited. If this is the top-level delimited, we are
233                // done. Otherwise, we unwind the result_stack to append what we have produced to
234                // any previous results.
235                FrameKind::Delimited { delim, span, mut spacing, .. } => {
236                    // Hack to force-insert a space after `]` in certain case.
237                    // See discussion of the `hex-literal` crate in #114571.
238                    if delim == Delimiter::Bracket {
239                        spacing.close = Spacing::Alone;
240                    }
241                    if tscx.result_stack.is_empty() {
242                        // No results left to compute! We are back at the top-level.
243                        return Ok(TokenStream::new(tscx.result));
244                    }
245
246                    // Step back into the parent Delimited.
247                    let tree =
248                        TokenTree::Delimited(span, spacing, delim, TokenStream::new(tscx.result));
249                    tscx.result = tscx.result_stack.pop().unwrap();
250                    tscx.result.push(tree);
251                }
252            }
253            continue;
254        };
255
256        // At this point, we know we are in the middle of a TokenTree (the last one on `stack`).
257        // `tree` contains the next `TokenTree` to be processed.
258        match tree {
259            // Replace the sequence with its expansion.
260            seq @ mbe::TokenTree::Sequence(_, seq_rep) => {
261                transcribe_sequence(&mut tscx, seq, seq_rep, interp)?;
262            }
263
264            // Replace the meta-var with the matched token tree from the invocation.
265            &mbe::TokenTree::MetaVar(sp, original_ident) => {
266                transcribe_metavar(&mut tscx, sp, original_ident)?;
267            }
268
269            // Replace meta-variable expressions with the result of their expansion.
270            mbe::TokenTree::MetaVarExpr(dspan, expr) => {
271                transcribe_metavar_expr(&mut tscx, *dspan, expr)?;
272            }
273
274            // If we are entering a new delimiter, we push its contents to the `stack` to be
275            // processed, and we push all of the currently produced results to the `result_stack`.
276            // We will produce all of the results of the inside of the `Delimited` and then we will
277            // jump back out of the Delimited, pop the result_stack and add the new results back to
278            // the previous results (from outside the Delimited).
279            &mbe::TokenTree::Delimited(mut span, ref spacing, ref delimited) => {
280                tscx.marker.mark_span(&mut span.open);
281                tscx.marker.mark_span(&mut span.close);
282                tscx.stack.push(Frame::new_delimited(delimited, span, *spacing));
283                tscx.result_stack.push(mem::take(&mut tscx.result));
284            }
285
286            // Nothing much to do here. Just push the token to the result, being careful to
287            // preserve syntax context.
288            &mbe::TokenTree::Token(mut token) => {
289                tscx.marker.mark_span(&mut token.span);
290                if let token::NtIdent(ident, _) | token::NtLifetime(ident, _) = &mut token.kind {
291                    tscx.marker.mark_span(&mut ident.span);
292                }
293                let tt = TokenTree::Token(token, Spacing::Alone);
294                tscx.result.push(tt);
295            }
296
297            // There should be no meta-var declarations in the invocation of a macro.
298            mbe::TokenTree::MetaVarDecl { .. } => {
    ::core::panicking::panic_fmt(format_args!("unexpected `TokenTree::MetaVarDecl`"));
}panic!("unexpected `TokenTree::MetaVarDecl`"),
299        }
300    }
301}
302
303/// Turn `$(...)*` sequences into tokens.
304fn transcribe_sequence<'tx, 'itp>(
305    tscx: &mut TranscrCtx<'tx, 'itp>,
306    seq: &mbe::TokenTree,
307    seq_rep: &'itp mbe::SequenceRepetition,
308    // Used only for better diagnostics in the face of typos.
309    interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
310) -> PResult<'tx, ()> {
311    let dcx = tscx.psess.dcx();
312
313    // We are descending into a sequence. We first make sure that the matchers in the RHS
314    // and the matches in `interp` have the same shape. Otherwise, either the caller or the
315    // macro writer has made a mistake.
316    match lockstep_iter_size(seq, tscx.interp, &tscx.repeats) {
317        LockstepIterSize::Unconstrained => {
318            let mut repeatables = Vec::new();
319            let mut non_repeatables = Vec::new();
320
321            #[allow(rustc::potential_query_instability)]
322            for (name, matcher) in interp.iter() {
323                if matcher.is_repeatable() {
324                    repeatables.push(name);
325                } else {
326                    non_repeatables.push(name);
327                }
328            }
329
330            let repeatable_names: Vec<Symbol> =
331                repeatables.iter().map(|&name| name.symbol()).collect();
332            let non_repeatable_names: Vec<Symbol> =
333                non_repeatables.iter().map(|&name| name.symbol()).collect();
334            let mut meta_vars = ::alloc::vec::Vec::new()vec![];
335            seq.meta_vars(&mut meta_vars);
336            let mut typo_repeatable = None;
337            let mut typo_unrepeatable = None;
338            let mut typo_unrepeatable_label = None;
339            let mut var_no_typo = None;
340            let mut no_repeatable_var = None;
341
342            for ident in meta_vars {
343                if let Some(name) = rustc_span::edit_distance::find_best_match_for_name(
344                    &repeatable_names[..],
345                    ident.name,
346                    None,
347                ) {
348                    typo_repeatable = Some(VarTypoSuggestionRepeatable { span: ident.span, name });
349                } else if let Some(name) = rustc_span::edit_distance::find_best_match_for_name(
350                    &non_repeatable_names[..],
351                    ident.name,
352                    None,
353                ) {
354                    typo_unrepeatable = Some(VarTypoSuggestionUnrepeatable { span: ident.span });
355                    if let Some(&orig_ident) = non_repeatables.iter().find(|n| n.symbol() == name) {
356                        typo_unrepeatable_label = Some(VarTypoSuggestionUnrepeatableLabel {
357                            span: orig_ident.ident().span,
358                        });
359                    }
360                } else {
361                    if !repeatable_names.is_empty()
362                        && let Some(msg) = listify(&repeatable_names, |s| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`${0}`", s))
    })format!("`${s}`"))
363                    {
364                        var_no_typo = Some(VarNoTypo { span: ident.span, msg });
365                    } else {
366                        no_repeatable_var = Some(NoRepeatableVar { span: ident.span });
367                    }
368                }
369            }
370            return Err(dcx.create_err(NoSyntaxVarsExprRepeat {
371                span: seq.span(),
372                typo_unrepeatable,
373                typo_repeatable,
374                typo_unrepeatable_label,
375                var_no_typo,
376                no_repeatable_var,
377            }));
378        }
379
380        LockstepIterSize::Contradiction(msg) => {
381            // FIXME: this really ought to be caught at macro definition time... It
382            // happens when two meta-variables are used in the same repetition in a
383            // sequence, but they come from different sequence matchers and repeat
384            // different amounts.
385            return Err(dcx.create_err(MetaVarsDifSeqMatchers { span: seq.span(), msg }));
386        }
387
388        LockstepIterSize::Constraint(len, _) => {
389            // We do this to avoid an extra clone above. We know that this is a
390            // sequence already.
391            let mbe::TokenTree::Sequence(sp, seq) = seq else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
392
393            // Is the repetition empty?
394            if len == 0 {
395                if seq.kleene.op == KleeneOp::OneOrMore {
396                    // FIXME: this really ought to be caught at macro definition
397                    // time... It happens when the Kleene operator in the matcher and
398                    // the body for the same meta-variable do not match.
399                    return Err(dcx.create_err(MustRepeatOnce { span: sp.entire() }));
400                }
401            } else {
402                // 0 is the initial counter (we have done 0 repetitions so far). `len`
403                // is the total number of repetitions we should generate.
404                tscx.repeats.push((0, len));
405
406                // The first time we encounter the sequence we push it to the stack. It
407                // then gets reused (see the beginning of the loop) until we are done
408                // repeating.
409                tscx.stack.push(Frame::new_sequence(seq_rep, seq.separator, seq.kleene.op));
410            }
411        }
412    }
413
414    Ok(())
415}
416
417/// Find the matched nonterminal from the macro invocation, and use it to replace
418/// the meta-var.
419///
420/// We use `Spacing::Alone` everywhere here, because that's the conservative choice
421/// and spacing of declarative macros is tricky. E.g. in this macro:
422/// ```
423/// macro_rules! idents {
424///     ($($a:ident,)*) => { stringify!($($a)*) }
425/// }
426/// ```
427/// `$a` has no whitespace after it and will be marked `JointHidden`. If you then
428/// call `idents!(x,y,z,)`, each of `x`, `y`, and `z` will be marked as `Joint`. So
429/// if you choose to use `$x`'s spacing or the identifier's spacing, you'll end up
430/// producing "xyz", which is bad because it effectively merges tokens.
431/// `Spacing::Alone` is the safer option. Fortunately, `space_between` will avoid
432/// some of the unnecessary whitespace.
433fn transcribe_metavar<'tx>(
434    tscx: &mut TranscrCtx<'tx, '_>,
435    mut sp: Span,
436    mut original_ident: Ident,
437) -> PResult<'tx, ()> {
438    let dcx = tscx.psess.dcx();
439
440    let ident = MacroRulesNormalizedIdent::new(original_ident);
441    let Some(cur_matched) = lookup_cur_matched(ident, tscx.interp, &tscx.repeats) else {
442        // If we aren't able to match the meta-var, we push it back into the result but
443        // with modified syntax context. (I believe this supports nested macros).
444        tscx.marker.mark_span(&mut sp);
445        tscx.marker.mark_span(&mut original_ident.span);
446        tscx.result.push(TokenTree::token_joint_hidden(token::Dollar, sp));
447        tscx.result.push(TokenTree::Token(Token::from_ast_ident(original_ident), Spacing::Alone));
448        return Ok(());
449    };
450
451    let MatchedSingle(pnr) = cur_matched else {
452        // We were unable to descend far enough. This is an error.
453        return Err(dcx.create_err(MacroVarStillRepeating { span: sp, ident }));
454    };
455
456    transcribe_pnr(tscx, sp, pnr)
457}
458
459fn transcribe_pnr<'tx>(
460    tscx: &mut TranscrCtx<'tx, '_>,
461    mut sp: Span,
462    pnr: &ParseNtResult,
463) -> PResult<'tx, ()> {
464    // We wrap the tokens in invisible delimiters, unless they are already wrapped
465    // in invisible delimiters with the same `MetaVarKind`. Because some proc
466    // macros can't handle multiple layers of invisible delimiters of the same
467    // `MetaVarKind`. This loses some span info, though it hopefully won't matter.
468    let mut mk_delimited = |mk_span, mv_kind, mut stream: TokenStream| {
469        if stream.len() == 1 {
470            let tree = stream.iter().next().unwrap();
471            if let TokenTree::Delimited(_, _, delim, inner) = tree
472                && let Delimiter::Invisible(InvisibleOrigin::MetaVar(mvk)) = delim
473                && mv_kind == *mvk
474            {
475                stream = inner.clone();
476            }
477        }
478
479        // Emit as a token stream within `Delimiter::Invisible` to maintain
480        // parsing priorities.
481        tscx.marker.mark_span(&mut sp);
482        with_metavar_spans(|mspans| mspans.insert(mk_span, sp));
483        // Both the open delim and close delim get the same span, which covers the
484        // `$foo` in the decl macro RHS.
485        TokenTree::Delimited(
486            DelimSpan::from_single(sp),
487            DelimSpacing::new(Spacing::Alone, Spacing::Alone),
488            Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind)),
489            stream,
490        )
491    };
492
493    let tt = match pnr {
494        ParseNtResult::Tt(tt) => {
495            // `tt`s are emitted into the output stream directly as "raw tokens",
496            // without wrapping them into groups. Other variables are emitted into
497            // the output stream as groups with `Delimiter::Invisible` to maintain
498            // parsing priorities.
499            maybe_use_metavar_location(tscx.psess, &tscx.stack, sp, tt, &mut tscx.marker)
500        }
501        ParseNtResult::Ident(ident, kind) => {
502            tscx.marker.mark_span(&mut sp);
503            with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
504            let kind = token::NtIdent(*ident, *kind);
505            TokenTree::token_alone(kind, sp)
506        }
507        ParseNtResult::Lifetime(ident, is_raw) => {
508            tscx.marker.mark_span(&mut sp);
509            with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
510            let kind = token::NtLifetime(*ident, *is_raw);
511            TokenTree::token_alone(kind, sp)
512        }
513        ParseNtResult::Item(item) => {
514            mk_delimited(item.span, MetaVarKind::Item, TokenStream::from_ast(item))
515        }
516        ParseNtResult::Block(block) => {
517            mk_delimited(block.node.span, MetaVarKind::Block, TokenStream::from_ast(block))
518        }
519        ParseNtResult::Stmt(stmt) => {
520            let stream = if let StmtKind::Empty = stmt.kind {
521                // FIXME: Properly collect tokens for empty statements.
522                TokenStream::token_alone(token::Semi, stmt.span)
523            } else {
524                TokenStream::from_ast(stmt)
525            };
526            mk_delimited(stmt.span, MetaVarKind::Stmt, stream)
527        }
528        ParseNtResult::Pat(pat, pat_kind) => {
529            mk_delimited(pat.node.span, MetaVarKind::Pat(*pat_kind), TokenStream::from_ast(pat))
530        }
531        ParseNtResult::Expr(expr, kind) => {
532            let (can_begin_literal_maybe_minus, can_begin_string_literal) = match &expr.kind {
533                ExprKind::Lit(_) => (true, true),
534                ExprKind::Unary(UnOp::Neg, e) if #[allow(non_exhaustive_omitted_patterns)] match &e.kind {
    ExprKind::Lit(_) => true,
    _ => false,
}matches!(&e.kind, ExprKind::Lit(_)) => {
535                    (true, false)
536                }
537                _ => (false, false),
538            };
539            mk_delimited(
540                expr.span,
541                MetaVarKind::Expr {
542                    kind: *kind,
543                    can_begin_literal_maybe_minus,
544                    can_begin_string_literal,
545                },
546                TokenStream::from_ast(expr),
547            )
548        }
549        ParseNtResult::Literal(lit) => {
550            mk_delimited(lit.span, MetaVarKind::Literal, TokenStream::from_ast(lit))
551        }
552        ParseNtResult::Ty(ty) => {
553            let is_path = #[allow(non_exhaustive_omitted_patterns)] match &ty.node.kind {
    TyKind::Path(None, _path) => true,
    _ => false,
}matches!(&ty.node.kind, TyKind::Path(None, _path));
554            mk_delimited(ty.node.span, MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
555        }
556        ParseNtResult::Meta(attr_item) => {
557            let has_meta_form = attr_item.node.meta_kind().is_some();
558            mk_delimited(
559                attr_item.node.span,
560                MetaVarKind::Meta { has_meta_form },
561                TokenStream::from_ast(attr_item),
562            )
563        }
564        ParseNtResult::Path(path) => {
565            mk_delimited(path.node.span, MetaVarKind::Path, TokenStream::from_ast(path))
566        }
567        ParseNtResult::Vis(vis) => {
568            mk_delimited(vis.node.span, MetaVarKind::Vis, TokenStream::from_ast(vis))
569        }
570        ParseNtResult::Guard(guard) => {
571            // FIXME(macro_guard_matcher):
572            // Perhaps it would be better to treat the leading `if` as part of `ast::Guard` during parsing?
573            // Currently they are separate, but in macros we match and emit the leading `if` for `:guard` matchers, which creates some inconsistency.
574
575            let leading_if_span =
576                guard.span_with_leading_if.with_hi(guard.span_with_leading_if.lo() + BytePos(2));
577            let ts = std::iter::once(TokenTree::token_alone(
578                token::Ident(kw::If, IdentKind::Normal),
579                leading_if_span,
580            ))
581            .chain(TokenStream::from_ast(&guard.cond).iter().cloned())
582            .collect();
583
584            mk_delimited(guard.span_with_leading_if, MetaVarKind::Guard, ts)
585        }
586    };
587
588    tscx.result.push(tt);
589    Ok(())
590}
591
592/// Turn `${expr(...)}` metavariable expressionss into tokens.
593fn transcribe_metavar_expr<'tx>(
594    tscx: &mut TranscrCtx<'tx, '_>,
595    dspan: DelimSpan,
596    expr: &MetaVarExpr,
597) -> PResult<'tx, ()> {
598    let dcx = tscx.psess.dcx();
599    let tt = match *expr {
600        MetaVarExpr::ConcatIdent(ref elements) => metavar_expr_concat_ident(tscx, dspan, elements)?,
601        MetaVarExpr::ConcatStr(ref elements) => metavar_expr_concat_str(tscx, dspan, elements)?,
602        MetaVarExpr::Count(original_ident, depth) => {
603            let matched = matched_from_ident(dcx, original_ident, tscx.interp)?;
604            let count = count_repetitions(dcx, depth, matched, &tscx.repeats, &dspan)?;
605            TokenTree::token_alone(
606                TokenKind::lit(token::Integer, sym::integer(count), None),
607                tscx.visited_dspan(dspan),
608            )
609        }
610        MetaVarExpr::Ignore(original_ident) => {
611            // Used to ensure that `original_ident` is present in the LHS
612            let _ = matched_from_ident(dcx, original_ident, tscx.interp)?;
613            return Ok(());
614        }
615        MetaVarExpr::Index(depth) => match tscx.repeats.iter().nth_back(depth) {
616            Some((index, _)) => TokenTree::token_alone(
617                TokenKind::lit(token::Integer, sym::integer(*index), None),
618                tscx.visited_dspan(dspan),
619            ),
620            None => {
621                return Err(out_of_bounds_err(dcx, tscx.repeats.len(), dspan.entire(), "index"));
622            }
623        },
624        MetaVarExpr::Len(depth) => match tscx.repeats.iter().nth_back(depth) {
625            Some((_, length)) => TokenTree::token_alone(
626                TokenKind::lit(token::Integer, sym::integer(*length), None),
627                tscx.visited_dspan(dspan),
628            ),
629            None => {
630                return Err(out_of_bounds_err(dcx, tscx.repeats.len(), dspan.entire(), "len"));
631            }
632        },
633    };
634    tscx.result.push(tt);
635    Ok(())
636}
637
638/// Handle the `${concat(...)}` metavariable expression.
639fn metavar_expr_concat_ident<'tx>(
640    tscx: &mut TranscrCtx<'tx, '_>,
641    dspan: DelimSpan,
642    elements: &[MetaVarExprConcatElem],
643) -> PResult<'tx, TokenTree> {
644    let (symbol, concatenated_span) = metavar_expr_concat(tscx, dspan, elements)?;
645    if !rustc_lexer::is_ident(symbol.as_str()) {
646        return Err(tscx.psess.dcx().create_err(ConcatInvalidIdent {
647            span: concatenated_span,
648            reason: InvalidIdentReason::new(symbol),
649        }));
650    }
651    tscx.psess.symbol_gallery.insert(symbol, concatenated_span);
652
653    // The current implementation marks the span as coming from the macro regardless of
654    // contexts of the concatenated identifiers but this behavior may change in the
655    // future.
656    Ok(TokenTree::Token(
657        Token::from_ast_ident(Ident::new(symbol, concatenated_span)),
658        Spacing::Alone,
659    ))
660}
661
662/// Handle the `${concat_str(...)}` metavariable expression.
663fn metavar_expr_concat_str<'tx>(
664    tscx: &mut TranscrCtx<'tx, '_>,
665    dspan: DelimSpan,
666    elements: &[MetaVarExprConcatElem],
667) -> PResult<'tx, TokenTree> {
668    let (symbol, concatenated_span) = metavar_expr_concat(tscx, dspan, elements)?;
669
670    // The current implementation marks the span as coming from the macro regardless of
671    // contexts of the concatenated identifiers but this behavior may change in the
672    // future.
673    Ok(TokenTree::Token(
674        Token::new(TokenKind::lit(LitKind::Str, symbol, None), concatenated_span),
675        Spacing::Alone,
676    ))
677}
678
679/// Shared logic for concat/concat_str metavariable expressions
680fn metavar_expr_concat<'tx>(
681    tscx: &mut TranscrCtx<'tx, '_>,
682    dspan: DelimSpan,
683    elements: &[MetaVarExprConcatElem],
684) -> PResult<'tx, (Symbol, Span)> {
685    let dcx = tscx.psess.dcx();
686    let mut concatenated = String::new();
687    for element in elements {
688        let symbol = match element {
689            MetaVarExprConcatElem::Ident(elem) => elem.name,
690            MetaVarExprConcatElem::Literal(elem) => *elem,
691            MetaVarExprConcatElem::Var(ident) => {
692                let key = MacroRulesNormalizedIdent::new(*ident);
693                match lookup_cur_matched(key, tscx.interp, &tscx.repeats) {
694                    Some(NamedMatch::MatchedSingle(pnr)) => {
695                        extract_symbol_from_pnr(dcx, pnr, ident.span)?
696                    }
697                    Some(NamedMatch::MatchedSeq(..)) => {
698                        return Err(dcx.struct_span_err(
699                            ident.span,
700                            "`${concat(...)}` variable is still repeating at this depth",
701                        ));
702                    }
703                    None => {
704                        return Err(dcx.create_err(MveUnrecognizedVar { span: ident.span, key }));
705                    }
706                }
707            }
708        };
709        concatenated.push_str(symbol.as_str());
710    }
711    let symbol = nfc_normalize(&concatenated);
712    let concatenated_span = tscx.visited_dspan(dspan);
713    Ok((symbol, concatenated_span))
714}
715
716/// Store the metavariable span for this original span into a side table.
717/// FIXME: Try to put the metavariable span into `SpanData` instead of a side table (#118517).
718/// An optimal encoding for inlined spans will need to be selected to minimize regressions.
719/// The side table approach is relatively good, but not perfect due to collisions.
720/// In particular, collisions happen when token is passed as an argument through several macro
721/// calls, like in recursive macros.
722/// The old heuristic below is used to improve spans in case of collisions, but diagnostics are
723/// still degraded sometimes in those cases.
724///
725/// The old heuristic:
726///
727/// Usually metavariables `$var` produce interpolated tokens, which have an additional place for
728/// keeping both the original span and the metavariable span. For `tt` metavariables that's not the
729/// case however, and there's no place for keeping a second span. So we try to give the single
730/// produced span a location that would be most useful in practice (the hygiene part of the span
731/// must not be changed).
732///
733/// Different locations are useful for different purposes:
734/// - The original location is useful when we need to report a diagnostic for the original token in
735///   isolation, without combining it with any surrounding tokens. This case occurs, but it is not
736///   very common in practice.
737/// - The metavariable location is useful when we need to somehow combine the token span with spans
738///   of its surrounding tokens. This is the most common way to use token spans.
739///
740/// So this function replaces the original location with the metavariable location in all cases
741/// except these two:
742/// - The metavariable is an element of undelimited sequence `$($tt)*`.
743///   These are typically used for passing larger amounts of code, and tokens in that code usually
744///   combine with each other and not with tokens outside of the sequence.
745/// - The metavariable span comes from a different crate, then we prefer the more local span.
746fn maybe_use_metavar_location(
747    psess: &ParseSess,
748    stack: &[Frame<'_>],
749    mut metavar_span: Span,
750    orig_tt: &TokenTree,
751    marker: &mut Marker,
752) -> TokenTree {
753    let undelimited_seq = #[allow(non_exhaustive_omitted_patterns)] match stack.last() {
    Some(Frame {
        tts: [_],
        kind: FrameKind::Sequence {
            sep: None, kleene_op: KleeneOp::ZeroOrMore | KleeneOp::OneOrMore,
            ..
            }, .. }) => true,
    _ => false,
}matches!(
754        stack.last(),
755        Some(Frame {
756            tts: [_],
757            kind: FrameKind::Sequence {
758                sep: None,
759                kleene_op: KleeneOp::ZeroOrMore | KleeneOp::OneOrMore,
760                ..
761            },
762            ..
763        })
764    );
765    if undelimited_seq {
766        // Do not record metavar spans for tokens from undelimited sequences, for perf reasons.
767        return orig_tt.clone();
768    }
769
770    marker.mark_span(&mut metavar_span);
771    let no_collision = match orig_tt {
772        TokenTree::Token(token, ..) => {
773            with_metavar_spans(|mspans| mspans.insert(token.span, metavar_span))
774        }
775        TokenTree::Delimited(dspan, ..) => with_metavar_spans(|mspans| {
776            mspans.insert(dspan.open, metavar_span)
777                && mspans.insert(dspan.close, metavar_span)
778                && mspans.insert(dspan.entire(), metavar_span)
779        }),
780    };
781    if no_collision || psess.source_map().is_imported(metavar_span) {
782        return orig_tt.clone();
783    }
784
785    // Setting metavar spans for the heuristic spans gives better opportunities for combining them
786    // with neighboring spans even despite their different syntactic contexts.
787    match orig_tt {
788        TokenTree::Token(Token { kind, span }, spacing) => {
789            let span = metavar_span.with_ctxt(span.ctxt());
790            with_metavar_spans(|mspans| mspans.insert(span, metavar_span));
791            TokenTree::Token(Token { kind: *kind, span }, *spacing)
792        }
793        TokenTree::Delimited(dspan, dspacing, delimiter, tts) => {
794            let open = metavar_span.with_ctxt(dspan.open.ctxt());
795            let close = metavar_span.with_ctxt(dspan.close.ctxt());
796            with_metavar_spans(|mspans| {
797                mspans.insert(open, metavar_span) && mspans.insert(close, metavar_span)
798            });
799            let dspan = DelimSpan::from_pair(open, close);
800            TokenTree::Delimited(dspan, *dspacing, *delimiter, tts.clone())
801        }
802    }
803}
804
805/// Lookup the meta-var named `ident` and return the matched token tree from the invocation using
806/// the set of matches `interpolations`.
807///
808/// See the definition of `repeats` in the `transcribe` function. `repeats` is used to descend
809/// into the right place in nested matchers. If we attempt to descend too far, the macro writer has
810/// made a mistake, and we return `None`.
811fn lookup_cur_matched<'a>(
812    ident: MacroRulesNormalizedIdent,
813    interpolations: &'a FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
814    repeats: &[(usize, usize)],
815) -> Option<&'a NamedMatch> {
816    interpolations.get(&ident).map(|mut matched| {
817        for &(idx, _) in repeats {
818            match matched {
819                MatchedSingle(_) => break,
820                MatchedSeq(ads) => matched = ads.get(idx).unwrap(),
821            }
822        }
823
824        matched
825    })
826}
827
828/// An accumulator over a TokenTree to be used with `fold`. During transcription, we need to make
829/// sure that the size of each sequence and all of its nested sequences are the same as the sizes
830/// of all the matched (nested) sequences in the macro invocation. If they don't match, somebody
831/// has made a mistake (either the macro writer or caller).
832#[derive(#[automatically_derived]
impl ::core::clone::Clone for LockstepIterSize {
    #[inline]
    fn clone(&self) -> Self {
        match self {
            Self::Unconstrained => Self::Unconstrained,
            Self::Constraint(__self_0, __self_1) =>
                Self::Constraint(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            Self::Contradiction(__self_0) =>
                Self::Contradiction(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone)]
833enum LockstepIterSize {
834    /// No constraints on length of matcher. This is true for any TokenTree variants except a
835    /// `MetaVar` with an actual `MatchedSeq` (as opposed to a `MatchedNonterminal`).
836    Unconstrained,
837
838    /// A `MetaVar` with an actual `MatchedSeq`. The length of the match and the name of the
839    /// meta-var are returned.
840    Constraint(usize, MacroRulesNormalizedIdent),
841
842    /// Two `Constraint`s on the same sequence had different lengths. This is an error.
843    Contradiction(String),
844}
845
846impl LockstepIterSize {
847    /// Find incompatibilities in matcher/invocation sizes.
848    /// - `Unconstrained` is compatible with everything.
849    /// - `Contradiction` is incompatible with everything.
850    /// - `Constraint(len)` is only compatible with other constraints of the same length.
851    fn with(self, other: LockstepIterSize) -> LockstepIterSize {
852        match self {
853            LockstepIterSize::Unconstrained => other,
854            LockstepIterSize::Contradiction(_) => self,
855            LockstepIterSize::Constraint(l_len, l_id) => match other {
856                LockstepIterSize::Unconstrained => self,
857                LockstepIterSize::Contradiction(_) => other,
858                LockstepIterSize::Constraint(r_len, _) if l_len == r_len => self,
859                LockstepIterSize::Constraint(r_len, r_id) => {
860                    let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("meta-variable `{0}` repeats {1} time{2}, but `{3}` repeats {4} time{5}",
                l_id, l_len, if l_len == 1 { "" } else { "s" }, r_id, r_len,
                if r_len == 1 { "" } else { "s" }))
    })format!(
861                        "meta-variable `{}` repeats {} time{}, but `{}` repeats {} time{}",
862                        l_id,
863                        l_len,
864                        pluralize!(l_len),
865                        r_id,
866                        r_len,
867                        pluralize!(r_len),
868                    );
869                    LockstepIterSize::Contradiction(msg)
870                }
871            },
872        }
873    }
874}
875
876/// Given a `tree`, make sure that all sequences have the same length as the matches for the
877/// appropriate meta-vars in `interpolations`.
878///
879/// Note that if `repeats` does not match the exact correct depth of a meta-var,
880/// `lookup_cur_matched` will return `None`, which is why this still works even in the presence of
881/// multiple nested matcher sequences.
882///
883/// Example: `$($($x $y)+*);+` -- we need to make sure that `x` and `y` repeat the same amount as
884/// each other at the given depth when the macro was invoked. If they don't it might mean they were
885/// declared at depths which weren't equal or there was a compiler bug. For example, if we have 3 repetitions of
886/// the outer sequence and 4 repetitions of the inner sequence for `x`, we should have the same for
887/// `y`; otherwise, we can't transcribe them both at the given depth.
888fn lockstep_iter_size(
889    tree: &mbe::TokenTree,
890    interpolations: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
891    repeats: &[(usize, usize)],
892) -> LockstepIterSize {
893    use mbe::TokenTree;
894    match tree {
895        TokenTree::Delimited(.., delimited) => {
896            delimited.tts.iter().fold(LockstepIterSize::Unconstrained, |size, tt| {
897                size.with(lockstep_iter_size(tt, interpolations, repeats))
898            })
899        }
900        TokenTree::Sequence(_, seq) => {
901            seq.tts.iter().fold(LockstepIterSize::Unconstrained, |size, tt| {
902                size.with(lockstep_iter_size(tt, interpolations, repeats))
903            })
904        }
905        TokenTree::MetaVar(_, name) | TokenTree::MetaVarDecl { name, .. } => {
906            let name = MacroRulesNormalizedIdent::new(*name);
907            match lookup_cur_matched(name, interpolations, repeats) {
908                Some(matched) => match matched {
909                    MatchedSingle(_) => LockstepIterSize::Unconstrained,
910                    MatchedSeq(ads) => LockstepIterSize::Constraint(ads.len(), name),
911                },
912                _ => LockstepIterSize::Unconstrained,
913            }
914        }
915        TokenTree::MetaVarExpr(_, expr) => {
916            expr.for_each_metavar(LockstepIterSize::Unconstrained, |lis, ident| {
917                lis.with(lockstep_iter_size(
918                    &TokenTree::MetaVar(ident.span, *ident),
919                    interpolations,
920                    repeats,
921                ))
922            })
923        }
924        TokenTree::Token(..) => LockstepIterSize::Unconstrained,
925    }
926}
927
928/// Used solely by the `count` meta-variable expression, counts the outermost repetitions at a
929/// given optional nested depth.
930///
931/// For example, a macro parameter of `$( { $( $foo:ident ),* } )*` called with `{ a, b } { c }`:
932///
933/// * `[ $( ${count(foo)} ),* ]` will return [2, 1] with a, b = 2 and c = 1
934/// * `[ $( ${count(foo, 0)} ),* ]` will be the same as `[ $( ${count(foo)} ),* ]`
935/// * `[ $( ${count(foo, 1)} ),* ]` will return an error because `${count(foo, 1)}` is
936///   declared inside a single repetition and the index `1` implies two nested repetitions.
937fn count_repetitions<'dx>(
938    dcx: DiagCtxtHandle<'dx>,
939    depth_user: usize,
940    mut matched: &NamedMatch,
941    repeats: &[(usize, usize)],
942    sp: &DelimSpan,
943) -> PResult<'dx, usize> {
944    // Recursively count the number of matches in `matched` at given depth
945    // (or at the top-level of `matched` if no depth is given).
946    fn count<'a>(depth_curr: usize, depth_max: usize, matched: &NamedMatch) -> PResult<'a, usize> {
947        match matched {
948            MatchedSingle(_) => Ok(1),
949            MatchedSeq(named_matches) => {
950                if depth_curr == depth_max {
951                    Ok(named_matches.len())
952                } else {
953                    named_matches.iter().map(|elem| count(depth_curr + 1, depth_max, elem)).sum()
954                }
955            }
956        }
957    }
958
959    /// Maximum depth
960    fn depth(counter: usize, matched: &NamedMatch) -> usize {
961        match matched {
962            MatchedSingle(_) => counter,
963            MatchedSeq(named_matches) => {
964                let rslt = counter + 1;
965                if let Some(elem) = named_matches.first() { depth(rslt, elem) } else { rslt }
966            }
967        }
968    }
969
970    let depth_max = depth(0, matched)
971        .checked_sub(1)
972        .and_then(|el| el.checked_sub(repeats.len()))
973        .unwrap_or_default();
974    if depth_user > depth_max {
975        return Err(out_of_bounds_err(dcx, depth_max + 1, sp.entire(), "count"));
976    }
977
978    // `repeats` records all of the nested levels at which we are currently
979    // matching meta-variables. The meta-var-expr `count($x)` only counts
980    // matches that occur in this "subtree" of the `NamedMatch` where we
981    // are currently transcribing, so we need to descend to that subtree
982    // before we start counting. `matched` contains the various levels of the
983    // tree as we descend, and its final value is the subtree we are currently at.
984    for &(idx, _) in repeats {
985        if let MatchedSeq(ads) = matched {
986            matched = &ads[idx];
987        }
988    }
989
990    if let MatchedSingle(_) = matched {
991        return Err(dcx.create_err(CountRepetitionMisplaced { span: sp.entire() }));
992    }
993
994    count(depth_user, depth_max, matched)
995}
996
997/// Returns a `NamedMatch` item declared on the LHS given an arbitrary [Ident]
998fn matched_from_ident<'ctx, 'interp, 'rslt>(
999    dcx: DiagCtxtHandle<'ctx>,
1000    ident: Ident,
1001    interp: &'interp FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
1002) -> PResult<'ctx, &'rslt NamedMatch>
1003where
1004    'interp: 'rslt,
1005{
1006    let span = ident.span;
1007    let key = MacroRulesNormalizedIdent::new(ident);
1008    interp.get(&key).ok_or_else(|| dcx.create_err(MveUnrecognizedVar { span, key }))
1009}
1010
1011/// Used by meta-variable expressions when an user input is out of the actual declared bounds. For
1012/// example, index(999999) in an repetition of only three elements.
1013fn out_of_bounds_err<'a>(dcx: DiagCtxtHandle<'a>, max: usize, span: Span, ty: &str) -> Diag<'a> {
1014    let msg = if max == 0 {
1015        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("meta-variable expression `{0}` with depth parameter must be called inside of a macro repetition",
                ty))
    })format!(
1016            "meta-variable expression `{ty}` with depth parameter \
1017             must be called inside of a macro repetition"
1018        )
1019    } else {
1020        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("depth parameter of meta-variable expression `{0}` must be less than {1}",
                ty, max))
    })format!(
1021            "depth parameter of meta-variable expression `{ty}` \
1022             must be less than {max}"
1023        )
1024    };
1025    dcx.struct_span_err(span, msg)
1026}
1027
1028/// Extracts an metavariable symbol that can be an identifier, a token tree or a literal.
1029fn extract_symbol_from_pnr<'a>(
1030    dcx: DiagCtxtHandle<'a>,
1031    pnr: &ParseNtResult,
1032    span_err: Span,
1033) -> PResult<'a, Symbol> {
1034    match pnr {
1035        ParseNtResult::Ident(nt_ident, kind) => {
1036            validate_ident_kind(dcx, *kind, span_err)?;
1037            Ok(nt_ident.name)
1038        }
1039        ParseNtResult::Tt(TokenTree::Token(
1040            Token { kind: TokenKind::Ident(symbol, kind), .. },
1041            _,
1042        )) => {
1043            validate_ident_kind(dcx, *kind, span_err)?;
1044            Ok(*symbol)
1045        }
1046        ParseNtResult::Tt(TokenTree::Token(
1047            Token {
1048                kind: TokenKind::Literal(Lit { kind: LitKind::Str, symbol, suffix: None }),
1049                ..
1050            },
1051            _,
1052        )) => Ok(*symbol),
1053        ParseNtResult::Literal(expr)
1054            if let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) = &expr.kind =>
1055        {
1056            Ok(*symbol)
1057        }
1058        ParseNtResult::Literal(expr)
1059            if let ExprKind::Lit(lit @ Lit { kind: LitKind::Integer, symbol, suffix }) =
1060                &expr.kind =>
1061        {
1062            if lit.is_semantic_float() {
1063                Err(dcx
1064                    .struct_err("floats are not supported as metavariables of `${concat(..)}`")
1065                    .with_span(span_err))
1066            } else if suffix.is_none() {
1067                Ok(*symbol)
1068            } else {
1069                Err(dcx
1070                    .struct_err("integer metavariables of `${concat(..)}` must not be suffixed")
1071                    .with_span(span_err))
1072            }
1073        }
1074        _ => Err(dcx
1075            .struct_err(
1076                "metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`",
1077            )
1078            .with_note("currently only string and integer literals are supported")
1079            .with_span(span_err)),
1080    }
1081}