Skip to content

VB -> C#: keep braces around a lone declaration in loop/lock/using/single-line If bodies (CS1023) - #14

Closed
GrahamTheCoder wants to merge 2 commits into
masterfrom
claude/beautiful-wright-pu6g4d
Closed

GrahamTheCoder wants to merge 2 commits into
masterfrom
claude/beautiful-wright-pu6g4d

Conversation

@GrahamTheCoder

Copy link
Copy Markdown
Owner

No issue filed; reported directly.

Problem

While a
    Dim z = F()
End While

converted to while (a) int z = F(); → CS1023 "Embedded statement cannot be a declaration or labeled statement". Same for Do While/Until, Do ... Loop While, For, For Each, SyncLock, Using <expr>, and single-line If ... Then Dim ... / Else Dim ....

Solution

  • Root cause: Extensions.UnpackNonNestedBlock / UnpackPossiblyNestedBlock unwrapped any single-statement block without checking C#'s embedded-statement rule. C# rejects LocalDeclarationStatement, LabeledStatement and LocalFunctionStatement there (Binder.BindPossibleEmbeddedStatement).
  • Fix in one shared place: new StatementSyntax.CanBeEmbedded() guard in UnpackPossiblyNestedBlock, which UnpackNonNestedBlock now delegates to (pure refactor in its own commit first). All callers in MethodBodyExecutableStatementVisitor are covered.
  • Multi-line If/ElseIf/Else was unaffected: VisitMultiLineIfBlock and ConvertElseClauseAsync always keep the Block (only unwrapping a nested if for else if).
  • A lone label was already braced: the label's inner EmptyStatement counts as a nested statement. Pinned with a test anyway.
  • The CanBeEmbedded rule mentioned for the upstream VB -> C#: Incorrect conversion of If immediately preceded by Else icsharpcode/CodeConverter#823 branch doesn't exist on master or claude/fix-823-dangling-else, so it's added here in CodeConverter/CSharp/Extensions.cs; VB -> C#: Incorrect conversion of If immediately preceded by Else icsharpcode/CodeConverter#823 could reuse it.
  • Needs attention: the lone-label test pins pre-existing trivia (a blank line before the closing brace). Unrelated to this fix, left as is.
  • At least one test covering the code changed

Tests

  • 11 conversion tests (*KeepsBraces* in LoopStatementTests, MethodStatementTests): While, Do While, Do Until, Do…Loop While, For, For Each, lone label, single-line If, single-line Else, SyncLock, Using expression. The 10 declaration cases failed with CS1023 before the fix.
  • Self-verifying VariableScopeTests.TestLoneDeclarationAsBlockBodyStillRuns: logs each lone Dim's initializer side effect across all of these constructs and checks the log. Failed with CS1023 before the fix.
  • Full suite (net10.0, Linux): 1437 passed, 0 failed, 5 skipped. Baseline: 1425 / 0 / 5. The difference is the 12 new tests.
  • Not done: coverage report, mutation testing and blind review rounds.

Process

  • Attachment level: low. Written by an LLM (Claude via Claude Code) from AGENTS.md and a bug report prompt.

Generated by Claude Code

Pure refactor so the single-statement unpacking rule lives in one place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FpweGikrBKYdcSCS1
C# forbids local declarations, labeled statements and local functions as
the embedded statement of if/else/while/do/for/foreach/lock/using (CS1023,
Binder.BindPossibleEmbeddedStatement). The block unpacking used by
While, Do, For, For Each, SyncLock, Using (expression form) and
single-line If/Else removed the braces around a lone Dim, giving e.g.
"while (a) int z = F();". Multi-line If was unaffected because it always
keeps its block.

A lone label was already kept braced (its EmptyStatement counts as nested);
a test pins that too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FpweGikrBKYdcSCS1
@GrahamTheCoder

Copy link
Copy Markdown
Owner Author

Superseded: icsharpcode#1322

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