Skip to content

Convert float/double += 1 into increment and decrement expressions - #3933

Merged
siegfriedpammer merged 3 commits into
masterfrom
test-cases-fp-types-2026
Aug 3, 2026
Merged

Convert float/double += 1 into increment and decrement expressions#3933
siegfriedpammer merged 3 commits into
masterfrom
test-cases-fp-types-2026

Conversation

@siegfriedpammer

Copy link
Copy Markdown
Member

Revives the 2020 test-cases-fp-types fixture work: ~770 lines of compound-assignment / increment / decrement coverage for float, double, and decimal in CompoundAssignmentTest.cs, mirroring the existing integer sections (fields, properties, params, locals, ref returns, custom-struct receivers).

The revived fixtures exposed an asymmetry: post-increment/decrement on float/double round-tripped as x++/x--, but the pre forms decompiled to x += 1f, because the increment detection in PrettifyAssignments only accepted integer constants. Since C# defines ++/-- on floating-point types as adding/subtracting exactly 1, a constant-1 float/double operand now also converts. Decimal needed no change (op_Increment/op_Decrement path).

Verified: CompoundAssignmentTest green in all 24 configs, full decompiler suite green on Linux (3082 passed / 0 failed / 44 skipped).

🤖 Generated with Claude Code

@christophwille
christophwille requested a review from Copilot July 29, 2026 06:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Updates increment/decrement detection in the C# decompiler so float/double pre-increment/decrement can round-trip as ++x/--x (instead of x += 1f), and expands the pretty-printing fixture coverage for floating-point and decimal compound assignments.

Changes:

  • Extend PrettifyAssignments increment/decrement detection to accept constant-1 operands for float and double.
  • Revive/expand CompoundAssignmentTest fixtures with extensive float/double/decimal compound-assignment and inc/dec scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ICSharpCode.Decompiler/CSharp/Transforms/PrettifyAssignments.cs Broadens ++/-- detection from integer-only constants to also cover float/double constant-1 cases.
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs Adds large fixture coverage for compound assignments and inc/dec over float, double, and decimal across many receiver shapes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ICSharpCode.Decompiler/CSharp/Transforms/PrettifyAssignments.cs Outdated
Comment thread ICSharpCode.Decompiler/CSharp/Transforms/PrettifyAssignments.cs Outdated
@siegfriedpammer
siegfriedpammer force-pushed the test-cases-fp-types-2026 branch 6 times, most recently from 0495d9e to f8ca20d Compare August 3, 2026 06:38
Reviving the 2020 test-cases-fp-types fixtures (compound assignment on
float, double and decimal) exposed an asymmetry: post-increment and
post-decrement on float/double round-tripped as x++/x--, but the pre
forms came back as x += 1f because the increment detection in
PrettifyAssignments only accepted integer constants. C# defines ++/--
on floating-point types as adding or subtracting exactly 1, so the
conversion is exact for a constant 1 operand. Decimal already works
through the op_Increment/op_Decrement path.

Assisted-by: Claude:claude-fable-5:Claude Code
Operator precedence made the condition read
(setting && Add) || Subtract, so 'x -= 1' was converted to 'x--' even
with IntroduceIncrementAndDecrement disabled. Only observable with the
non-default setting, which no fixture configuration exercises, so no
test accompanies the fix.

Assisted-by: Claude:claude-fable-5:Claude Code
@siegfriedpammer
siegfriedpammer force-pushed the test-cases-fp-types-2026 branch from f8ca20d to affceb2 Compare August 3, 2026 06:46
Comment thread ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs Outdated
decimal has no IL literal: legacy csc compiles 0m to a Decimal.Zero
field load, which already decompiled to the literal, but Roslyn
compiles it to a zero-initialization, which decompiled to
default(decimal). The two forms are bit-identical for decimal, so the
literal is no less faithful to the IL and matches what a human writes;
it also removes the per-compiler split in the CompoundAssignmentTest
fixture.

Assisted-by: Claude:claude-fable-5:Claude Code
@siegfriedpammer
siegfriedpammer force-pushed the test-cases-fp-types-2026 branch from f34dd80 to 7852e7d Compare August 3, 2026 18:54
@siegfriedpammer
siegfriedpammer merged commit f8617a0 into master Aug 3, 2026
15 checks passed
@siegfriedpammer
siegfriedpammer deleted the test-cases-fp-types-2026 branch August 3, 2026 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants