rustc_expand/mbe/
transcribe.rs

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