New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create an extended version of syn::Meta that accepts paths and expressions in value position. #1704
base: master
Are you sure you want to change the base?
Conversation
Where I'm at now: I've replaced all uses of syn::Meta with the extended version, and it now supports path parsing. The only test failures are UI mismatches due to the different high-level parsing logic and also due to a new message "expected path or literal" in value position. Adding support for expressions isn't as straightforward. |
I'm having second thoughts about exactly specifying the type of values in This will also result in better diagnostic error messages for the end-user. If the type were specified within the structure of This pattern already exists in other places (e.g. |
I had some issues with ambiguity when parsing values as Example:
|
Ready for discussion / review |
I've been working on the next step, modifying the parsing frontend to accept inline syntax, you can see progress on my branch Something I ran into is an ambiguity with regard to where clauses. Since they use commas as punctuation, you can't write something like |
@dtolnay any feedback? There are a few UI failures but they don't seem to be any harder to understand, though a bit more verbose. |
Hi @agausmann, I am still interested in this change but I haven't gotten a chance to take a look yet. It is still definitely on the list (https://triage.rust-lang.org/notifications?user=dtolnay). |
I did a brief review, looks good to me. But I don't know the internals of serde that well to forsee any future problems. |
As discussed in #368 and #1490, there is a desire for supporting a larger set of syntaxes in attributes, including the ability to pass paths and expressions as values.
Right now, attribute parsing uses
syn::Meta
, but this only allows for literals in value position. This means that any other desired syntax must be wrapped in string literals (e.g.#[serde(default = "defaults::one")]
). The long-term goal is to eventually replace such string literals with paths, and to be able to support other syntax elements in value position, such as expressions, for new kinds of options likedefault_value
.The approach that I'm taking is to duplicate and extend
syn::Meta
. I haven't bothered with changing the names or the basic structure as it seems to be otherwise sufficient, and this allows us to use it as a drop-in replacement with minimal changes. The main difference is thatlit: Lit
inMetaNameValue
is replaced withlit: MetaValue
, whereMetaValue
is an enum that allows types other thanLit
to be parsed in its place.