From 88b99224c1f59bef4f62aab5120f08dbd3c011c9 Mon Sep 17 00:00:00 2001 From: mark <markm@cs.wisc.edu> Date: Wed, 2 Mar 2022 21:33:43 -0600 Subject: [PATCH] add some examples to comments in mbe code --- compiler/rustc_expand/src/mbe/macro_parser.rs | 32 +++++++++++++++++++ compiler/rustc_expand/src/mbe/transcribe.rs | 6 ++++ 2 files changed, 38 insertions(+) diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs index dd82add08dd..a419612e315 100644 --- a/compiler/rustc_expand/src/mbe/macro_parser.rs +++ b/compiler/rustc_expand/src/mbe/macro_parser.rs @@ -345,6 +345,38 @@ fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree]) -> MatcherPos<'root, 't /// token tree. The depth of the `NamedMatch` structure will therefore depend /// only on the nesting depth of `ast::TTSeq`s in the originating /// token tree it was derived from. +/// +/// In layman's terms: `NamedMatch` will form a tree representing nested matches of a particular +/// meta variable. For example, if we are matching the following macro against the following +/// invocation... +/// +/// ```rust +/// macro_rules! foo { +/// ($($($x:ident),+);+) => {} +/// } +/// +/// foo!(a, b, c, d; a, b, c, d, e); +/// ``` +/// +/// Then, the tree will have the following shape: +/// +/// ```rust +/// MatchedSeq([ +/// MatchedSeq([ +/// MatchedNonterminal(a), +/// MatchedNonterminal(b), +/// MatchedNonterminal(c), +/// MatchedNonterminal(d), +/// ]), +/// MatchedSeq([ +/// MatchedNonterminal(a), +/// MatchedNonterminal(b), +/// MatchedNonterminal(c), +/// MatchedNonterminal(d), +/// MatchedNonterminal(e), +/// ]) +/// ]) +/// ``` #[derive(Debug, Clone)] crate enum NamedMatch { MatchedSeq(Lrc<NamedMatchVec>), diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 01a7f726617..54000527c15 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -358,6 +358,12 @@ impl LockstepIterSize { /// Note that if `repeats` does not match the exact correct depth of a meta-var, /// `lookup_cur_matched` will return `None`, which is why this still works even in the presence of /// multiple nested matcher sequences. +/// +/// Example: `$($($x $y)+*);+` -- we need to make sure that `x` and `y` repeat the same amount as +/// each other at the given depth when the macro was invoked. If they don't it might mean they were +/// declared at unequal depths or there was a compile bug. For example, if we have 3 repetitions of +/// the outer sequence and 4 repetitions of the inner sequence for `x`, we should have the same for +/// `y`; otherwise, we can't transcribe them both at the given depth. fn lockstep_iter_size( tree: &mbe::TokenTree, interpolations: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,