[wip] Account for alignment directives on DSTs in derive(IntoBytes)#3064
[wip] Account for alignment directives on DSTs in derive(IntoBytes)#3064
derive(IntoBytes)#3064Conversation
TODO: Regression tests. Fixes #3063 gherrit-pr-id: Gf18655827d1c30e4421c83579b962bf7aa453969
Summary of ChangesHello @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 Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| 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); |
There was a problem hiding this comment.
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.
| 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`. |
There was a problem hiding this comment.
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.
| // TODO: Assert `$align` and `$packed` are `None`. | |
| // FIXME: Assert `$align` and `$packed` are `None`. |
References
- The style guide explicitly forbids the use of
TODOcomments unless they are intended to block the PR, suggestingFIXMEfor non-blocking issues. (link)
| let repr = | ||
| Repr::<PrimitiveRepr, NonZeroU32>::from_attrs(&self.ctx.ast.attrs).unwrap(); |
There was a problem hiding this comment.
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.
| quote! { #zerocopy_crate::util::macro_util::core_reexport::option::Option }; | ||
| let none = quote! { #option::None }; |
There was a problem hiding this comment.
You can simplify the construction of the Option path by leveraging the existing core_path() method on Ctx.
| 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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
TODO: Regression tests.
Fixes #3063
derive(IntoBytes)#3064⬇️ Download this PR
Branch
git fetch origin refs/heads/Gf18655827d1c30e4421c83579b962bf7aa453969 && git checkout -b pr-Gf18655827d1c30e4421c83579b962bf7aa453969 FETCH_HEADCheckout
git fetch origin refs/heads/Gf18655827d1c30e4421c83579b962bf7aa453969 && git checkout FETCH_HEADCherry Pick
git fetch origin refs/heads/Gf18655827d1c30e4421c83579b962bf7aa453969 && git cherry-pick FETCH_HEADPull
Stacked PRs enabled by GHerrit.