Skip to content

internal: only require one zeroable field for unions - #183

Open
miguelcorderopamphile wants to merge 1 commit into
Rust-for-Linux:mainfrom
miguelcorderopamphile:zeroable-union-one-field
Open

miguelcorderopamphile wants to merge 1 commit into
Rust-for-Linux:mainfrom
miguelcorderopamphile:zeroable-union-one-field

Conversation

@miguelcorderopamphile

Copy link
Copy Markdown

A union is zeroable when at least one of its fields is: the all-zero bit pattern is a valid value of the union if it is valid for any field. The derive previously required every field to implement Zeroable, rejecting unions that are sound to zero.

This adds a type-level "at least one" check in __internal: ZeroableCheck reports True/False per field through method resolution, the results are combined with BitOr, and IsTrue asserts the result. The failure diagnostic is:

error[E0277]: no field type of this union implements `Zeroable`

Structs are unchanged, and no new dependencies are added.

Tests:

  • New compile-fail test for a union with no zeroable field.
  • Runtime tests: a union with one zeroable field, one with all fields zeroable, and a generic union.
  • Updated the zeroable expansion snapshot.

Validated locally with cargo test --all-targets (nightly, -Dwarnings), doctests, UI tests (--cfg UI_TESTS), clippy with and without alloc, MSRV 1.82, Miri (--all-targets, --doc, and -Zmiri-tree-borrows), and cargo fmt --check --all.

Closes #43.

A union is zeroable when at least one field is: the all-zero bit pattern
is a valid value of the union if it is valid for any field. The derive
previously required every field to implement `Zeroable`, which rejected
unions that are sound to zero.

Add a type-level "at least one" check: `__internal::ZeroableCheck`
reports `True` or `False` per field through method resolution, the
results are combined with `BitOr`, and `IsTrue` asserts the result.
Structs keep requiring every field.

Signed-off-by: Miguel Cordero Pamphile <miguelcorderopamphile@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

improve derive(Zeroable) on unions

1 participant