Skip to content

comp: Fix alignment of bitfields in some edge cases. #3247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025

Conversation

emilio
Copy link
Contributor

@emilio emilio commented Jul 11, 2025

In order to properly compute the size of the bitfield for overaligned fields, we need to know the offset in the struct, not just in the allocation unit.

E.g., something like:

char; // Byte 1
char: 8; // Byte 2
short: 16; // Bytes 3 and 4

Should align differently than:

char: 8; // Byte 1
short: 16; // Bytes 3 and 4

If we can't find the start offset in the struct, we fall back to our previous behavior (this can happen with C++ templates for example).

We remove some code related to is_ms_struct, which was effectively dead. If someone cares about it they can resurrect it.

We also don't align per-bitfield unit, but in order to mostly preserve behavior, we keep inserting a field at the beginning of the struct rather than using repr(align). Otherwise we hit #2179 in cases where we did not use to.

We might want to extend that approach to repr(align) stuff, in order to try to fix #2179 more often, but for now do the minimal change.

Fixes #1377
Fixes #3105
Fixes #743
Fixes #981
Fixes #1314

In order to properly compute the size of the bitfield for overaligned
fields, we need to know the offset in the struct, not just in the
allocation unit.

E.g., something like:

  char; // Byte 1
  char: 8; // Byte 2
  short: 16; // Bytes 3 and 4

Should align differently than:

  char: 8; // Byte 1
  short: 16; // Bytes 3 and 4

If we can't find the start offset in the struct, we fall back to our
previous behavior (this can happen with C++ templates for example).

We remove some code related to `is_ms_struct`, which was effectively
dead. If someone cares about it they can resurrect it.

We also don't align per-bitfield unit, but in order to mostly preserve
behavior, we keep inserting a field at the beginning of the struct
rather than using repr(align). Otherwise we hit rust-lang#2179 in cases where we
did not use to.

We might want to extend that approach to repr(align) stuff, in order to
try to fix rust-lang#2179 more often, but for now do the minimal change.

Fixes rust-lang#1377
Fixes rust-lang#3105
Fixes rust-lang#743
Fixes rust-lang#981
Fixes rust-lang#1314

Co-authored-by: qinghon <[email protected]>
@emilio emilio added this pull request to the merge queue Jul 11, 2025
Merged via the queue into rust-lang:main with commit bd011d4 Jul 11, 2025
34 checks passed
@Skgland

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants