Skip to content

Comments

[wip] Account for alignment directives on DSTs in derive(IntoBytes)#3064

Open
jswrenn wants to merge 1 commit intomainfrom
Gf18655827d1c30e4421c83579b962bf7aa453969
Open

[wip] Account for alignment directives on DSTs in derive(IntoBytes)#3064
jswrenn wants to merge 1 commit intomainfrom
Gf18655827d1c30e4421c83579b962bf7aa453969

Conversation

@jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Feb 23, 2026

TODO: Regression tests.

Fixes #3063


⬇️ Download this PR

Branch

git fetch origin refs/heads/Gf18655827d1c30e4421c83579b962bf7aa453969 && git checkout -b pr-Gf18655827d1c30e4421c83579b962bf7aa453969 FETCH_HEAD

Checkout

git fetch origin refs/heads/Gf18655827d1c30e4421c83579b962bf7aa453969 && git checkout FETCH_HEAD

Cherry Pick

git fetch origin refs/heads/Gf18655827d1c30e4421c83579b962bf7aa453969 && git cherry-pick FETCH_HEAD

Pull

git pull origin refs/heads/Gf18655827d1c30e4421c83579b962bf7aa453969

Stacked PRs enabled by GHerrit.

TODO: Regression tests.

Fixes #3063

gherrit-pr-id: Gf18655827d1c30e4421c83579b962bf7aa453969
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jswrenn, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the derive(IntoBytes) macro to correctly interpret and apply #[repr(align)] and #[repr(packed)] attributes when calculating padding for dynamically sized types (DSTs). It introduces new parameters to the underlying padding calculation macros and updates the derive macro to extract these alignment directives from the type's attributes, ensuring more accurate memory layout computations and addressing a known issue.

Highlights

  • Macro Signature Update: Updated the signatures of internal padding calculation macros (struct_padding!, union_padding!, enum_padding!, repr_c_struct_has_padding!) to accept new align and packed arguments.
  • Attribute Parsing: Modified the derive(IntoBytes) macro to parse #[repr(align)] and #[repr(packed)] attributes from the derived type.
  • Alignment Directive Propagation: Passed the newly parsed align and packed directives from the type's attributes to the internal padding calculation macros for more accurate layout analysis.
  • Test Updates: Adjusted existing test calls for padding macros to conform to their new argument signatures, passing None for align and packed where not explicitly specified.
  • UI Test Output Adjustment: Updated the expected error output for union.msrv.stderr UI tests to reflect changes in macro parsing behavior and error reporting.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/macros.rs
    • Updated calls to struct_padding! and union_padding! within cryptocorrosion_derive_traits! to include None, None for new align and packed arguments.
  • src/util/macro_util.rs
    • Modified struct_padding!, repr_c_struct_has_padding!, union_padding!, and enum_padding! macros to accept $align and $packed expressions.
    • Updated repr_c_struct_has_padding! to pass the new align and packed arguments to DstLayout::for_repr_c_struct.
    • Adjusted test calls for all padding macros to pass None, None for the new align and packed parameters.
  • zerocopy-derive/src/util.rs
    • Imported NonZeroU32 and quote_spanned for attribute parsing.
    • Implemented logic to parse #[repr(align)] and #[repr(packed)] attributes from the derived type's attributes.
    • Passed the extracted repr_align and repr_packed values to the validator_macro! call.
  • zerocopy-derive/tests/ui/union.msrv.stderr
    • Modified the expected output for UI tests to reflect a new macro parsing error and the removal of an old PaddingFree trait bound error.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request aims to account for alignment directives on Dynamically Sized Types (DSTs) when deriving IntoBytes. The changes involve updating internal macros to accept alignment and packing parameters and modifying the procedural macro logic to extract and pass these values. While the overall direction is correct, there are several issues to address: the use of unwrap() in the procedural macro is risky and redundant, the current implementation causes a regression in MSRV tests due to how paths are passed to macros, and the use of TODO comments violates the repository's policy of using FIXME for non-blocking issues to avoid CI failures.

Comment on lines +576 to +585
let repr_align =
repr.get_align().map(|spanned| {
let n = spanned.t.get();
quote_spanned! { spanned.span => #option::Some(#n) }
}).unwrap_or(none.clone());
let repr_packed =
repr.get_packed().map(|packed| {
let n = packed.get();
quote! { #option::Some(#n) }
}).unwrap_or(none);
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The regression in union.msrv.stderr (error: no rules expected the token ::) is likely caused by passing paths starting with :: as expr arguments to macros on the MSRV toolchain. Parenthesizing these paths when they are passed as macro arguments usually resolves this ambiguity in the macro parser.

Suggested change
let repr_align =
repr.get_align().map(|spanned| {
let n = spanned.t.get();
quote_spanned! { spanned.span => #option::Some(#n) }
}).unwrap_or(none.clone());
let repr_packed =
repr.get_packed().map(|packed| {
let n = packed.get();
quote! { #option::Some(#n) }
}).unwrap_or(none);
let repr_align =
repr.get_align().map(|spanned| {
let n = spanned.t.get();
quote_spanned! { spanned.span => (#option::Some(#n)) }
}).unwrap_or(quote! { (#none) });
let repr_packed =
repr.get_packed().map(|packed| {
let n = packed.get();
quote! { (#option::Some(#n)) }
}).unwrap_or(quote! { (#none) });

macro_rules! struct_padding {
($t:ty, [$($ts:ty),*]) => {
($t:ty, $align:expr, $packed:expr, [$($ts:ty),*]) => {
// TODO: Assert `$align` and `$packed` are `None`.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

According to the project's coding guidelines in AGENTS.md, TODO comments should only be used if they are intended to block the PR, as CI is configured to fail on them. For non-blocking improvements, please use FIXME instead.

Suggested change
// TODO: Assert `$align` and `$packed` are `None`.
// FIXME: Assert `$align` and `$packed` are `None`.
References
  1. The style guide explicitly forbids the use of TODO comments unless they are intended to block the PR, suggesting FIXME for non-blocking issues. (link)

Comment on lines +571 to +572
let repr =
Repr::<PrimitiveRepr, NonZeroU32>::from_attrs(&self.ctx.ast.attrs).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using .unwrap() here is risky as it can cause the procedural macro to panic if the repr attribute parsing fails. While the repr is likely validated earlier in the derive process, it is better to handle the error or pass the already-parsed repr into the ImplBlockBuilder to avoid redundant parsing and potential panics.

Comment on lines +574 to +575
quote! { #zerocopy_crate::util::macro_util::core_reexport::option::Option };
let none = quote! { #option::None };
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

You can simplify the construction of the Option path by leveraging the existing core_path() method on Ctx.

Suggested change
quote! { #zerocopy_crate::util::macro_util::core_reexport::option::Option };
let none = quote! { #option::None };
let core = self.ctx.core_path();
let option = quote! { #core::option::Option };
let none = quote! { #option::None };

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.87%. Comparing base (b7fc71d) to head (b4bbce6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3064   +/-   ##
=======================================
  Coverage   91.87%   91.87%           
=======================================
  Files          20       20           
  Lines        6057     6057           
=======================================
  Hits         5565     5565           
  Misses        492      492           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

derive(IntoBytes) accepts DSTs with trailing padding

2 participants