Skip to content
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

Improve derive syntax #2626

Merged
merged 8 commits into from
Dec 10, 2021
Merged

Improve derive syntax #2626

merged 8 commits into from
Dec 10, 2021

Conversation

pksunkara
Copy link
Member

@pksunkara pksunkara commented Jan 19, 2021

  • Sqlite tests
  • Postgres tests
  • Mysql tests
  • Comprehensive compile errors tests
  • Deprecated syntax support
  • Comprehensive compile warnings tests
  • #[column_name = "QUERY PLAN"] support
  • TypePath for sql_type and foreign_derive?
  • Remove changeset_options keyword

@pksunkara pksunkara added this to the 2.0 milestone Jan 19, 2021
@pksunkara pksunkara marked this pull request as draft January 19, 2021 19:50
@weiznich weiznich requested a review from a team January 20, 2021 07:43
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks quite good 👍 Left a first bunch of comments. There are likely a few more things which need to change later on or I've missed now.

.vscode/settings.json Outdated Show resolved Hide resolved
diesel_derives/src/attrs.rs Outdated Show resolved Hide resolved
diesel_derives/src/attrs.rs Outdated Show resolved Hide resolved
diesel_derives/src/field.rs Outdated Show resolved Hide resolved
diesel_derives/src/field.rs Outdated Show resolved Hide resolved
diesel_derives/src/field.rs Outdated Show resolved Hide resolved
diesel_derives/src/insertable.rs Outdated Show resolved Hide resolved
diesel_derives/src/lib.rs Outdated Show resolved Hide resolved
diesel_derives/src/model.rs Outdated Show resolved Hide resolved
diesel_derives/src/model.rs Outdated Show resolved Hide resolved
@pksunkara
Copy link
Member Author

Still some work to do. I just wanted to create a draft PR to show some progress. You can see that the commits are from 2 months ago :)

Regarding duplicated attributes, neither clap nor other repos which follow this overridable pattern has warnings. And I actually think it's good like that because it might open up some modularized use cases for end users.

@weiznich
Copy link
Member

@pksunkara Are you interested in finishing this PR in the next few weeks?

@pksunkara
Copy link
Member Author

Yup, I was just rebasing this.

@pksunkara
Copy link
Member Author

@weiznich Why does selectable derive have sql_type attribute helper? reference. I don't see it being used anywhere in the selectable expansion.

Also, since this is a new derive, we don't need backward compatibility for this and remove the old attribute helpers, right?

@weiznich
Copy link
Member

Yes, sql_type is not used there.

Also, since this is a new derive, we don't need backward compatibility for this and remove the old attribute helpers, right?

Sounds reasonable 👍

@pksunkara
Copy link
Member Author

pksunkara commented Jul 7, 2021

@weiznich Can you please give a preliminary look and see if you are okay with the format of the new attributes? Field attribute and Struct attribute

@pksunkara
Copy link
Member Author

pksunkara commented Jul 7, 2021

CI is weird. I am locally on 1.48.0 and am unable to reproduce this error at all. Or the spanned::Spanned error. ./bin/test is passing completely on my local.

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done another round of review and left some comments on places where I have questions or small suggestions.

General points to discuss:

  • As far as I see there is currently no way to emit a deprecation warning on a build not using rustc nightly. That's bad, we should find some way to "fix" that, even if that means to emit some "warnings" via eprintln!
  • The get_parent() error for the minimal rust version build is caused by -Z minimal-versions, which just means that the minimal syn version that is specified do not have the corresponding method. This should be fixed by just increasing the minimal supported syn version to some version that has this method (at least syn 1.0.3 as far as I can see)
  • There are a few places the old deprecated attributes are not converted. The CI mostly did emit warnings for them, they should be fixed
  • I'm not sure about the spanned::Spanned error, are you really sure that this trait is really needed in that scope for all backends?

diesel/src/associations/mod.rs Show resolved Hide resolved
diesel/src/mysql/types/mod.rs Outdated Show resolved Hide resolved
diesel/src/type_impls/json.rs Outdated Show resolved Hide resolved
diesel_derives/src/model.rs Outdated Show resolved Hide resolved
diesel_derives/src/queryable_by_name.rs Outdated Show resolved Hide resolved
diesel_derives/src/util.rs Outdated Show resolved Hide resolved
diesel_derives/tests/queryable_by_name.rs Outdated Show resolved Hide resolved
diesel_tests/tests/schema/mod.rs Outdated Show resolved Hide resolved
@pksunkara
Copy link
Member Author

I haven't gone through the error/warning messages yet for the compile tests. I should have specifically said not to review those files inorder to save some time.

@weiznich
Copy link
Member

As far as I see there is currently no way to emit a deprecation warning on a build not using rustc nightly. That's bad, we should find some way to "fix" that, even if that means to emit some "warnings" via eprintln!

So the Readme states:

Warnings are emitted only on nightly, they are ignored on stable.

