-
Notifications
You must be signed in to change notification settings - Fork 68
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
Upgrade syn to v2 #226
Upgrade syn to v2 #226
Conversation
The Spanned trait becomes sealed in v2 of syn. This means manual implementations of Spanned are no longer possible. If needed, the only way to implement Spanned is to implement `quote::ToTokens`, because there is a blanket implementation.
Based on this darling PR: TedDriggs/darling#226
I think the failures I see with This PR should then be mostly ready, besides some small cleanup. |
darling heavily relies on `syn::NestedMeta`. This type has been removed in syn v2. Instead create a copy under `darling::ast::NestedMeta` and use that to replace all uses of `syn::NestedMeta`.
Switch access to `.nested` to use `parse_meta_list` instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for starting on this - I'm going to need to dig into the changes more deeply over the coming week.
// The number of errors here is 1 for the bad value of the `st` attribute | ||
assert_eq!(1, errors.len()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this test as-written even make sense any more? It was guarding against attributes that can't be expressed as Meta
, but that seems to have largely gone away?
Based on this darling PR: TedDriggs/darling#226
It's no longer allowed to use bare trait types for Box type parameter.
This is syn v2's MSRV, and darling cannot go below that.
This was initially removed in an attempt to get rid of `NestedMeta`, but since the type stays, the function can too.
Hello, saw this pr as I'm also looking to upgrade my projects dependencies on |
Thanks for the information. I updated the example in the README. Right now, I am not sure this PR is on the right track, given the comments about the non-exhaustive enums and |
@jonasbb I think we're on the right track here - I was traveling last week, and need to get back to this this week. |
@jonasbb I'd like your opinion on what to do about There seem to be two options:
The commit I pushed to the branch implements option 1, and while I think that's fine I'm not enthused about it long-term. This approach creates a weird hole for anyone whose attribute takes an expression where a string literal would be valid input, and there's currently no escape hatch. |
If I understand the commit correctly, it is about supporting non-string arguments in attributes. If so, it is related to #96 and #216? Darling only supported string arguments, so far. Being backwards compatible with that is important I think. This functionality probably doesn't have to go into Maybe something like this as a new function. impl FromMeta for syn::Expr {
fn from_str_lit_or_expr(expr: &Expr) -> Result<Self> {
if let syn::Expr::Lit(expr_lit) = expr {
if let syn::Lit::Str(s) = &expr_lit.lit {
return Self::from_string(s);
}
}
Self::from_expr(expr)
}
// ...
} Changes to the derive macros could look something like this. The new #[derive(FromMeta)]
struct SerdeContainerOptions {
#[darling(rename = "crate", from_str=true)]
alt_crate_path: Option<Path>,
} |
fn from_expr(expr: &Expr) -> Result<Self> { | ||
if let syn::Expr::Lit(expr_lit) = expr { | ||
if let syn::Lit::Str(_) = &expr_lit.lit { | ||
return Self::from_value(&expr_lit.lit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If from_value
returns Err
you could consider returning Ok(expr.clone())
here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is that if we see a literal string we'll always assign it the v1 behavior of parsing the contents? I wish there was a good way to deprecate that behavior, but I don't see a means of emitting a deprecation warning that would show up to the user of the crate that is using darling.
@jonasbb I'm planning to merge and release this tomorrow; if you have any last-second edits or concerns, now's the time to voice them. |
Maybe increasing the version of The code should be good to release to enable I checked the PR again and I think these are all the breaking changes:
|
@jonasbb I just saw your comment about the backwards-compatibility situation; I think your idea of an additional meta option in I'm wondering instead if a better approach is to add #[derive(FromMeta)]
pub struct Example {
#[darling(with = "darling::util::parse_str_content")]
foo: Expr,
} I need to check whether the generated code would do well with type inference there, but it seems like a good way to allow people to opt into backwards-compatible behavior in cases where ambiguity exists, without needlessly forcing changes to code where the quotation marks don't introduce ambiguity (like |
@TedDriggs The proposed |
@jonasbb let's move that out to its own issue - this branch handles the backwards compatibility case natively, and I think it's probably time to ship this as v0.15.0 |
Last-second update; this is going to go out as v0.20.0; that way it'll be clearer to people there's been a significant change, and it leaves room for servicing the syn 1 branch |
Based on this darling PR: TedDriggs/darling#226
Hi,
syn
just got a new v2 release and I wanted to see how much work it would be to make this crate compatible with it.I got it to compile and pass the tests in darling.
Please don't hesitate to close or edit this PR in any way you see fit. The main changes I did were:
syn::spanned::Spanned
is now a sealed trait and cannot be implemented directly. This only affectsFlag
andSpannedValue
. The only way now is to use the blanket implementation viaquote::ToTokens
.parse_meta()
, instead they have ameta
field now.NestedMeta
type got removed. Since this crate relies so heavily on it, I created a replacement indarling::ast::NestedMeta
and replaced all occurrences ofsyn::NestedMeta
with it.LifetimeDef
and functions are renamed toLifetimeParam
to followsyn
.I just wanted to share the progress. Anyone should feel welcome to finish the code.