Ensure unsigned enum initializers cast to the signed underlying type - #762
#762Merged
tannergooding merged 1 commit intoJul 14, 2026
dotnet:maindotnet/ClangSharp:mainfrom
tannergooding:fix-unsigned-enum-init-casttannergooding/ClangSharp:fix-unsigned-enum-init-castCopy head branch name to clipboard
Merged
Ensure unsigned enum initializers cast to the signed underlying type#762tannergooding merged 1 commit intodotnet:maindotnet/ClangSharp:mainfrom tannergooding:fix-unsigned-enum-init-casttannergooding/ClangSharp:fix-unsigned-enum-init-castCopy head branch name to clipboard
tannergooding merged 1 commit into
dotnet:maindotnet/ClangSharp:mainfrom
tannergooding:fix-unsigned-enum-init-casttannergooding/ClangSharp:fix-unsigned-enum-init-castCopy head branch name to clipboard
Conversation
An in-range unsigned initializer on an enum with a signed underlying type (e.g. `1U << 22` on the default `int`) was emitted without a cast, producing a `uint` assigned to an `int` member which fails to compile (CS0266). Emit an explicit cast to the underlying type in that case, matching the existing out-of-range `unchecked((int)...)` behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
tannergooding
force-pushed
the
fix-unsigned-enum-init-cast
branch
from
July 14, 2026 14:11
ead2ef4 to
e4e1e00
Compare
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.
An in-range unsigned enum initializer on an enum with a signed underlying type (e.g. shaderc's
1U << 22on the defaultint) was emitted without a cast, producing auintassigned to anintmember which fails to compile (CS0266).UncheckStmtnow emits an explicit(int)cast for that case. Since the value is in-range, nouncheckedis needed, matching the existing out-of-rangeunchecked((int)...)behavior. The newEnumConstantInitNeedsCasthelper drives this and is guarded onPreviousContext.Cursor is EnumConstantDeclso only the top-level initializer is cast, preventing recursion into nested sub-expressions.Out-of-range and signed initializers are unchanged.
Adds
WithUnsignedInitConversionTestplus the 16 baseline variants (CSharp/Xml x Default/Latest/Preview/Compatible x Windows/Unix) covering the regression.Fixes #519