which is unfortunate, as we really want to show the deprecated message everywhere. I see a few options around this:

  • Provide our own wrapping macro that emits warnings via eprintln! instead of just ignoring them. This likely requires a similar version check than done by proc-macro-error
  • Try to fix that in proc-macro-error by maybe using eprintln! there?
  • Emit an error instead? Maybe behind a feature flag? (Diesel already has a with/without-deprectated feature flag) (I'm not really sold on that option)

@pksunkara
Copy link
Member Author

Yeah, I am not exactly sure what we want to do regarding that. I am leaving it for the last thing to do though. Hopefully we will come up with something by then.

@pksunkara pksunkara marked this pull request as ready for review September 22, 2021 18:42
@pksunkara
Copy link
Member Author

pksunkara commented Sep 22, 2021

@weiznich This is ready for review

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update 👍
This looks much better now. I left a few comments where I noticed small things. I think most of them should be easy to fix, some will likely require just a clarification.

diesel_compile_tests/rust-toolchain Show resolved Hide resolved
diesel_compile_tests/tests/fail/derive/selectable.rs Outdated Show resolved Hide resolved
diesel_derives/src/parsers/postgres_type.rs Outdated Show resolved Hide resolved
diesel_derives/src/parsers/postgres_type.rs Outdated Show resolved Hide resolved
diesel_derives/src/util.rs Outdated Show resolved Hide resolved
diesel_derives/tests/queryable_by_name.rs Outdated Show resolved Hide resolved
guide_drafts/trait_derives.md Outdated Show resolved Hide resolved
@weiznich
Copy link
Member

I should probably comment here to say that I've a rebased version of this locally, which also fixes at least some of the remarks. I can try to clean it up a bit tomorrow and push it afterwards.

@pksunkara
Copy link
Member Author

I should probably comment here to say that I've a rebased version of this locally, which also fixes at least some of the remarks. I can try to clean it up a bit tomorrow and push it afterwards.

Oh, I was just about to do the rebase for the final push this week to get this merged. I will wait for you. Would you mind if you create a new commit for your changes instead of bundling them up with my rebased commits?

@weiznich
Copy link
Member

I've pushed my rebased version. Hopefully this is a good starting point for fixing the remaining issues.

@pksunkara
Copy link
Member Author

I am not sold on the commit you pushed since we have the span already at dba9212 (#2626).

I will be able to take a deeper look tomorrow and also address the last comment that's left.

@weiznich
Copy link
Member

weiznich commented Dec 6, 2021

I am not sold on the commit you pushed since we have the span already at dba9212 (#2626).

I think an important point to note here is that there are a few different spans involved here:

  • The span of the field as whole, as returned by SynField.span(). That's the span that seems to point only to the beginning of the field.
  • The span of each element in the attribute. So in a #[diesel(embed, serialize_as = "…")] attribute are two of those spans. One pointing to embed and one pointing to serialize_as = "…". Those are stored in ident_span
  • The span of a field attribute as whole thing. For #[diesel(embed, serialize_as = "…")] this will point to diesel(embed, serialize_as = "…"). It seems like trying to point to the whole attribute just creates a single char span at the beginning 😞 . That needs to be a separate entry as there may be more than one attribute here per field. That stored in attribute_span
  • The span of the field itself. I opted to use either the span of the field name (for structs with named fields) or the span of the field type (for tuple structs) for this. That stored in field.span

@pksunkara
Copy link
Member Author

I am going to separate the AttributeSpanWrapper work into a separate PR that will get opened up once this is merged as a fast follow. I think we need a little bit discussion on it. And I might have a better idea on how to do that.

I pulled the commit locally and saved it.

@pksunkara
Copy link
Member Author

Addressed all the comments now. The only question left from my original goal of the work is:

TypePath for sql_type, serialize_as, deserialize_as?

Can you please give a look at https://docs.rs/syn/latest/syn/enum.Type.html and tell me which of those variants do we allow for the above mentioned attributes?

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a bunch of minor notes. Those should be easy to fix. Otherwise I think this now looks really good 🎉.

One thing is still missing: A changelog entry + probably some draft section for the upgrade guide (Just some few words that we updated the attribute format…)

diesel_compile_tests/tests/fail/derive/bad_not_sized.rs Outdated Show resolved Hide resolved
error: unexpected `oid` when `name` is present
--> $DIR/bad_postgres_type.rs:30:44
|
30 | #[diesel(postgres_type(name = "foo", oid = 2, array_oid = 3))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a thing for the followup PR: This spans could also be improved using the same strategy as demonstrated for deserialize_as/embed

diesel_derives/src/parsers/postgres_type.rs Outdated Show resolved Hide resolved
diesel_derives/src/parsers/postgres_type.rs Outdated Show resolved Hide resolved
diesel_derives/src/parsers/postgres_type.rs Outdated Show resolved Hide resolved
diesel_derives/src/parsers/mysql_type.rs Outdated Show resolved Hide resolved
diesel_derives/src/parsers/sqlite_type.rs Outdated Show resolved Hide resolved
@weiznich
Copy link
Member

weiznich commented Dec 9, 2021

Can you please give a look at https://docs.rs/syn/latest/syn/enum.Type.html and tell me which of those variants do we allow for the above mentioned attributes?

I would just accept TypePath there and nothing else. If that turns out to be not enough we can easily include more variants later on.

@pksunkara
Copy link
Member Author

Address review and used TypePath

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this 🎉

@weiznich weiznich merged commit 6aadc7f into diesel-rs:master Dec 10, 2021
@weiznich
Copy link
Member

@pksunkara I want to use this opportunity to thank your for your long term effort here. I really appreciate this. ❤️

@pksunkara pksunkara deleted the derive branch December 10, 2021 13:55
@pksunkara
Copy link
Member Author

No problem. I just wish I had the time to actually focus on this properly in a straight forward manner instead of doing over a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants