Don't emit unnecessary unchecked scope for native-int literals - #773
Merged
tannergooding merged 2 commits intoJul 14, 2026
Merged
Conversation
A bare integer literal whose target type resolves to a native-sized integer (nint/nuint) was wrapped in an unchecked scope whenever its value fell outside the 32-bit range, even in expression contexts where the literal keeps its natural C# type and is never narrowed. The narrowing (nint)/(nuint) cast is only emitted for VarDecl initializers, so restrict the native-int range check to that context. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…exts The literal-only check missed the constant-evaluation path (the CXEvalResult overload reached via ParenExpr/BinaryOperator .Handle.Evaluate), which still saw the un-widened nuint on an LP64 host and re-emitted the unchecked scope. Widen nint/nuint to long/ulong once at the single Stmt funnel so every recursive and constant-folded path is consistent. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
tannergooding
deleted the
tannergooding-fix-unnecessary-unchecked-literals
branch
July 14, 2026 22:46
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 #709.
A bare integer literal whose target type resolves to a native-sized integer (
nint/nuint) was wrapped in anuncheckedscope whenever its value fell outside the 32-bit range, even in expression contexts where the literal keeps its natural C# type and is never narrowed.The root cause is that on an LP64 target
unsigned longmaps tonuint, so the18446744073709551615ULliteral inSDL_size_add_check_overflowis treated asnuintandIsUncheckedapplies thenuint32-bit-range check (> uint.MaxValue) to it. But the narrowing(nuint)/(nint)cast -- the only thing that would actually overflow on a 32-bit target -- is only emitted forVarDeclinitializers (see theIsPrevContextDecl<VarDecl>gate inUncheckStmt). In a plain expression the literal just keeps its naturalulongtype, so nouncheckedis needed.This restricts the native-int range check in the
IntegerLiteralpath ofIsUncheckedto theVarDeclcontext, which is symmetric with the existing cast-emission gate:SDL_size_add_check_overflow(expression) now emitsif (b > (18446744073709551615U) - a).SIZE_MAXand friends (VarDeclinit) still emitunchecked((nuint)(18446744073709551615U)).The existing
CLongDefinesRegressionTestUnixbaseline test (added in #699 to track this) is reused as the regression; its expected output is updated to the ideal (uncheckedremoved) and the now-stale "not ideal yet" comments are refreshed. The case is inherently Unix-only -- it requires theunsigned longtonuintmapping -- so it keeps its[Platform("unix")]gate.Verified with
dotnet build -c Release(0 warnings) anddotnet test -c Release(generator suite: 3770 passed, 0 failed). The Unix-gated regression was also confirmed by temporarily un-gating it and running against the Unix-typed baseline harness on a Windows host.