Skip to content

Conversation

@tgross35
Copy link
Contributor

@tgross35 tgross35 commented Nov 2, 2025

Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via s! still did not support
these (fixed by 79d1bdb ("Flip Copy and PartialEq, Hash
derives")). This meant that any packed struct implementing
extra_traits had handwritten implementations that are no longer
needed.

There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.

Clean up all of the above by switching from s_no_extra_traits! to s!
and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
unsafe (which will have to be addressed) or floats.

Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in libc by reading them. Practically the only way
to know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.

The new Padding type will also improve the situation here in the
future.

@tgross35 tgross35 force-pushed the derive-where-possible branch 2 times, most recently from 4a6d93f to f28ac38 Compare November 2, 2025 19:28
@tgross35 tgross35 force-pushed the derive-where-possible branch 2 times, most recently from 7a72d95 to 3025a53 Compare November 2, 2025 20:02
@tgross35 tgross35 changed the title WIP: Use derived traits in more places cleanup: Use derived traits as much as possible Nov 2, 2025
@tgross35 tgross35 force-pushed the derive-where-possible branch from 3025a53 to f9329a5 Compare November 2, 2025 20:03
@tgross35 tgross35 marked this pull request as ready for review November 2, 2025 20:07
@rustbot
Copy link
Collaborator

rustbot commented Nov 2, 2025

Some changes occurred in solarish module

cc @jclulow, @pfmooney

Some changes occurred in the Android module

cc @maurer

Some changes occurred in OpenBSD module

cc @semarie

Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via `s!` still did not support
these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash`
derives")). This meant that any packed struct implementing
`extra_traits` had handwritten implementations that are no longer
needed.

There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.

Clean up all of the above by switching from `s_no_extra_traits!` to `s!`
and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
`unsafe` (which will have to be addressed) or floats. 

Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in `libc` by reading them. Practically the only way
to know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.

The new `Padding` type will also improve the situation here in the
future.
@tgross35 tgross35 force-pushed the derive-where-possible branch from f9329a5 to 3ce0190 Compare November 2, 2025 20:12
@tgross35 tgross35 enabled auto-merge November 2, 2025 20:12
@tgross35 tgross35 added this pull request to the merge queue Nov 2, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 2, 2025
@tgross35 tgross35 added this pull request to the merge queue Nov 2, 2025
Merged via the queue into rust-lang:main with commit 141aea1 Nov 2, 2025
50 of 51 checks passed
@tgross35 tgross35 deleted the derive-where-possible branch November 2, 2025 21:38
@tgross35
Copy link
Contributor Author

tgross35 commented Nov 3, 2025

@rustbot label +stable-nominated

@rustbot rustbot added the stable-nominated This PR should be considered for cherry-pick to libc's stable release branch label Nov 3, 2025
tgross35 added a commit to tgross35/rust-libc that referenced this pull request Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via `s!` still did not support
these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash`
derives")). This meant that any packed struct implementing
`extra_traits` had handwritten implementations that are no longer
needed.

There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.

Clean up all of the above by switching from `s_no_extra_traits!` to `s!`
and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
`unsafe` (which will have to be addressed) or floats.

Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in `libc` by reading them. Practically the only way
to know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.

The new `Padding` type will also improve the situation here in the
future.

(backport <rust-lang#4806>)
(cherry picked from commit 141aea1)
[ note that this was very conflict-heavy so the cherry pick differs
  quite a bit from the original commit - Trevor ]
tgross35 added a commit to tgross35/rust-libc that referenced this pull request Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via `s!` still did not support
these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash`
derives")). This meant that any packed struct implementing
`extra_traits` had handwritten implementations that are no longer
needed.

There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.

Clean up all of the above by switching from `s_no_extra_traits!` to `s!`
and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
`unsafe` (which will have to be addressed) or floats.

Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in `libc` by reading them. Practically the only way
to know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.

The new `Padding` type will also improve the situation here in the
future.

(backport <rust-lang#4806>)
(cherry picked from commit 141aea1)
[ note that this was very conflict-heavy so the cherry pick differs
  quite a bit from the original commit - Trevor ]
tgross35 added a commit to tgross35/rust-libc that referenced this pull request Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via `s!` still did not support
these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash`
derives")). This meant that any packed struct implementing
`extra_traits` had handwritten implementations that are no longer
needed.

There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.

Clean up all of the above by switching from `s_no_extra_traits!` to `s!`
and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
`unsafe` (which will have to be addressed) or floats.

Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in `libc` by reading them. Practically the only way
to know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.

The new `Padding` type will also improve the situation here in the
future.

(backport <rust-lang#4806>)
(cherry picked from commit 141aea1)
[ note that this was very conflict-heavy so the cherry pick differs
  quite a bit from the original commit - Trevor ]
tgross35 added a commit to tgross35/rust-libc that referenced this pull request Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via `s!` still did not support
these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash`
derives")). This meant that any packed struct implementing
`extra_traits` had handwritten implementations that are no longer
needed.

There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.

Clean up all of the above by switching from `s_no_extra_traits!` to `s!`
and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
`unsafe` (which will have to be addressed) or floats.

Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in `libc` by reading them. Practically the only way
to know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.

The new `Padding` type will also improve the situation here in the
future.

(backport <rust-lang#4806>)
(cherry picked from commit 141aea1)
[ note that this was very conflict-heavy so the cherry pick differs
  quite a bit from the original commit - Trevor ]
github-merge-queue bot pushed a commit that referenced this pull request Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via `s!` still did not support
these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash`
derives")). This meant that any packed struct implementing
`extra_traits` had handwritten implementations that are no longer
needed.

There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.

Clean up all of the above by switching from `s_no_extra_traits!` to `s!`
and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
`unsafe` (which will have to be addressed) or floats.

Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in `libc` by reading them. Practically the only way
to know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.

The new `Padding` type will also improve the situation here in the
future.

(backport <#4806>)
(cherry picked from commit 141aea1)
[ note that this was very conflict-heavy so the cherry pick differs
  quite a bit from the original commit - Trevor ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants