Skip to content

internal: rework how #[pin_data] handles cfg - #161

Merged
nbdd0121 merged 1 commit into
mainfrom
dev/cfg
Jul 27, 2026
Merged

internal: rework how #[pin_data] handles cfg#161
nbdd0121 merged 1 commit into
mainfrom
dev/cfg

Conversation

@nbdd0121

Copy link
Copy Markdown
Member

I'm unhappy about the cfg handling in #155, and when working on self-referential pin-init the conditional existence of fields become annoying as well. So here's an alternative way of handling this.

cc @mqqz


Attribute macros are invoked without cfg being resolved. This adds quite a bit complexity to the macro because all of the macro needs to be careful to attach necessary cfgs. This becomes especially tricky for tuple structs. Thus, it is convenient if cfgs are all resolved like derive macros.

The most optimal way to handle this is via TokenStream::expand_expr, but that is still unstable. Implement an approach where we generate two cfg-gated macro invocations with cfg resolved within the invocation. This would mean when the loop falls through, all field cfgs are resolved, so remove all handling of cfg_attrs for the rest of the macro.

Attribute macros are invoked without cfg being resolved. This adds quite a
bit complexity to the macro because all of the macro needs to be careful to
attach necessary cfgs. This becomes especially tricky for tuple structs.
Thus, it is convenient if cfgs are all resolved like derive macros.

The most optimal way to handle this is via `TokenStream::expand_expr`, but
that is still unstable. We can also create an internal derive macro and
transform the attribute macro invocation to be derive macro, but doing
requires us to serialize all extracted information in a form of helper
attributes; it would also make it more difficult if we want to make changes
to the struct (which the self-reference feature would need).

Implement an approach where we generate two cfg-gated macro invocations
with cfg resolved within the invocation. This would mean when the loop
falls through, all field cfgs are resolved, so remove all handling of
cfg_attrs for the rest of the macro.

Signed-off-by: Gary Guo <gary@garyguo.net>
@nbdd0121
nbdd0121 merged commit dc4dac7 into main Jul 27, 2026
22 checks passed
@nbdd0121
nbdd0121 deleted the dev/cfg branch July 27, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant