Skip to content

Commit 88aa75b

Browse files
authored
Rollup merge of #94544 - mark-i-m:macro-comments, r=petrochenkov
Add some examples to comments in mbe code I found these things non-obvious when re-familiarizing myself with the code. r? `@petrochenkov`
2 parents 98c9ee8 + 88b9922 commit 88aa75b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

+32
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,38 @@ fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree]) -> MatcherPos<'root, 't
345345
/// token tree. The depth of the `NamedMatch` structure will therefore depend
346346
/// only on the nesting depth of `ast::TTSeq`s in the originating
347347
/// token tree it was derived from.
348+
///
349+
/// In layman's terms: `NamedMatch` will form a tree representing nested matches of a particular
350+
/// meta variable. For example, if we are matching the following macro against the following
351+
/// invocation...
352+
///
353+
/// ```rust
354+
/// macro_rules! foo {
355+
/// ($($($x:ident),+);+) => {}
356+
/// }
357+
///
358+
/// foo!(a, b, c, d; a, b, c, d, e);
359+
/// ```
360+
///
361+
/// Then, the tree will have the following shape:
362+
///
363+
/// ```rust
364+
/// MatchedSeq([
365+
/// MatchedSeq([
366+
/// MatchedNonterminal(a),
367+
/// MatchedNonterminal(b),
368+
/// MatchedNonterminal(c),
369+
/// MatchedNonterminal(d),
370+
/// ]),
371+
/// MatchedSeq([
372+
/// MatchedNonterminal(a),
373+
/// MatchedNonterminal(b),
374+
/// MatchedNonterminal(c),
375+
/// MatchedNonterminal(d),
376+
/// MatchedNonterminal(e),
377+
/// ])
378+
/// ])
379+
/// ```
348380
#[derive(Debug, Clone)]
349381
crate enum NamedMatch {
350382
MatchedSeq(Lrc<NamedMatchVec>),

compiler/rustc_expand/src/mbe/transcribe.rs

+6
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ impl LockstepIterSize {
358358
/// Note that if `repeats` does not match the exact correct depth of a meta-var,
359359
/// `lookup_cur_matched` will return `None`, which is why this still works even in the presence of
360360
/// multiple nested matcher sequences.
361+
///
362+
/// Example: `$($($x $y)+*);+` -- we need to make sure that `x` and `y` repeat the same amount as
363+
/// each other at the given depth when the macro was invoked. If they don't it might mean they were
364+
/// declared at unequal depths or there was a compile bug. For example, if we have 3 repetitions of
365+
/// the outer sequence and 4 repetitions of the inner sequence for `x`, we should have the same for
366+
/// `y`; otherwise, we can't transcribe them both at the given depth.
361367
fn lockstep_iter_size(
362368
tree: &mbe::TokenTree,
363369
interpolations: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,

0 commit comments

Comments
 (0)