Error in Rust Declarative Macro for Trait MethodsWrapping

for this rust macro:

#[macro_export]
macro_rules! refine_fn {
    // 1.1 match fns(functions or methods) with default implementation, but with `&self` param prefix
    (
        [fns_impls_with_self: $($fns_impls_with_self:tt)*]
        [fns_impls_with_self_mut: $($fns_impls_with_self_mut:tt)*]
        [fns_impls_without_self: $($fns_impls_without_self:tt)*]
        [fns_no_impls: $($fns_no_impls:tt)*]
        fn $fn_name:ident(& $self:expr, $($arg:tt)*) $(-> $ret_ty:ty)? { $($fn_body:tt)* }
        $($rest:tt)*
    ) => (
        $crate::refine_fn! {
            [fns_impls_with_self: $($fns_impls_with_self)* /* */ &$self, $fn_name ($($arg)*) $($ret_ty)? {$($fn_body)*}]
            [fns_impls_with_self_mut: $($fns_impls_with_self_mut)*]
            [fns_impls_without_self: $($fns_impls_without_self)*]
            [fns_no_impls: $($fns_no_impls)*]
            $($rest)*
        }
    );
    // 1.2 match fns(functions or methods) with default implementation, but with `&mut self` param prefix
    (
        [fns_impls_with_self: $($fns_impls_with_self:tt)*]
        [fns_impls_with_self_mut: $($fns_impls_with_self_mut:tt)*]
        [fns_impls_without_self: $($fns_impls_without_self:tt)*]
        [fns_no_impls: $($fns_no_impls:tt)*]
        fn $fn_name:ident(&mut $self:expr, $($arg:tt)*) $(-> $ret_ty:ty)? { $($fn_body:tt)* }
        $($rest:tt)*
    ) => (
        $crate::refine_fn! {
            [fns_impls_with_self: $($fns_impls_with_self)*]
            [fns_impls_with_self_mut: $($fns_impls_with_self_mut)* /* */ &mut $self, $fn_name ($($arg)*) $($ret_ty)? {$($fn_body)*}]
            [fns_impls_without_self: $($fns_impls_without_self)*]
            [fns_no_impls: $($fns_no_impls)*]
            $($rest)*
        }
    );
     // 1.3 match fns(functions or methods) with default implementation, but without `&(mut)self` param prefix
     (
        [fns_impls_with_self: $($fns_impls_with_self:tt)*]
        [fns_impls_with_self_mut: $($fns_impls_with_self_mut:tt)*]
        [fns_impls_without_self: $($fns_impls_without_self:tt)*]
        [fns_no_impls: $($fns_no_impls:tt)*]
        fn $fn_name:ident($($arg:tt)*) $(-> $ret_ty:ty)? { $($fn_body:tt)* }
        $($rest:tt)*
    ) => ($crate::refine_fn! {
        [fns_impls_with_self: $($fns_impls_with_self)*]
        [fns_impls_with_self_mut: $($fns_impls_with_self_mut)*]
        [fns_impls_without_self: $($fns_impls_without_self)* /* */ $fn_name ($($arg)*) $($ret_ty)? {$($fn_body)*}]
        [fns_no_impls: $($fns_no_impls)*]
        $($rest)*
    });
    // 1.4.match fns(functions or methods) with no default implementation
    (
        [fns_impls_with_self: $($fns_impls_with_self:tt)*]
        [fns_impls_with_self_mut: $($fns_impls_with_self_mut:tt)*]
        [fns_impls_without_self: $($fns_impls_without_self:tt)*]
        [fns_no_impls: $($fns_no_impls:tt)*]
        fn $fn_name_no_impl:ident($($arg_no_impl:tt)*) $(-> $ret_ty_no_impl:ty)? ;
        $($rest:tt)*
    ) => ($crate::refine_fn! {
        [fns_impls_with_self: $($fns_impls_with_self)*]
        [fns_impls_with_self_mut: $($fns_impls_with_self_mut)*]
        [fns_impls_without_self: $($fns_impls_without_self)*]
        [fns_no_impls: $($fns_no_impls)* /* */ $fn_name_no_impl ($($arg_no_impl)*) $($ret_ty_no_impl)?;]
        $($rest)*
    });
    // 2.final output
    (
        [fns_impls_with_self: $( $fn_name_impl_with_self:ident (&$self:expr, $($arg_impl_with_self:tt)*) $($ret_ty_impl_with_self:ty)? {$($fn_body_with_self:tt)*} )*]
        [fns_impls_with_self_mut: $( $fn_name_impl_with_self_mut:ident (&mut $self_mut:expr, $($arg_impl_with_self_mut:tt)*) $($ret_ty_impl_with_self_mut:ty)? {$($fn_body_with_self_mut:tt)*} )*]
        [fns_impls_without_self: $( $fn_name_impl_without_self:ident ($($arg_impl_without_self:tt)*) $($ret_ty_impl_without_self:ty)? {$($fn_body_without_self:tt)*} )*]
        [fns_no_impls: $( $fn_name_no_impl:ident ($($arg_no_impl:tt)*) $($ret_ty_no_impl:ty)? ; )*]
    ) => (
        paste::paste!{
            // 2.1.1 copy and refine for each function with default implementation, but with `&self.` prefix
            $(
                fn $fn_name_impl_with_self(&$self, $($arg_impl_with_self)*) $(-> $ret_ty_impl_with_self)? {
                    $crate::refine_fn_body! {
                        $self, // input `self` here to avoid `self` as a keyword issue.
                        [pre_content: ]
                        $($fn_body_with_self)*
                    }
                    // just for test
                    // $($fn_body)*
                }
            )*
            // 2.1.2 copy and refine for each function with default implementation, but with `&mut self.` prefix
            $(
                fn $fn_name_impl_with_self_mut(&mut $self, $($arg_impl_with_self_mut)*) $(-> $ret_ty_impl_with_self_mut)? {
                    $crate::refine_fn_body! {
                        $self,  // input `self` here to avoid `self` as a keyword issue.
                        [pre_content: ]
                        $($fn_body_with_self_mut)*
                    }
                    // just for test
                    // $($fn_body)*
                }
            )*
            // 2.1.3 copy and refine for each function with default implementation, but without `&(mut)self.` prefix
            $(
                fn $fns_impls_without_self($($arg_impl_without_self)*) $(-> $ret_ty_impl_without_self)? {
                    $($fn_body_without_self)*
                }
            )*
            // 2.1.4 copy and paste for each function with no default implementation
            $(
                fn $fn_name_no_impl($($arg_no_impl)*) $(-> $ret_ty_no_impl)? ;
            )*
        }
    );
}

