Repository navigation
fix(macros): reject #[repr(C)] on enums in derive(Type) - #4508
Open
thelastbackspace wants to merge 2 commits into
Open
thelastbackspace wants to merge 2 commits into
thelastbackspace wants to merge 2 commits into
Conversation
… for enums The derive took the first path-only item of #[repr(..)] as the integer type, so #[repr(C)] produced `as C` casts that don't compile. Only use integer reprs; anything else is treated like an enum without a repr.
Collaborator
|
Did you see this comment on a prior PR I reviewed: #4403 (comment) |
repr(C) doesn't say whether the enum should be encoded as a string or as a C-like integer, and the C integer type differs per target. Instead of guessing, emit a compile error that points at #[repr(i32)] or removing the repr. Integer reprs work as before (also combined with C, e.g. #[repr(C, u8)]). Layout-only reprs like align(..), packed, transparent and Rust are still ignored since they don't affect the encoding.
#[repr(C)] and other non-integer reprs in derive(Type) for enums#[repr(C)] on enums in derive(Type)
Author
|
Yes, I saw it, thanks for the pointer. I changed this PR to reject |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Does your PR solve an issue?
fixes #4366
The enum derive picked the first path-only item in
#[repr(..)]as the integer type to encode through. With#[repr(C)]that ended up asC, so the expansion hadMyEnum::A as Cand failed with confusing errors.Following the review on #4403,
#[repr(C)]on an enum is now rejected with a clear error instead of guessing what it should map to:Other details:
u8..u128,i8..i128,usize,isize) are used as the encoded type.#[repr(C, u8)]is allowed since the integer type is explicit there.align(..),packed,transparentandRustare still ignored. They only affect layout and don't say anything about the encoding, so I didn't want to error on them.I added unit tests in
sqlx-macros-core/src/derives/attributes.rs(enum_repr_c_is_rejected,enum_repr_c_with_integer_is_accepted,enum_layout_only_reprs_are_ignored). The first two fail without the change. I also checked the error end to end on a sqlite derive.Is this a breaking change?
No. Enums with only
#[repr(C)]didn't compile before either, now they just get a better error.