syn::Variant有一种机制可以指定变体的可见性吗?

huangapple go评论65阅读模式
英文:

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

问题

我有一个生成枚举及其变体的过程宏,并希望为其添加可配置的可见性,但似乎 syn::Variant 结构体没有可见性字段。由于它可以解析带有可见性的变体,所以我不确定是否有其他机制可以让我设置可见性,我可能错过了什么。

是否有其他设置可见性的方法,或者这是一个错误?

英文:

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?

答案1

得分: 2

抱歉,这是按设计要求的。

https://doc.rust-lang.org/reference/items/enumerations.html#variant-visibility 中提到:“枚举变体在语法上允许可见性注释,但在验证枚举时会被拒绝。这允许在不同上下文中统一语法下解析项目。”

此外,syn 在解析enum变体时忽略了可见性:https://github.com/dtolnay/syn/blob/bf7774b10555bd24a14008ad0c46d6ebde202a1c/src/data.rs#LL166C28-L166C28

英文:

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

huangapple
  • 本文由 发表于 2023年2月6日 03:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定