Skip to content

Account for alignment directives in derive(IntoBytes)#3064

Open
jswrenn wants to merge 1 commit intomainfrom
Gf18655827d1c30e4421c83579b962bf7aa453969
Open

Account for alignment directives in derive(IntoBytes)#3064
jswrenn wants to merge 1 commit intomainfrom
Gf18655827d1c30e4421c83579b962bf7aa453969

Conversation

@jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Feb 23, 2026

This fixes #3063 by threading alignment directives into the padding
check macros, and fixes #3067 by unconditionally emitting the padding
check even in the absence of fields (thus ensuring that there exists a
padding check into which alignment directives can be threaded).

Fixes #3063, #3067


Latest Update: v7 — Compare vs v6

📚 Full Patch History

Links show the diff between the row version and the column version.

Version v6 v5 v4 v3 v2 v1 Base
v7 vs v6 vs v5 vs v4 vs v3 vs v2 vs v1 vs Base
v6 vs v5 vs v4 vs v3 vs v2 vs v1 vs Base
v5 vs v4 vs v3 vs v2 vs v1 vs Base
v4 vs v3 vs v2 vs v1 vs Base
v3 vs v2 vs v1 vs Base
v2 vs v1 vs Base
v1 vs Base
⬇️ 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.

@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.

@codecov-commenter
Copy link

codecov-commenter commented Feb 23, 2026

Codecov Report

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

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.

@jswrenn jswrenn force-pushed the Gf18655827d1c30e4421c83579b962bf7aa453969 branch 2 times, most recently from 93d17cc to 1b397a6 Compare February 24, 2026 16:59
@jswrenn jswrenn force-pushed the Gf18655827d1c30e4421c83579b962bf7aa453969 branch from 1b397a6 to 0e20369 Compare February 25, 2026 17:58
@jswrenn
Copy link
Collaborator Author

jswrenn commented Feb 25, 2026

@joshlf This test:

#[test]
fn test_into_bytes_enum() {
macro_rules! test_repr {
($(#[$attr:meta])*) => {
$(test! {
IntoBytes {
#[$attr]
enum Foo {
Bar,
}
} expands to "expected/into_bytes_enum.expected.rs"
})*
};
}
test_repr! {
#[repr(C)]
#[repr(u8)]
#[repr(u16)]
#[repr(u32)]
#[repr(u64)]
#[repr(u128)]
#[repr(usize)]
#[repr(i8)]
#[repr(i16)]
#[repr(i32)]
#[repr(i64)]
#[repr(i128)]
#[repr(isize)]
}
}

...no longer passes, because the sub-tests no longer expand to the same thing (the different repr is now reflected in the expansion). Do you have a preference between either:

  1. splatting to different output files
  2. slimming down the test to just exercise one repr

@joshlf
Copy link
Member

joshlf commented Feb 25, 2026

@joshlf This test:

#[test]
fn test_into_bytes_enum() {
macro_rules! test_repr {
($(#[$attr:meta])*) => {
$(test! {
IntoBytes {
#[$attr]
enum Foo {
Bar,
}
} expands to "expected/into_bytes_enum.expected.rs"
})*
};
}
test_repr! {
#[repr(C)]
#[repr(u8)]
#[repr(u16)]
#[repr(u32)]
#[repr(u64)]
#[repr(u128)]
#[repr(usize)]
#[repr(i8)]
#[repr(i16)]
#[repr(i32)]
#[repr(i64)]
#[repr(i128)]
#[repr(isize)]
}
}

...no longer passes, because the sub-tests no longer expand to the same thing (the different repr is now reflected in the expansion). Do you have a preference between either:

  1. splatting to different output files
  2. slimming down the test to just exercise one repr

Let's go with (1) – should be easy to get an agent to grind through it for you.

@jswrenn jswrenn changed the title [wip] Account for alignment directives on DSTs in derive(IntoBytes) Account for alignment directives in derive(IntoBytes) Feb 26, 2026
@jswrenn jswrenn force-pushed the Gf18655827d1c30e4421c83579b962bf7aa453969 branch 2 times, most recently from 964c653 to fd1fc83 Compare February 26, 2026 16:12
This fixes #3063 by threading alignment directives into the padding
check macros, and fixes #3067 by unconditionally emitting the padding
check even in the absence of fields (thus ensuring that there exists a
padding check into which alignment directives can be threaded).

Fixes #3063, #3067

gherrit-pr-id: Gf18655827d1c30e4421c83579b962bf7aa453969
@jswrenn jswrenn force-pushed the Gf18655827d1c30e4421c83579b962bf7aa453969 branch from fd1fc83 to ea77523 Compare February 26, 2026 17:56
@jswrenn jswrenn requested a review from joshlf February 26, 2026 18:01
Copy link
Member

@joshlf joshlf left a comment

Choose a reason for hiding this comment

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

Please address these before merging, but otherwise LGTM!

#[macro_export]
macro_rules! struct_padding {
($t:ty, [$($ts:ty),*]) => {
($t:ty, $align:expr, $packed:expr, [$($ts:ty),*]) => {{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
($t:ty, $align:expr, $packed:expr, [$($ts:ty),*]) => {{
($t:ty, $_align:expr, $_packed:expr, [$($ts:ty),*]) => {{

#[macro_export]
macro_rules! union_padding {
($t:ty, [$($ts:ty),*]) => {{
($t:ty, $align:expr, $packed:expr, [$($ts:ty),*]) => {{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
($t:ty, $align:expr, $packed:expr, [$($ts:ty),*]) => {{
($t:ty, $_align:expr, $_packed:expr, [$($ts:ty),*]) => {{

#[macro_export]
macro_rules! enum_padding {
($t:ty, $disc:ty, $([$($ts:ty),*]),*) => {{
($t:ty, $align:expr, $packed:expr, $disc:ty, $([$($ts:ty),*]),*) => {{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
($t:ty, $align:expr, $packed:expr, $disc:ty, $([$($ts:ty),*]),*) => {{
($t:ty, $_align:expr, $packed:expr, $disc:ty, $([$($ts:ty),*]),*) => {{

@joshlf
Copy link
Member

joshlf commented Feb 26, 2026

Mark #3072 (which releases 0.8.40) as merge-when-ready once this PR is in the merge queue?

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 enums with padding derive(IntoBytes) accepts DSTs with trailing padding

3 participants