internal: add tuple struct support in pin_data and pin_init! (V2)#155
internal: add tuple struct support in pin_data and pin_init! (V2)#155mqqz wants to merge 4 commits into
pin_data and pin_init! (V2)#155Conversation
c9dab99 to
1f2c279
Compare
Add plumbing for tuple struct support to `#[pin_data]` by generating tuple struct projections and tuple-aware pin-data metadata. Keep the projection API native to Rust tuple structs, so projected fields are accessed as `.0`, `.1`, etc. instead of introducing synthetic public field names. Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
Expand `init!` and `pin_init!` to initialize tuple structs using both indexed brace syntax and tuple-constructor syntax. Add tuple-specific runtime and UI coverage for the basic initializer forms, duplicate/missing fields, invalid indices, and syntax errors. Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
Extend tuple-struct coverage with the semantic cases that are easiest to review independently from the implementation changes. Add runtime coverage for generic and const-generic tuple structs, `Unpin` behaviour with `!Unpin` field types, pinned drop, and fallible partial-init rollback. Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
Support tuple structs whose final field or final constructor argument is removed by #[cfg]. This preserves tuple indices without needing to evaluate user cfgs in the proc macro. Reject #[cfg] on earlier tuple fields and tuple constructor arguments, because those cases would require reindexing the remaining fields after cfg stripping. That is possible to generate, but the extra complexity is not justified for the tuple struct support added here. Add regression tests for the supported final-field case and UI diagnostics for the unsupported index-shifting cases. Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
1f2c279 to
2d2042e
Compare
| return Err(syn::Error::new_spanned( | ||
| attr, | ||
| "`#[cfg]` on tuple constructor arguments is only supported on the last argument", | ||
| )); |
There was a problem hiding this comment.
This should be checked after parsing, thus would allow it to use the dcx mechanism and provide better error recovery.
| fn member_ident(member: &Member) -> Ident { | ||
| match member { | ||
| Member::Named(ident) => ident.clone(), | ||
| Member::Unnamed(Index { index, .. }) => format_ident!("_{index}"), | ||
| } | ||
| } | ||
|
|
||
| fn member_display_name(member: &Member) -> String { | ||
| match member { | ||
| Member::Named(ident) => format!("`{ident}`"), | ||
| Member::Unnamed(Index { index, .. }) => format!("index `{index}`"), | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
These should be methods of FieldInfo
| )); | ||
| field_projections.push(quote!(#binding,)); | ||
| } | ||
| field_bindings.push(quote!(ref mut #binding,)); |
There was a problem hiding this comment.
Any reason to use this instead of .0, .1, etc?
| #ty, | ||
| >, | ||
| }); | ||
| field_values.push(quote! { |
There was a problem hiding this comment.
This is changing how tuple struct is handled to be completely different from normal structs?
There's a reason that methods are used, because they support HRTB, so it makes it possible to add self-reference support later (#156)
| this: Option<This>, | ||
| path: Path, | ||
| brace_token: token::Brace, | ||
| close_span: Span, |
There was a problem hiding this comment.
| close_span: Span, | |
| delim_close_span: Span, |
| if input.peek(Token![<-]) { | ||
| return Err(input.error( | ||
| "`<-` is not supported in tuple constructor syntax; use braces with indices, e.g. `Type { 0 <- init, 1: value }`", | ||
| )); |
There was a problem hiding this comment.
I'd move this into parse_paren_initializer and drop the type.
| field_bindings.push(quote!(ref mut #binding,)); | ||
| } | ||
| let projection_init = if let Some(last_field) = fields.last() { | ||
| if let Some(cfg) = cfg_condition(&last_field.field.attrs) { |
There was a problem hiding this comment.
You should just unconditionally apply cfgs?
| let tuple_fields: Vec<_> = tuple_fields.into_iter().collect(); | ||
| let mut fields = Punctuated::new(); | ||
|
|
||
| for tuple_field in tuple_fields |
There was a problem hiding this comment.
Cfg rejection should happen before tuple struct is accepted at all, so there is no intermediate commit that allows it but performs incorrectly.
Replaces #113.
This adds tuple struct support to
#[pin_data],init!, andpin_init!.The projected form of a tuple struct is also a tuple struct, so projected fields are accessed with native tuple syntax (
.0,.1, ...), which keeps the generated API closer toordinary Rust and avoids exposing synthetic field names (also makes it easier fields are
cfg'd out).#[pin_data]pin_init!(Foo { 0 <- ..., 1: ... })pin_init!(Foo(...))(but no<-e.g.Foo(a, <- b, c)is rejected)support for tuple structs when fields or constructor arguments are removed by(Error out unless#[cfg]#[cfg]is on the final tuple field)Notes
._0like last time.cfg handling does not try to evaluate user#[cfg]conditions in the proc macroinstead, the macro generates the necessary cfg-dependent layouts and lets rustc select the active branch#[cfg]is supported only on the final tuple field / constructor argument, when it does not change numbering.Tests
Added coverage for:
#[pin]interaction with!Unpinfield typesfeature-dependent cfg field layoutsComparison with the old PR
This replaces the earlier attempt in #113.
Compared with that PR, this version is intentionally narrower and easier to review:
cfghandling wasredesigned to correctly handlesimplified to accept only on last field/arg. or error outcfg-stripped tuple fields and constructor argumentsCloses: #85