Fix invalid codegen for bool bitfields#727
Merged
Merged
Conversation
A bool bitfield previously emitted a bool backing field together with bool & int arithmetic and (bool) casts, none of which are valid C#. Route bool bitfields through the same unsigned-integer path used for the equivalently-sized integer type (a byte backing store), keep the public accessor typed as bool, and convert at the get/set boundary: the getter compares the masked value against zero and the setter maps the incoming bool to 0/1. Non-bool bitfields are unaffected. Fixes dotnet#508 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.
Fixes #508.
A
boolbitfield generated fully invalid C#: aboolbacking field,bool & intmasking, and(bool)casts, e.g.Fix
Route
boolbitfields through the same unsigned-integer path already used for the equivalently-sized integer type (abytebacking store), but keep the public accessor typedbooland convert at the get/set boundary:byte),... != 0),boolto0/1(value ? 1 : 0).Non-bool bitfields are untouched (an adjacent
int c : 2in the test emits identical output to before).Validation
bool/intmix compiles and round-trips correctly underTreatWarningsAsErrors.BooleanBitfieldTestcases covering thereadonly getand compatible-code accessor shapes).Known limitation (pre-existing, out of scope)
Mixing a
boolbitfield with a differently-typed bitfield of the same size in a single storage unit (e.g.bool a : 1; char b : 1;) was already emitting invalid code before this change and still is; the common all-boolcase andboolmixed with different-sized fields both work.Note
This PR description and the change were drafted with Copilot.