However, on the second input it fails to compile and the macro trace shows it expanding the SAME input as before but with a different output. Why is this happening? Thanks for the help.
Once something gets captured into a $_:expr metavariable, then emitting that metavariable does not yield exactly the source code that was captured by it: instead, it emits it but wrapped within "invisible" parenthesis. In the remainder of this post, I'll be using ⦑ ⦒ for such parenthesis:
macro_rules! array_to_vec {
// Match arrays of arrays of expressions
([$([$($x: expr),*]),*]) => {
// Outer array becomes a vec. Inner array is expanded recursively.
VecEnum::Vec(
vec![$(
- array_to_vec!([$( $x ),*])
+ array_to_vec!([$( ⦑$x⦒ ),*])
),*]
)
};
Thus, if $x happened to be, itself, a [ … ] expression, when recursing,
your macro will stumble upon ⦑ [ … ] ⦒ rather than [ … ],
hence failing the first two rules and directly falling back to the third one, the one expecting a $_:expr.