Flatten anonymous bitfield sub-properties (#408 phase 2)#1727
Merged
jevansaks merged 2 commits intoJun 13, 2026
Conversation
Phase 1 (microsoft#1721) surfaces fields nested in anonymous structs/unions as [UnscopedRef] ref accessors, but only the raw bitfield backing field was reachable that way. This forwards the computed bitfield sub-properties (e.g. PSAPI_WORKING_SET_EX_BLOCK's Valid/ShareCount/Win32Protection) as value get/set properties on the declaring struct, so they can be read and written directly as value.Field instead of value.Anonymous.Anonymous.Field. Each forwarded property delegates to the nested property (readonly get => this.Anonymous...Prop; set => ... = value) and inherits its docs via <inheritdoc>. The projected type is computed from the bitfield width (1 bit => bool, else smallest fitting integer of the backing field's signedness), matching the nested property exactly, so no narrowing occurs even when the backing field is wider (e.g. nuint backing, byte/ushort sub-properties). The raw backing field continues to be surfaced as a ref (phase 1 behavior is unchanged; this is additive). Only fields reached through anonymous holders are forwarded; bitfields under a named holder are not. Gated on C# 11 like the rest of the feature. Adds syntax-shape unit tests (PSAPI_WORKING_SET_EX_BLOCK, including the named-holder negative case and the C# 10 gate) and functional tests proving the forwarded properties alias the same storage, pack without bit overlap (exact backing-field bit pattern asserted), and isolate neighbors. Validated across net8.0, net472, and net10.0.
The aligned trailing comments in FlattenedBitfieldPropertiesPackWithoutOverlap used multiple consecutive spaces, which fails the CI -warnAsError build (SA1025). Collapse to a single space before each comment.
Member
Author
|
Fixes #408 |
Member
The "Fixes" has to go in the description I'm pretty sure (which I did by editing yours) |
jevansaks
approved these changes
Jun 13, 2026
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.
Summary
Phase 2 follow-up to #1721. Phase 1 surfaces fields nested in anonymous structs/unions as
[UnscopedRef] refaccessors on the declaring struct, but for a bitfield-bearing nested struct only the raw backing field was reachable that way. This forwards the computed bitfield sub-properties (e.g.PSAPI_WORKING_SET_EX_BLOCK''sValid/ShareCount/Win32Protection) as valueget/setproperties on the declaring struct, so callers can read and write them directly asvalue.ShareCountinstead ofvalue.Anonymous.Anonymous.ShareCount.Details
readonly get => this.Anonymous...Prop; set => ... = value;) and inherits its docs via<inheritdoc>.bool, otherwise the smallest integer of the backing field''s signedness that fits), matching the nested property exactly — so no narrowing occurs even when the backing field is wider (e.g. annuintbacking field withbyte/ushortsub-properties).ref(Phase 1 behavior unchanged; this is purely additive).Tests
StructTests): forwarding shape + types onPSAPI_WORKING_SET_EX_BLOCK, a negative case proving the sibling namedInvalidholder''s bitfields are not forwarded, and the C# 10 gate.GenerationSandbox.Tests, compiled & executed): the forwarded properties alias the same storage as the nested path; all nine sub-fields pack without bit overlap (the exact backing-field bit pattern is asserted); and writes to one field do not disturb its neighbors.Fixes #408