Registry Mirror
Skip to content

Conversation

@LiquidSnake0

Copy link
Copy Markdown
Contributor

ILSpy lost the foreach whenever the loop variable was used in a named argument. The cause: inlining was allowed to move an expression into a named-argument block, but nothing was allowed to move it back out. Two test cases added, full decompiler test suite green on Linux.


The description below was written by an AI agent (Claude, Claude Code) under my supervision.

Fixes #4153.

Cause. Inlining may extend a CallWithNamedArgs block (NamedArgumentTransform.CanExtendNamedArgument): it moves an expression into the first named-argument store, or the second one for an instance call, and checks reordering against the stored values. The reverse move was refused on both counts:

  • Block.CanInlineIntoSlot returned false for every slot of a CallWithNamedArgs block;
  • ILInlining.CanMoveInto checked reordering against the whole stloc of the named-argument temporary, which carries MayWriteLocals and therefore never reorders with the Current getter call.

StatementBuilder.DetectGetCurrentTransformation relies on ILInlining.CanUninline to pull the Current getter back out of the loop body's first statement. Once the getter had been inlined into a named-argument block, that failed and the loop was emitted as using + while (MoveNext()).

Fix. Both checks now mirror what inlining allows: CanInlineIntoSlot accepts slot 0 of a named-argument block (and slot 1 for an instance call, whose slot 0 holds the this argument), and the predecessor reorder check in CanMoveInto / CanMoveIntoCallVirt looks at the stored value of a named-argument store rather than the store itself. A named-argument temporary is read only by the block's final call, so the store imposes no ordering constraint of its own.

Tests. Two cases added to TestCases/Pretty/NamedArguments.cs: a static call over a Dictionary<,> enumerator (the repro from the issue) and an instance call over a List<KeyValuePair<,>> enumerator (the this-argument slot). Red before the fix on all 16 configurations, green after. Full ICSharpCode.Decompiler.Tests run on Linux: 3765 tests, 0 failed, 249 skipped (Windows-only / missing fixtures).

LiquidSnake0 and others added 2 commits October 9, 2026 16:38
…loop variable

Inlining may extend a named-argument block: CanExtendNamedArgument moves an
expression into the first named-argument store (or the second one for an
instance call) and checks reordering against the stored values. The reverse
move was refused: Block.CanInlineIntoSlot rejected every slot of a
CallWithNamedArgs block, and CanMoveInto checked reordering against the whole
store instruction, which counts as a write to a local. The foreach pattern
relies on CanUninline to pull the Current getter back out of the loop body's
first statement, so once the getter had been inlined into a named-argument
block the loop was emitted as using + while (MoveNext()). Both checks now
mirror what inlining allows, so an expression inlined into a named-argument
block can always be un-inlined again.

Assisted-by: Claude:claude-fable-5-1:Claude Code
@siegfriedpammer

Copy link
Copy Markdown
Member

Thank you for the contribution!

@siegfriedpammer
siegfriedpammer merged commit 65182b8 into icsharpcode:master Oct 11, 2026
9 checks passed
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.

foreach is lost when a named argument inlines the loop variable

2 participants