1

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?

A.Z.
  • 318
  • 1
  • 8
  • 2
    Would you mind providing a code example? I'm not completely sure what you mean with 'visibility'. – Finomnis Feb 05 '23 at 19:49
  • 2
    https://doc.rust-lang.org/reference/items/enumerations.html#variant-visibility: "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." – Solomon Ucko Feb 05 '23 at 19:55
  • 1
    @SolomonUcko That's unfortunate. I saw the syntax tree at the top of the page and assumed it actually worked. Can you post that as an answer? – A.Z. Feb 05 '23 at 19:56
  • Done: https://stackoverflow.com/a/75356349/5445670 – Solomon Ucko Feb 05 '23 at 23:59

1 Answers1

2

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

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45