, which is used to wrap some trait methods in another declarative macro like this:

$crate::refine_fn! {
    [fns_impls_with_self: ]
    [fns_impls_with_self_mut: ]
    [fns_impls_without_self: ]
    [fns_no_impls: ]
    $($trait_content)*  // there are only valid trait methods, be it in any form(with or without default implementation).
}

I want to only deal with the funcs that have body content with &(mut)self as 1st param in the refine_fn! macro, but it raised the issue:

error: unexpected end of macro invocation
  --> D:\DATA\BaiduSyncdisk\project\personal\trait_variable\src\refine_fn.rs:33:46
   |
33 |             [fns_no_impls: $($fns_no_impls)*]
   |                                              ^ missing tokens in macro arguments
   |
note: while trying to match `fn`
  --> D:\DATA\BaiduSyncdisk\project\personal\trait_variable\src\refine_fn.rs:9:9
   |
9  |         fn $fn_name:ident(& $self:expr, $($arg:tt)*) $(-> $ret_ty:ty)? { $($fn_body:tt)* }
   |         ^^

. I have no idea, everything seems ok.

For the tt-muncher pattern used here, I didn't plan to do so at first( which is complicated), but if not doing so, it would raise a local ambiguity issue when matching multiple fns. However, finally, it seems that the current issue is still somewhat related to the ambiguity.

This expansion (in RHS of section 1.1)

which seems to expand into something like

[fns_impls_with_self: &self, the_func (arg: i32) i32 { arg + 2 } ... ]

is inconsistent with this pattern (in LHS of section 2.)

which seems to expect something like

[fns_impls_with_self: the_func(&self, arg: i32) i32 { arg + 2 } ... ]

It seems you just misplaced the &$self, part in the expansion.

So after collecting all function items into [...] blocks no LHS matches (1.x's do not match because all fn items are exhausted, 2. does not match because of mentioned inconsistency) and the compiler probably just blames the first rule.

Also I used $self:tt during testing/debugging and did not retest with $self:expr, maybe that also does something.

Also there were some random typo-like bugs ($self vs. $self_mut and similar) which I do not remember.

Yes, you are write, I misplaced it. I fell into my own cognitive bias and failed to identify it.