Support Default Field Values, backwards compatibly #1955
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR incorporates work from @estebank in #1851. See also rust-lang/rust#132162 (comment) for context; this is the PR for "solution 2", to unblock the language feature.
Fixes #1774
#![feature(default_field_values)]By running
cargo-semver-checks --all-features(v0.45.0), I have confirmed that this is not a breaking change.This PR has two related changes:
FieldWithDefault, which is a version ofField, with the addition of the optionaldefaultfield. This is then used to create a parallel hierarchy of all Syn types, with the same naming pattern. That is, it also createsFieldsNamedWithDefault,FieldsWithDefault,VariantsWithDefault,DataStructWithDefault, etc.Field, to allow for as much shared code as possible. There is one subtle change here; the parsing forField(and thereforeFieldsNamed,Fields,DeriveInput, etc.) will now ignore any default value provided (i.e. not give an error, but also not reflect it in the AST). This enables a silent upgrade of most derive macros to support their same codegen as if the default field values didn't exist, which will be extremely valuable in the ecosystem.This is explicitly not #1911; instead, that issue would likely be updated to mean "revert this PR, and land #1851 as part of v3.x".
The new structs in this PR is not behind a feature flag. This is because the current infrastructure in
syn-internal-codegenonly supports "any" style feature flags, whereas this would need to be handled as "all" style feature flags.I'm not going to make the changes to the codegen infrastructure myself. I would move these types behind feature flags if:
The silent ignoring of the default values would definitely be reasonable to object to. I think it's the least bad option on an ecosystem level, but I'm happy to change it (either some form of logging, or just throwing an error).
The parallel hierarchy is not complete. That is, we do not support structs with default field values inside blocks (i.e. inside functions/block expressions/etc.). That's because it requires changing so much more code. (But obviously if they exist, they'll be silently ignored).
A reasonable alternative would be to implement #1870 for those nested fields.