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

Update syn: v2.0 #1096

Merged
merged 1 commit into from May 12, 2023
Merged

Update syn: v2.0 #1096

merged 1 commit into from May 12, 2023

Conversation

ranfdev
Copy link
Member

@ranfdev ranfdev commented May 9, 2023

closes #1061

Problem
syn 2.0 removed the type NestedMeta, that was extensively used by gtk-rs-core to parse macro attributes, without providing a direct replacement type.
Instead, the suggested way to parse an attribute is now to use Attribute::parse_nested_meta, which iterates over the arguments of an attribute and forces you to parse them. parse_nested_meta tracks the spans of the arguments being parsed, so in the case an error is returned, it provides good error messages.

Most of the glib-macros depended on utility functions defined in glib-macros::utils. Those functions often tried to retrieve a single argument inside an attribute, without caring about the other arguments. This is not possible when using Attribute::parse_nested_meta, because it requires to handle every possible argument in a single place. We can't just parse the argument name = "value" and skip nick = "nick", because parse_nested_meta will consider nick as an unexpected value and will throw an error.

Proposed solution
Rewriting the parsing of attributes taking advantage of parse_nested_meta, which is now the suggested way to parse attribute arguments (also see https://docs.rs/syn/latest/syn/meta/struct.ParseNestedMeta.html). A new set of utilities have been implemented in glib-macro::utils to ease the parsing process.

To do
implement some tests for the new parsing utility functions, to polish their interface and ensure correctness.

Observations
I would consider using the crate https://github.com/jf2048/deluxe (by @jf2048, #970) to handle attribute parsing.

Conclusions

  • Improvements from syn 1.0:
    • We are up to date!
    • the errors from the macro should now be improved, because parse_nested_meta keeps track of argument spans and ensures every argument is parsed correctly.
  • Drawbacks: even though the tests are passing, rewriting the parsing system can easily introduce some bugs. I fear I've missed some corner cases.

sdroege
sdroege previously approved these changes May 11, 2023
Copy link
Member Author

@ranfdev ranfdev left a comment

Choose a reason for hiding this comment

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

Ah, I've noticed I still need to change some little things, please don't merge yet.

glib-macros/src/enum_derive.rs Outdated Show resolved Hide resolved
@sdroege sdroege merged commit 7ad321f into gtk-rs:master May 12, 2023
46 checks passed
@ranfdev ranfdev deleted the syn2 branch May 12, 2023 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

glib-macros: Port to syn 2.0
2 participants