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

Forward cfgs on pyclass fields to the method defs #2796

Merged
merged 4 commits into from
Dec 29, 2022
Merged

Forward cfgs on pyclass fields to the method defs #2796

merged 4 commits into from
Dec 29, 2022

Conversation

mejrs
Copy link
Member

@mejrs mejrs commented Dec 6, 2022

With this and the cfg_attr PR, I don't need cfg_eval at all anymore :)

  • needs some more tests

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Seems like an uncontroversial good idea to me! We already forward cfgs in other places.

Needs a CHANGELOG entry - changed, perhaps?

As for tests, I'm not sure there's much else which could be added? The hygiene test will already fail if the cfg are not forwarded correctly. I suppose a unit test which checks the generated proc-macro output would be most precise test, though we don't do that at all yet :)

Comment on lines +665 to +670
let mut cfg_attrs = TokenStream::new();
if let PropertyType::Descriptor { field, .. } = &property_type {
for attr in field.attrs.iter().filter(|attr| attr.path.is_ident("cfg")) {
attr.to_tokens(&mut cfg_attrs);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I suspect it probably works to use an Option instead of having the intermediate TokenStream buffer:

Suggested change
let mut cfg_attrs = TokenStream::new();
if let PropertyType::Descriptor { field, .. } = &property_type {
for attr in field.attrs.iter().filter(|attr| attr.path.is_ident("cfg")) {
attr.to_tokens(&mut cfg_attrs);
}
}
let mut cfg_attrs = None;
if let PropertyType::Descriptor { field, .. } = &property_type {
cfg_attrs = Some(field.attrs.iter().filter(|attr| attr.path.is_ident("cfg")));
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think I can collect into a tokenstream :(

@davidhewitt davidhewitt modified the milestones: 0.19, 0.18 Dec 27, 2022
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@@ -0,0 +1 @@
Mixing cfgs and pyo3 attributes on struct fields will now work
Copy link
Member

Choose a reason for hiding this comment

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

Needs to be 2796.changed.md (category is important so that towncrier knows where this goes on the changelog).

@davidhewitt
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 29, 2022

Build succeeded:

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.

2 participants