Skip to content

Comments

[derive] Implement #[zerocopy(on_error = skip/fail)]#2982

Merged
joshlf merged 1 commit intomainfrom
G4f303a34ae0677fde28c03a0e395212a3bd05ab2
Feb 5, 2026
Merged

[derive] Implement #[zerocopy(on_error = skip/fail)]#2982
joshlf merged 1 commit intomainfrom
G4f303a34ae0677fde28c03a0e395212a3bd05ab2

Conversation

@joshlf
Copy link
Member

@joshlf joshlf commented Feb 4, 2026

This attribute allows deriving traits on types that do not meet
requirements (e.g. missing #[repr(C)]) by suppressing compilation
errors and emitting no implementation instead. On success, emit
#[allow(trivial_bounds)] so that rustc permits code with unsatisfied
trait bounds.

For now, we're keeping this attribute experimental, requiring users to
pass --cfg zerocopy_derive_on_error to enable it.

Closes #1769


Latest Update: v15 — Compare vs v14

📚 Full Patch History

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

Version v14 v13 v12 v11 v10 v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v15 v14 v13 v12 v11 v10 v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v14 v13 v12 v11 v10 v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v13 v12 v11 v10 v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v12 v11 v10 v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v11 v10 v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v10 v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v9 v8 v7 v6 v5 v4 v3 v2 v1 Base
v8 v7 v6 v5 v4 v3 v2 v1 Base
v7 v6 v5 v4 v3 v2 v1 Base
v6 v5 v4 v3 v2 v1 Base
v5 v4 v3 v2 v1 Base
v4 v3 v2 v1 Base
v3 v2 v1 Base
v2 v1 Base
v1 Base
⬇️ Download this PR

Branch

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

Checkout

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

Cherry Pick

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

Pull

git pull origin refs/heads/G4f303a34ae0677fde28c03a0e395212a3bd05ab2

Stacked PRs enabled by GHerrit.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @joshlf, 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 significantly enhances the flexibility of the zerocopy-derive macro by introducing a new #[zerocopy(derive_fail_silently)] attribute. This feature allows developers to opt into a mode where trait derivations that fail due to unmet requirements (such as incorrect #[repr(...)] attributes) will no longer halt compilation with an error. Instead, the macro will simply omit the trait implementation, providing a more graceful failure mechanism. Additionally, it addresses potential trivial_bounds warnings by automatically adding an #[allow] attribute to successfully derived implementations, streamlining the development experience for complex type structures.

Highlights

  • New Attribute: #[zerocopy(derive_fail_silently)]: Introduced a new attribute that allows zerocopy trait derivations to fail silently instead of emitting compilation errors when a type does not meet the required constraints (e.g., missing #[repr(C)]).
  • Error Suppression: When derive_fail_silently is enabled and a derivation requirement is not met, the macro will suppress the compilation error and emit no trait implementation for that specific trait.
  • Trivial Bounds Handling: On successful trait derivation with derive_fail_silently, the generated implementation now includes #[allow(trivial_bounds)] to prevent rustc from flagging unsatisfied trait bounds as warnings or errors.
  • Refactored Error Handling: A new error_or_fail_silently method was added to the Ctx struct to centralize the logic for either returning a compilation error or silently failing based on the new attribute.

🧠 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
  • zerocopy-derive/src/derive/from_bytes.rs
    • Modified error returns to use ctx.error_or_fail_silently for #[repr(C)] or #[repr(Int)] checks.
    • Applied ctx.error_or_fail_silently to errors related to enum variant discriminants and variant count mismatches.
  • zerocopy-derive/src/derive/into_bytes.rs
    • Updated error handling to use ctx.error_or_fail_silently for #[repr(...)] attribute validation.
    • Applied ctx.error_or_fail_silently to errors for types with type parameters and incorrect #[repr(...)] attributes.
  • zerocopy-derive/src/derive/try_from_bytes.rs
    • Adjusted derive_is_bit_valid call to return an empty TokenStream on error if fail_silently is active.
    • Added !ctx.fail_silently condition to try_gen_trivial_is_bit_valid to prevent trivial FromBytes implementation when failing silently.
  • zerocopy-derive/src/derive/unaligned.rs
    • Converted error returns to use ctx.error_or_fail_silently for #[repr(...)] attribute checks for structs, enums, and unions.
  • zerocopy-derive/src/util.rs
    • Added fail_silently: bool field to the Ctx struct to track the new attribute's presence.
    • Implemented parsing logic for the derive_fail_silently attribute within Ctx::try_from_derive_input.
    • Updated Ctx::with_input to correctly propagate the fail_silently flag.
    • Introduced Ctx::error_or_fail_silently to encapsulate the conditional error return or silent failure.
    • Added #[allow(trivial_bounds)] to the generated impl block when fail_silently is true.
  • zerocopy-derive/tests/fail_silently.rs
    • Added a new test file to validate the behavior of #[zerocopy(derive_fail_silently)].
    • Includes tests for error suppression for IntoBytes and Unaligned on a repr(Rust) struct.
    • Verifies FromBytes is still implemented for valid cases even with fail_silently.
    • Tests the trivial_bounds suppression with a generic struct.
Activity
  • The pull request introduces a new #[zerocopy(derive_fail_silently)] attribute, addressing issue Support silent failure in derives #1769.
  • Core changes involve modifying the Ctx struct to include a fail_silently flag and adding a new error_or_fail_silently utility function.
  • Existing derive logic across from_bytes.rs, into_bytes.rs, try_from_bytes.rs, and unaligned.rs has been updated to utilize this new error handling mechanism.
  • A new test file, fail_silently.rs, has been added to thoroughly validate the functionality of the new attribute, including its impact on trait implementation and trivial_bounds warnings.
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 introduces the #[zerocopy(derive_fail_silently)] attribute, which allows derives to fail silently by not emitting an implementation instead of a compilation error. This is a valuable feature, especially for conditional compilation. The implementation is well-structured, adding a fail_silently flag to the derive context and using a helper function to conditionally suppress errors. The logic to also emit #[allow(trivial_bounds)] is correctly implemented to handle cases with unsatisfied trait bounds. The changes are applied consistently across the various derive macros. I have one suggestion to improve the test coverage for this new functionality.

@codecov-commenter
Copy link

codecov-commenter commented Feb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.87%. Comparing base (735980c) to head (0df5621).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2982   +/-   ##
=======================================
  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.

@joshlf joshlf changed the title [derive] Implement #[zerocopy(derive_fail_silently)] [derive] Implement #[zerocopy(derive_fail_silently)] Feb 4, 2026
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from a5fa822 to a584347 Compare February 4, 2026 15:33
@joshlf
Copy link
Member Author

joshlf commented Feb 4, 2026

/gemini review

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 introduces the #[zerocopy(derive_fail_silently)] attribute, which allows the derive macros to suppress compilation errors and emit no implementation when requirements are not met. It also correctly adds #[allow(trivial_bounds)] when fail_silently is active, as described in the PR. The changes are consistently applied across from_bytes.rs, into_bytes.rs, try_from_bytes.rs, unaligned.rs, and util.rs, and a new test file fail_silently.rs has been added to cover the new functionality. The implementation is clean and directly addresses the stated goal.

@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch 2 times, most recently from 6cb859d to ce1dfe1 Compare February 4, 2026 15:41
@joshlf joshlf enabled auto-merge February 4, 2026 15:42
@joshlf joshlf requested a review from jswrenn February 4, 2026 15:42
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch 2 times, most recently from 160c14e to 04ef1e5 Compare February 4, 2026 15:55
@joshlf joshlf changed the base branch from main to G9c6fe23f1983e0f1a3d30e1db70e020595260e23 February 4, 2026 15:55
auto-merge was automatically disabled February 4, 2026 15:55

Merge commits are not allowed on this repository

@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from 04ef1e5 to 1a18d72 Compare February 4, 2026 16:57
@joshlf joshlf changed the title [derive] Implement #[zerocopy(derive_fail_silently)] [derive] Implement #[zerocopy(on_error = skip/fail)] Feb 4, 2026
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from 1a18d72 to 645c197 Compare February 4, 2026 17:00
Base automatically changed from G9c6fe23f1983e0f1a3d30e1db70e020595260e23 to main February 4, 2026 17:51
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from 645c197 to ffe31b7 Compare February 4, 2026 19:52
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from ffe31b7 to 465c40a Compare February 4, 2026 20:35
@joshlf
Copy link
Member Author

joshlf commented Feb 4, 2026

See also: rust-lang/rust-bindgen#3343

@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from 465c40a to af4ff84 Compare February 4, 2026 20:39
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from af4ff84 to 21d67e8 Compare February 4, 2026 21:06
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from 21d67e8 to 64e6bb9 Compare February 5, 2026 19:11
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch 2 times, most recently from 01aac40 to d47026e Compare February 5, 2026 21:05
This attribute allows deriving traits on types that do not meet
requirements (e.g. missing `#[repr(C)]`) by suppressing compilation
errors and emitting no implementation instead. On success, emit
`#[allow(trivial_bounds)]` so that rustc permits code with unsatisfied
trait bounds.

For now, we're keeping this attribute experimental, requiring users to
pass `--cfg zerocopy_derive_on_error` to enable it.

Closes #1769

gherrit-pr-id: G4f303a34ae0677fde28c03a0e395212a3bd05ab2
@joshlf joshlf force-pushed the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch from d47026e to 0df5621 Compare February 5, 2026 21:11
@joshlf joshlf added this pull request to the merge queue Feb 5, 2026
@joshlf joshlf removed this pull request from the merge queue due to a manual request Feb 5, 2026
@joshlf joshlf enabled auto-merge February 5, 2026 22:31
@joshlf joshlf mentioned this pull request Feb 5, 2026
@joshlf joshlf disabled auto-merge February 5, 2026 22:34
@joshlf joshlf enabled auto-merge February 5, 2026 22:34
@joshlf joshlf added this pull request to the merge queue Feb 5, 2026
Merged via the queue into main with commit 0cceb23 Feb 5, 2026
205 checks passed
@joshlf joshlf deleted the G4f303a34ae0677fde28c03a0e395212a3bd05ab2 branch February 5, 2026 23:12
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.

Support silent failure in derives

3 participants