rustrust-proc-macros

Does syn::Variant have a mechanism to specify variant visibility?


I have a procedural macro that generates an enum plus its variants and I'd like to add configurable visibility to it, but it looks like the syn::Variant struct doesn't have a visibility field. For some reason it can parse a variant with a visibility so I'm not sure if there's another mechanism for me to set the visibility that I've missed.

Is there another way to set the visibility, or is this a bug?


Solution

  • Unfortunately, this is by design.

    https://doc.rust-lang.org/reference/items/enumerations.html#variant-visibility says: "Enum variants syntactically allow a Visibility annotation, but this is rejected when the enum is validated. This allows items to be parsed with a unified syntax across different contexts where they are used."

    Also, syn ignores the visibility when parsing an enum variant: https://github.com/dtolnay/syn/blob/bf7774b10555bd24a14008ad0c46d6ebde202a1c/src/data.rs#LL166C28-L166C28