Cannot recognize #[attrs] in declarative macro rule

I have a macro where I want to generate extra methods on affected struct. But the issue with macro is that it cannot recognize field attributes (like #[attr]). The error is next:

error: no rules expected the token `#`
  --> src/main.rs:36:9
   |
2  | macro_rules! with_opcode {
   | ------------------------ when calling this macro
...
36 |         #[dynamic_field]
   |         ^ no rules expected this token in macro call

This is my macro:

#[macro_export]
macro_rules! with_opcode {
    (
        $(@[$opcode:expr])?
        $(#[$outer:meta])*
        $vis:vis struct $PacketStruct:ident {
            $(#[$field_attr: meta])*
            $($field_vis:vis $field_name:ident: $field_type:ty),*$(,)?
        }

        $($PacketStructImpl: item)*
    ) => {
        $(#[$outer])*
        $vis struct $PacketStruct {
            $($field_vis $field_name: $field_type),*
        }
        
        impl $PacketStruct {
            $(
                fn opcode() {
                    println!("OPCODE: {:?}", $opcode);
                }
            )?
        }
    };
}

and how I use it:

const TEST: u32 = 100;

with_opcode! {
    @[TEST]
    #[derive(Debug)]
    struct Test {
        #[dynamic_field]
        size: u8,
        #[dynamic_field]
        size1: u16,
        #[dynamic_field]
        size2: u8,
        #[dynamic_field]
        field2: u8,
        field3: String,
    }
}

This is sandbox to reproduce.

Could somebody explain how to fix the issue ?

Oh, I fixed it by wrapping into $() with other field patterns:

(
	$(@[$opcode:expr])?
	$(#[$outer:meta])*
	$vis:vis struct $PacketStruct:ident {
		$(
			$(#[$field_attr: meta])? $field_vis:vis $field_name:ident: $field_type:ty
		),*$(,)?
	}

	$($PacketStructImpl: item)*
)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.