Fix #3826: wrap an overflowing constant subexpression in unchecked()#3828
Open
sailro wants to merge 1 commit into
Open
Fix #3826: wrap an overflowing constant subexpression in unchecked()#3828sailro wants to merge 1 commit into
sailro wants to merge 1 commit into
Conversation
…nchecked() After inlining turns a runtime accumulator (e.g. a hand-written GetHashCode prime chain `h = h * -1521134295 + ...`) into one expression, the leading `SEED * PRIME` becomes a compile-time constant subexpression. C# always evaluates constant subexpressions in a checked context, so emitting it bare fails to compile with CS0220, even though the surrounding context is unchecked. HandleBinaryNumeric now annotates such an overflowing constant binary operation with ExplicitUncheckedAnnotation (instead of the implicit UncheckedAnnotation), so AddCheckedBlocks wraps it in an explicit unchecked(...) - mirroring the existing handling of overflowing constant n(u)int casts. Assisted-by: Copilot:claude-opus-4.8:GitHub Copilot CLI
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes decompiled C# output failing to recompile when an inlined “hash accumulator” turns into a compile-time constant subexpression that overflows in checked constant-evaluation (CS0220). The change ensures the decompiler emits an explicit unchecked(...) wrapper only for the overflowing constant subexpression, preserving original unchecked runtime semantics and keeping non-constant arithmetic unchanged.
Changes:
- Update
ExpressionBuilder.HandleBinaryNumericto applyAddCheckedBlocks.ExplicitUncheckedAnnotationfor unchecked binary ops that become overflowing compile-time constants under checked evaluation. - Add
ConstantBinaryOperatorOverflows(...)helper to detect checked-context overflow for already-constant binary operations. - Add a new correctness test case (
UncheckedConstants) and wire it intoCorrectnessTestRunner.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs | Detects overflowing constant subexpressions in unchecked binary ops and forces explicit unchecked(...) emission via ExplicitUncheckedAnnotation. |
| ICSharpCode.Decompiler.Tests/TestCases/Correctness/UncheckedConstants.cs | Adds a repro-style hash-chain test that becomes a constant overflow after inlining. |
| ICSharpCode.Decompiler.Tests/CorrectnessTestRunner.cs | Registers the new UncheckedConstants correctness test in the test runner. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 #3826.
A common hand-written
GetHashCodeuses a local accumulator:Because
hashCodeis a local, every* -1521134295is a runtime operation andcompiles fine. The decompiler inlines the accumulator into one nested expression,
so the leading
1688038063 * -1521134295becomes a compile-time constantsubexpression. In C#, constant subexpressions are always evaluated in a checked
context regardless of the project's overflow setting, so the decompiled output
failed to compile with CS0220 ("operation overflows at compile time in checked
mode").
Fix
HandleBinaryNumericnow detects an unchecked binary operation whose result is acompile-time constant that overflows in a checked context, and annotates it with
ExplicitUncheckedAnnotationsoAddCheckedBlocksemits an explicitunchecked(...)wrapper. This mirrors the existing handling of an overflowingconstant cast to
nint/nuint(TranslatedExpression.cs).Before:
After:
Only the overflowing constant subexpression is wrapped; non-constant
multiplications are left as-is.
Test
Correctness/UncheckedConstantsexercises the inlined-accumulator hash. Withoutthe fix the decompiled output fails to recompile (CS0220); with it the output
recompiles and produces identical results.
Real-world occurrence: DiffPlex
DiffPiece.GetHashCode(net45).