Skip to content

Place ByRef write-backs where VB runs them (return/throw/yield, block headers, field initializers) - #9

Closed
GrahamTheCoder wants to merge 12 commits into
masterfrom
claude/fix-896-1081-byref-writeback-placement
Closed

GrahamTheCoder wants to merge 12 commits into
masterfrom
claude/fix-896-1081-byref-writeback-placement

Conversation

@GrahamTheCoder

@GrahamTheCoder GrahamTheCoder commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Fixes icsharpcode#896
Fixes icsharpcode#1081

Both issues come from the same gap. When a ByRef argument needs a temp (a property, a method result, an omitted Optional ByRef), VB copies the temp back straight after the call. Roslyn's LocalRewriter_Call.vb lowers the call to a sequence: the call's value, then the copy-backs as side effects. The converter instead emits the write-back after the whole converted statement. For field initializers there is no statement at all.

Root causes

  • VB -> C#: out param is set after return icsharpcode/CodeConverter#896, return/throw/yield: PerScopeState.CreateLocalsAsync adds write-backs (AdditionalAssignment) after the statement. After return or throw they can never run. After yield return they only run when the consumer resumes.
  • Block headers (Select Case, Case, For start/bound, For Each, Using, SyncLock, With, Catch … When): the "statement" is the whole block, so the body saw the old value. For a later Case clause, the write-back ran even when the clause was never evaluated. NextStatementDefinitelyExecutedAfter treated these headers like plain statements. If/ElseIf and loop conditions were already handled.
  • VB -> C#: Byref optional parameters to New field initializer can throw nullref icsharpcode/CodeConverter#1081: DeclarationNodeVisitor.CreateAdditionalLocalMembers read v.Initializer, which is null once an instance initializer has moved to the constructor. That threw a NullReferenceException. The same path dropped write-backs, left ph<guid> placeholders in constructors and initialX functions, and refused declarations with several variables or nested calls.
  • Found along the way: a partial class's initialX() function (used when the constructors are in another part) was always static. So any initializer that read an instance member failed with CS0120. The existing multi-file characterization initialAnArrayWithNonStaticInitializerReferencingOtherPart is one example.

Output by case (progressive fallback; the existing simple output is unchanged)

VB C#
F(P) as a statement, x = F(P), Dim x = F(P) unchanged: temp, call, write-back after
any shape without a write-back (e.g. Return F(New X)) unchanged single statement
Return F(P) int argv = P; long ret = F(ref argv); P = argv; return ret;
Throw CreateEx(P) / Yield F(P) same shape, with throw ex; / yield return ret;
Select Case F(P), Case F(P), For i = 0 To F(P), For Each x In F(P), Using d = F(P), SyncLock F(P), With F(P), Catch When F(P) the existing local-function fallback (as for If): int localF() { int argv = P; var ret = F(ref argv); P = argv; return ret; }, then switch (localF()) etc.
Private Second As New Outer(First) with an omitted Optional ByRef (icsharpcode#1081), instance initializer constructor Second = initSecond();, plus an instance method Outer initSecond() { string argx = ""; return new Outer(First, x: ref argx); }
Private _a As Integer = Inc(P), instance _a = Inc__a(); in the constructor; Inc__a() declares, calls, writes back and returns
Private Shared _c As Integer = Inc(SP) / any initializer that stays on the field static int Inc__c() { … SP = argv; return ret; } and _c = Inc__c();, the existing Foo__p1 style, now with the write-back
Private _d, _e As New Outer(First), or A As Integer = Inc(C), B As Integer = Inc(C) one method per variable, each emitted once
Two(p.Value, p.Value) (two ByRef arguments writing back to the same place) write-backs last argument first, as VB does; a call nested in an argument still writes back before its outer call
P = F(P), x = F(h.Items(x)), h = F(h.P) (= to a plain name or Me.X that a write-back reads) string argv = P; var ret = F(ref argv); P = argv; P = ret;
h.P = F(h.P), x += F(h.Items(x)), h.P = h.P.Ext() (compound, or a target with a receiver or index, that a write-back reads) local-function fallback: string localF() { …; h.P = argv; return ret; } then h.P = localF();
any local-function case (operands, If/ElseIf, loop conditions, block headers, Dim declarators, assignments) in a Structure, when the body uses this (including through With Me) the same local function, taking the struct by ref: int localF(ref S self) { int argv = self.P; var ret = F(ref argv); self.P = argv; return ret; }, then if (localF(ref this) > 0)
a local-function case inside With structField (class or structure) the withBlock ref local is passed by ref: int localF(ref T withBlock) { … }, then localF(ref withBlock)
a local-function case inside With b, where b is a ByRef parameter b is passed by ref: int localF(ref Box b) { … }, then localF(ref b); a ByRef parameter used only in a header that was evaluated into withBlock isn't passed, and neither is a type with the parameter's name (H.S beside <Out> ByRef h As H)
With b over a class-typed variable var withBlock = b; (it was ref var withBlock = ref b;, which followed a reassignment of b inside the block)
Return Inc(P) + ValueOf(Me) in a Structure, ValueOf(ByRef s As S) the copy of Me is taken at the call: int localValueOf(ref S self) { var args = self; var ret = ValueOf(ref args); return ret; }
Q = F(P) (assignment target not read by any write-back) unchanged compact form
Dim a = Inc(P), b = Inc(P) each declarator except a lone one takes the local-function fallback, so b reads the value a's call wrote back
WithEvents W As New Src(P) the method is emitted, and W = initW(); goes in the constructor
Partial class with constructors in another part initialX() contains the temps, and is now an instance method

The jump result uses var only when the VB expression's own type is the type the jump needs. Otherwise the converted type is written out, e.g. long ret for a widening return.

Design

  • PerScopeState.WithWriteBacksBefore (extracted in a pure refactor commit): if the current scope has write-backs, evaluate into a uniquely named local, write back, then use the local. Otherwise keep the single statement.
  • Block headers reuse the existing local-function fallback through NextStatementDefinitelyExecutedAfter, so it's a single change. Capturing each header into a variable before the block would be a little prettier, but every visitor would need its own change. For Case clauses and Catch When it would also be wrong, because they're evaluated conditionally.
  • Field initializers:
    • Each field initializer is converted in its own scope (CommonConversions.SplitVariableDeclarationsAsync).
    • If anything was hoisted, PerScopeState.CreateReturnBlockAsync builds a method body with the same machinery as method bodies: unique names, temps, write-backs before the return, and local functions. It doesn't consume the scope, so each variable sharing an initializer gets its own body.
    • DeclarationNodeVisitor takes the hoisted methods from the field scope as it emits them, so each one is emitted exactly once, for WithEvents fields too.
  • What a local function must take as a parameter is decided against its converted C# body, since that's what C# checks:
    • ref this (CS1673) and ref locals (CS8175) come from the body alone.
    • The enclosing method's ByRef parameters (CS1628) are the ones the VB call references (resolving a With receiver to its expression). The body then drops any it no longer mentions.
  • The ByRef-specific code is in CodeConverter/CSharp/ByRefArguments/:
    • ByRefArgumentHoister: temps, write-backs, and VB's reverse order per call.
    • WriteBackPlacement: whether a call needs the local function, and building it.
    • RefLocalReferences: marks references to ref locals so a local function can take them as ref parameters.
    • README.md: each case mapped to the VB/C# rules, its code, its sources and the known gaps. CodeConverter/CSharp/README.md points to it.

Decisions (accepted in review)

  1. VB -> C#: Byref optional parameters to New field initializer can throw nullref icsharpcode/CodeConverter#1081: a wrapper method, rather than temps inlined in the constructor.
    • Inlined temps would be the most idiomatic output. They aren't cheap to do safely, though:
      • The initializers are copied into every constructor, so temps from different fields, and constructor parameters or locals, would need unique names in each constructor (CS0128/CS0136).
      • A partial class whose constructors are in another part needs a function anyway.
    • An omitted Optional ByRef argument already has no write-back. C# still needs a variable for ref (out _ only works for out), so a temp is the simplest correct form.
  2. The method is static only when the initializer stays on the field. C# field initializers can't call instance methods. A constructor call needs an instance method to read instance members.
  3. WithObjectInitializerCanReadFromPropertiesOfObjectAsync expectation changed: var init = new SomeClass(); became SomeClass @init = new SomeClass();. This is the same naming a method body uses for the same initializer.
  4. Multi-file characterization PartialClassPart.cs (3 copies): static int[] initialAnArray…() lost static. It read an instance field, so it didn't compile before.

Review round 1

  • Fixed, test-first:
    • A hoisted method was emitted twice when a field declaration had several declarators or array bounds (CS0111/CS0121).
    • A WithEvents field's method was never emitted (CS0103).
  • Declined at the time and added to the README's Known gaps:
    • Reverse copy-back order. This was later fixed; see "Known gaps revisited".
    • ReadOnly auto-property write-back, which is still a gap.

Review round 2

Review converged after 2 blind rounds: round 2 found only nitpicks and quality items.

  • Fixed:
    • Generated initializer method names are now checked against existing members, using the same unique-name check as hoisted locals. Before, initW() beside a user initW(), or Inc_v() beside a field Inc_v, gave CS0111/CS0121/CS0102. Partial-class initialX() functions keep their fixed name, because another part calls them by it.
    • Removed the dead rename loop in CreateExtraMethodMembers (pure refactor).
  • Added to the README's Known gaps (they existed before this PR and are out of scope here).

Known gaps revisited

Following the rule "fix a gap if it's a small change within code this PR already changes", two more gaps are fixed, each test-first in its own commit:

  • 0cffd9b: VB copies back a call's ByRef arguments in reverse order. This follows Roslyn's copyBackArray.ReverseContents() in LocalRewriter_Call.vb.
    • WithWriteBacksInVbOrderAsync (now in ByRefArgumentHoister) collects each argument list's write-backs and hoists them last-first. A nested call has already hoisted its own by then.
    • Self-verifying tests: two arguments to the same target, a nested call, inside Return, and in a Select Case header.
    • Changed expectation: ConstructorTests.TestHoistedOutParameterLambdaUsingByRefParameterAsync now writes back i before x for F3(x, i), as VB does.
  • 9967913: in Dim a = Inc(P), b = Inc(P), b read a stale P, because every temp was hoisted before the declaration and every write-back after it.
    • Now a declarator that isn't the first doesn't count as "straight after the previous statement", and one that isn't the last isn't followed by the next statement. Both take the existing local-function fallback.
    • A single declarator is unchanged. Static locals are excluded, because their initializers become fields.

Assignment placement (found in PR #10's review; existed before this PR)

  • Bug: VB writes back straight after the call, before the enclosing assignment stores its result. The converter wrote back after the statement, so:
    • h.Plain = SetValueReturnOther(h.Plain) ended with the written-back value, not the call's result.
    • x = F(h.Items(x)) and h = F(h.Plain) wrote back through the newly assigned x / h.
  • Fix (912e14d, refined in 81f4e51): when the assigned symbol appears in an expression that's written back:
    • For = to a plain name or Me.X, the result goes through a local: var ret = call; write-backs; P = ret;. There's nothing to evaluate before the call, so this is VB's order. It's the simplest form, and it works in a struct.
    • For a compound assignment, or a target with a receiver or index, the call takes the local-function fallback. C# then evaluates the target, calls (writing back inside the call) and stores the result, in VB's order. The ret form would read the target, or its receiver, only after the write-back.
    • Assignments whose target no write-back reads keep the compact form.
  • Before / after:
    // P = F(P), before
    string argv = P;
    P = F(ref argv);
    P = argv;
    // P = F(P), after
    string argv = P;
    var ret = F(ref argv);
    P = argv;
    P = ret;
    // h.Q = F(h.Q), after
    string localF() { string argv = h.Q; var ret = F(ref argv); h.Q = argv; return ret; }
    
    h.Q = localF();
  • Tests: 4 self-verifying tests, plus the conversion test WritesBackBeforeAssignmentCompletesWhenItChangesTheWriteBackTargetAsync.
    • The target and index tests failed before the fix. So did the receiver test, but that red run also hit a bug in the test itself (since fixed), so I haven't re-run that red step.
    • The compound-assignment test is a control: it passed before and after.
  • Not detected: the same object reached through different names, e.g. h1.P = F(h2.P) when h1 Is h2.

Review round 3 (blind, 9967913..74a6a79)

Fixed, test-first, one commit each:

  • 47b99ef: a local function always declared var ret, which hid a parameter or local called ret that the argument used (CS0841). It now picks a free name, as the jump statements already do.
  • 81f4e51: P = F(P) in a Structure gave CS1673, because the local function can't use this. Simple targets now use the result-local form (see above).
  • d1d4226: in a Structure, block headers, Dim declarators, and compound or receiver-target assignments kept the statement placement from before this PR when the call used an instance member, instead of giving CS1673. (Superseded by 482b2b9, see "Structures".)
  • a09e199: an extension method whose first parameter is ByRef (h.P = h.P.Ext()) had its receiver ignored by the assignment check.
  • 92e89b1: removed a BOM that 74a6a79 added to NameExpressionNodeVisitor.cs.

Not changed:

  • The comparison is by symbol only, so a.P = F(b.P) takes the local function even when a and b are different objects. There's no sound cheap rule for "different object": two names, or Me and a local, can always refer to the same instance.
  • ArgumentConverter.ByRefArgumentHoister stays a public property.

Added to the known gaps:

Review round 4 (blind, 74a6a79..a2a214d): no new bugs

Fixed, one commit each:

  • d22baa0: in a Structure, a call in a binary operand or a condition that used an instance member still became a local function capturing this (CS1673). It was made to write back after the statement: compiling, but late. (Superseded by 482b2b9, see "Structures".)
  • c8c3931: tests for the remaining uncovered branches:
    • self-verifying: a parenthesized right side, a last Dim declarator, a Mid assignment to the written-back property (VB gives "ZZcdef"), and a ByRef extension receiver in a loop condition
    • conversion: a late-bound target (o.X = F(P), Option Strict Off), which takes the local function. That output puts the assignment on the local function's line, so the test opts out of the automatic comment-order check.

Declined: nothing else was raised.

Structures: the local function takes ref this (a116487, 482b2b9)

The pending decision on the late struct write-back is resolved: compiling code with the wrong timing is no longer emitted.

  • A local function in a struct can't use this (CS1673), but it can take the struct as a ref parameter. Where the call uses an instance member, the local function now takes ref S self, is called with ref this, and its body uses self for this. Example: int localInc(ref S self) { int argv = self.P; var ret = Inc(ref argv); self.P = argv; return ret; } then if (b && localInc(ref this) > x).
  • This replaces the after-the-statement fallback for operands, If/ElseIf, block headers, Dim declarators and compound or receiver-target assignments. It also fixes struct loop conditions, which gave CS1673 before this PR.
  • The converted body qualifies every instance member with this, including generic and extension calls and MyClass, so replacing this covers them. A nested call's local function gets ref self. The parameter's name avoids names in scope (e.g. self1 beside a parameter called self).
  • I checked the C# rules by compiling: ref this is legal in struct methods, property and event accessors, indexers, constructors (including before every field is assigned, since C# 11 structs are auto-default), iterators and async methods. Non-static local functions can still capture other locals and parameters. The one illegal context, a readonly member (CS1605), can't come from VB. VB has no read-only members, doesn't allow MyBase in a structure (BC30044), and doesn't allow Me in a structure's lambdas (BC36638). So no remaining case needs a conversion error.
  • Shared members are unchanged: there's no this there, so the local function stays parameterless.
  • Not done: hoisting a definitely-evaluated If condition into a statement before the if (var ret = F(ref argv); P = argv; if (ret)). It would be prettier for classes and structs alike, but it changes class output in many existing tests and needs per-visitor changes. With ref this, structs now match the class shape.
  • Changed expectations: the two struct conversion tests pinned the late write-back. They now pin the local function and are renamed StructureBlockHeaderDeclaratorAndCompoundAssignmentPassThisToTheLocalFunctionAsync and StructureOperandsPassThisToTheLocalFunctionAsync.
  • New tests:
    • 12 self-verifying tests on a Structure with a logging property. They cover If, ElseIf with Me.Value, a short-circuited AndAlso operand (no get or set unless called), While and Do … Loop Until, an operand followed by other evaluation, a Dim declarator followed by another, nested calls, an instance method that sees its own state, a constructor, a property getter, and a shared member.
    • A conversion test, StructureConditionsPassThisToTheLocalFunctionAsync.
    • Red first: the file's struct loop conditions gave CS1673.

Structures: review round 1 (blind, c8c3931..482b2b9)

Fixed, test-first, one commit each:

  • 55b4b4d (nitpick, pure refactor): the enclosing symbol and the operation are looked up once in HoistAndCallLocalFunctionAsync.
  • 7a2e552 (ugly output): a With block's implicit receiver counted as a use of this, so With b : If Inc(.X) > 0 on a local in a Structure got an unused ref S self. Only ContainingTypeInstance references count now. Conversion test: StructureWithBlockOnALocalDoesNotPassThisAsync. (This broke With Me; see round 2.)
  • 74c5ded (bug): With Inner, where Inner is a structure field, declares ref var withBlock = ref Inner;. A local function can't use a ref local (CS8175). This was already broken in a class; the struct change made structures reach it too.
    • Fix: references to a ref local are annotated with its type (RefLocalReferences). The local function takes each one it uses as a ref parameter of the same name, as it already did for the enclosing method's ByRef parameters: int localInc(ref T withBlock) { … } then localInc(ref withBlock).
    • Tests: self-verifying in a class and in a Structure, and the conversion test WithBlockRefLocalIsPassedToTheLocalFunctionAsync (class and structure). The conversion test was red with CS8175 first.
  • 66c9f04 (bug, existed before this PR): in a Structure, Return Inc(P) + ValueOf(Me) with ValueOf(ByRef s As S) hoisted the copy of Me (var args = this;) before Inc's write-back. VB returned 2 and C# returned 1. The same happened after AndAlso.
    • Fix: Me was never considered to need the local function. That's right for a class, where a copy is the same reference whenever it's taken, but not for a structure. A structure's Me now takes the local function like any other temporary.
    • Tests: two self-verifying tests (+ and AndAlso) and the conversion test StructureMePassedByRefAfterAWriteBackTakesTheLocalFunctionAsync, which was red first.
  • 44a635f: added these older gaps to the README; they're unchanged here:

Structures: review round 2 (blind, 482b2b9..44a635f)

Fixed, test-first, one commit each. Each test was red for the reported error first.

  • 0b2cc99 (bug, regression from 7a2e552): With Me in a Structure gave CS1673. The implicit receiver there is this, so the parameterless local function used it.
    • Fix: decide from the converted body instead of the VB operation. The body qualifies every instance member with this before simplification, so it needs ref S self exactly when it contains a this. That's what triggers CS1673, so the two can't drift apart.
    • Tests: self-verifying (With Me in the logging structure) and the conversion test StructureWithMePassesThisToTheLocalFunctionAsync.
  • e762991 (bug, regression from 74c5ded): a With over a structure field inside a lambda argument (If Apply(P, Function() … With Inner …) > 0) passed ref withBlock to the local function, but withBlock is only declared inside the lambda (CS0103). Now ref locals declared within the body are skipped.
    • Tests: self-verifying in a class, and the conversion test RefLocalDeclaredInsideTheLocalFunctionIsNotPassedAsync. That conversion test opts out of the comment-order check, because the multi-line lambda is emitted inside the local function, before the If's line.
  • 067bc7f (bug, existed before this PR, sibling of CS8175): With b, where b is a ByRef parameter (class or structure), reuses b directly as the receiver. The parameter reference is in the With header, so the local function used it without taking it (CS1628).
    • Fix: when collecting the enclosing method's ByRef parameters, a With block's implicit receiver is resolved to the With expression's operations.
    • Tests: a self-verifying test with a class and a structure ByRef parameter, and the conversion test WithByRefParameterIsPassedToTheLocalFunctionAsync.
  • 1f19e57 (quality, pure refactor): RefLocalReferences keeps the ref local's TypeSyntax, keyed by the annotation, instead of storing it as text and parsing it again.

Declined, as the reviewer suggested: ValueOf(Me) + ValueOf(Me) in a structure takes the local function for the second call even with no write-back. It's correct, and consistent with other non-variable arguments.

Structures: review round 3 (blind, 44a635f..1f19e57): no runtime bugs in this PR's changes

One commit each:

  • 879fd38 (ugly output, regression from 067bc7f): resolving a With receiver to the whole With expression gave unused ref parameters when the header had been evaluated into withBlock, e.g. With arr(i) passed arr and With GetBox(b) passed b.
    • Fix: a parent ByRef parameter is passed only when its name is used in the converted body (not as a member name). This introduced a regression; round 4's c15ced0 combines it with the VB-side check.
    • Conversion test: ByRefParameterOnlyInAWithHeaderIsNotPassedAsync, red with the unused parameters first.
  • 4645ea1 (nitpick): a ref local reference without a recorded type now throws instead of giving a null type.
  • 553c78e (missing test): StructureSharedMemberDoesNotPassThisAsync pins localInc() with no self for a Shared member of a Structure. It's a characterization test, so it passed when added.
  • bc8edc0 (bug, existed before this PR, not ByRef-specific): With b over a class-typed variable that's reassigned inside the block emitted ref var withBlock = ref b, which followed the reassignment. VB keeps the original object (VB "1,0", C# "0,1").
    • Fix: this was a small change in the With conversion this PR already touches, so it's fixed rather than listed as a gap. Only a value type (or unconstrained type parameter) gets a ref local; a class reference is copied: var withBlock = b;.
    • Self-verifying test WithOverAReassignedClassVariableKeepsTheOriginalObject, red first.
    • Changed expectations, both class types: MethodStatementTests.WithBlockWithNullConditionalAccessAsync (ref var withBlock = ref _Data; became var withBlock = _Data;), and ByRefParameterOnlyInAWithHeaderIsNotPassedAsync (var withBlock = arr[i];, so the local function takes no withBlock).
  • 0d0ecc2: the suggested gap MyBase.GetHashCode() in a hoisted argument in a Structure isn't valid VB. I compiled it and the source gives BC30044 ("'MyBase' is not valid within a structure"), so there's nothing to convert. The README now says so instead of listing a gap.

Structures: review round 4 (blind, 1f19e57..0d0ecc2)

  • c15ced0 (bug, regression from 879fd38): matching parameter names case-insensitively in the converted body, whatever they bound to, counted a type named like a parameter as a use.
    • Case 1, CS0269: Shared Function M(<Out> ByRef h As H) with If Inc(H.S) > 0 (a Shared property) gave int localInc(ref H h) { int argv = H.S; … }, called as localInc(ref h). h is unassigned at that call.
    • Case 2, ugly output: ByRef h As Integer with Inc(GetH(New H().Q).Q) passed an unused ref h.
    • Fix: the two checks are combined.
      • The candidates are the ByRef parameters the VB operations reference, with With receivers resolved to their expression (DescendantsResolvingWithReceivers is restored, without the precedence slip).
      • The converted body only drops candidates it no longer mentions, i.e. a header that was evaluated into withBlock.
      • Names compare ordinally, since the converter writes a parameter reference with its declared casing.
    • The doc comment no longer claims that a name in the body can only be the parameter or a member.
    • Tests:
      • The conversion test TypeNamedLikeAByRefParameterIsNotPassedAsync covers both cases and was red with CS0269 and the unused ref h.
      • The self-verifying test OutParameterNamedLikeItsTypeIsNotPassedToTheLocalFunction was red with CS0269.
      • The round 2/3 With and CS1628 tests still pass.
  • 3861a12: merged origin/master (no conflicts). This brings in master's MSBuild node-reuse hang fix.

Out of scope / known gaps (pre-existing, see CodeConverter/CSharp/ByRefArguments/README.md)

  • Aliasing through two names (h1.P = F(h2.P) with h1 Is h2), a ByRef New argument in an assignment (WF = New W(WF.Name)), the receiver re-evaluated on write-back (F(GetH(h).P) calls GetH twice; left to VB -> C#: Property Getter is used instead of Setter for ref argument icsharpcode/CodeConverter#909 / PR Write back ByRef parameterized properties through the setter, evaluating receiver and indexes once #10), and a setter with side effects on the written-back target.
  • ByRef arguments of New (object creation) in a block header still write back after the block. The local-function path only handles method calls (InvocationExpressionSyntax), and widening it means changing several signatures, besides giving constructors a function name and return type.
  • A non-constant For … Step is re-evaluated on every iteration. This is a separate For-conversion bug, not specific to ByRef.
  • A ReadOnly auto-property passed ByRef in a constructor or initializer is not written back. Fixing it needs VB's "assignable here" rule, and a C# target the initializer method can assign: a get-only auto-property can only be assigned in the constructor (CS0200).
  • A lambda parameter inside a ByRef argument (Function(x) Inc(x.P)): the local function is hoisted outside the lambda, which gives CS0103. The fix belongs in lambda hoisting, which this PR doesn't touch.
  • An auto-property initializer (Public Property X As Integer = Inc(P)) leaves a ph… placeholder, which gives CS0103. Properties have no way to emit an extra method, and instance-referencing property initializers aren't moved to the constructor at all, so this needs new plumbing.
  • Partial-class instance initializers run in part order, not in declaration order. Fixing that needs coordination across parts.
  • A parameterized property argument (Inc(Item(0)), CS0131; VB -> C#: Property Getter is used instead of Setter for ref argument icsharpcode/CodeConverter#909 / PR Write back ByRef parameterized properties through the setter, evaluating receiver and indexes once #10), a member of a structure returned by a property (Inc(InnerProp.P), CS1612), and Select Case Inc(P) with Case Inc(P) (CS0825).

Tests

  • Tests/TestData/SelfVerifyingTests/VBToCS/ByRefWriteBackPlacementTests.vb: 64 self-verifying tests. They cover:

    • return, return with a widening or Object conversion, return when the name ret is taken, property getter, multi-line lambda, throw, yield
    • Select Case, Case clause, a later Case that's never reached, For start and bound, For Each, nested headers, Using, SyncLock, With, Catch When
    • the VB -> C#: Byref optional parameters to New field initializer can throw nullref icsharpcode/CodeConverter#1081 repro, instance initializers in declaration order (including Inc(Counter) + Counter * 10), several variables in one declarator, several declarators with array bounds, a WithEvents field, a Shared initializer, a partial-class initializer
    • reverse write-back order (two arguments, a nested call, in Return, in a block header), and multiple Dim declarators
    • assignments that change what's written back (the target itself, an index, the receiver, an extension receiver, in a Structure), and a compound assignment
    • a local function's result name when ret is taken
    • a parenthesized right side, a last Dim declarator, Mid assignment, and an extension receiver in a loop condition
    • evaluation order in a Structure (see "Structures"), With over a structure field in a class and in a Structure, With Me in a Structure, a With inside a lambda argument, With over ByRef parameters, With over a reassigned class variable, an Out parameter named like its type, and a structure's Me passed ByRef after a write-back
    • a control test for the unchanged shapes.

    Each fix was seen red first, for the expected reason, with two exceptions:

    • the receiver-assignment test, noted under "Assignment placement"
    • the Structure assignment test, whose CS1673 I didn't reproduce myself; the round 3 reviewer reproduced it on that input.
  • These ByRefTests.cs tests pin the output:

    • WritesBackBeforeJumpingIssue896Async, WritesBackBeforeBlockBodyAsync, OmittedOptionalRefInInstanceFieldInitializerIssue1081Async (the exact repro), FieldInitializersWriteBackAsync, FieldInitializerMethodNamesAvoidExistingMembersAsync
    • WritesBackBeforeAssignmentCompletesWhenItChangesTheWriteBackTargetAsync
    • StructureBlockHeaderDeclaratorAndCompoundAssignmentPassThisToTheLocalFunctionAsync, StructureOperandsPassThisToTheLocalFunctionAsync, StructureConditionsPassThisToTheLocalFunctionAsync, StructureWithBlockOnALocalDoesNotPassThisAsync, StructureWithMePassesThisToTheLocalFunctionAsync, StructureSharedMemberDoesNotPassThisAsync
    • WithBlockRefLocalIsPassedToTheLocalFunctionAsync, RefLocalDeclaredInsideTheLocalFunctionIsNotPassedAsync, WithByRefParameterIsPassedToTheLocalFunctionAsync, ByRefParameterOnlyInAWithHeaderIsNotPassedAsync, TypeNamedLikeAByRefParameterIsNotPassedAsync
    • StructureMePassedByRefAfterAWriteBackTakesTheLocalFunctionAsync, LateBoundAssignmentTargetTakesTheLocalFunctionAsync
  • Every commit builds.

Suite vs baseline

  • Full suite at 3861a12 (after merging master; net10.0, --blame-hang): 1957 passed, 5 skipped, 0 failed, with no hang. I ran it twice: with and without a filter.
  • origin/master (cd6dd93): 1926 passed, 5 skipped, 0 failed. Master already contains this branch's tests up to c8c3931. Counting [Fact]/[Theory]/<Fact> declarations, the tip adds 31 tests over master, and 1926 + 31 = 1957.
  • Existing expectations changed only where listed above.

Coverage of changed lines

I ran the full suite with XPlat Code Coverage before review round 1. Every changed line is covered except:

  • MethodBodyExecutableStatementVisitor.GetResultTypeSyntax: half the branches of typeInfo.Type is { } && Equals(...). The null-type branch is a guard, because a ByRef call's result always has a type.
  • CommonConversions.GetInitializerMethodName: its unqualified-name branch was unreachable and has been removed.

The struct ref this branch in WriteBackPlacement.HoistAndCallLocalFunctionAsync is covered by the ByRef conversion tests. Apart from that, I haven't re-measured coverage for the commits after review round 1. Round 4 added tests for the branches its reviewer found uncovered.

🤖 Generated with Claude Code

https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg

…e#896)

VB copies a ByRef argument back to its property straight after the call.
The converter emits that write-back after the converted statement, which
after return/throw is unreachable, and after yield return only runs when
the consumer resumes.

When a jump statement has write-backs, evaluate its expression into a
local, write back, then jump with the local. Statements without
write-backs keep their single-statement form. The local uses var only
when the expression's own type is the one the jump needs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
A ByRef write-back is emitted after the converted statement. For a call
in a block header (Select Case, Case, For, For Each, Using, SyncLock,
With, Catch When) that statement is the whole block, so the body saw the
old value, and a later Case clause wrote back even when never evaluated.

These headers now take the same local-function fallback that If and
ElseIf conditions already use, so the write-back happens inside the call
site's own evaluation. Headers without write-backs keep their output.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
Pure refactor, so field initializers can reuse it. No output changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
…#1081)

A ByRef argument that needs a temporary (e.g. an omitted Optional ByRef
parameter, or a property) hoists a declaration and maybe a write-back.
A field initializer has no statement to put them in. The old code moved
them into a static method, but read v.Initializer, which is null once an
instance initializer has moved to the constructor: NullReferenceException.
That path also dropped write-backs, left placeholder names in the
constructor and partial-class initial functions, and refused fields with
several variables or nested calls.

Now each field initializer is converted in its own scope. If anything was
hoisted, the initializer becomes a call to a method built with the same
statement machinery as method bodies: temporaries, the write-backs before
returning, and local functions. The method is static when the initializer
stays on the field, and an instance method when it moves to the
constructor. Initializers without temporaries are unchanged.

WithObjectInitializerCanReadFromPropertiesOfObjectAsync: the object
initializer's temporary now gets the same unique name and explicit type as
in a method body (`SomeClass @init`), instead of an unchecked `var init`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
When a type has constructors in another part, a non-static field
initializer becomes an initialX() function that the other part's
constructor calls. It was always emitted static, so any initializer
reading an instance member (which is why it isn't on the field) failed
to compile with CS0120, including one holding ByRef temporaries.

The multi-file characterization initialAnArrayWithNonStaticInitializer...
read the instance field anInt from a static method; it now compiles.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
Coverage showed the unqualified-name branch is never hit: invoked names
are qualified (e.g. Me.Foo) before conversion. Falling back to init<Field>
would still be a valid name. No output changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
@GrahamTheCoder
GrahamTheCoder force-pushed the claude/fix-896-1081-byref-writeback-placement branch from a7363ef to 7377689 Compare November 12, 2026 01:24
@GrahamTheCoder GrahamTheCoder changed the title WIP: Place ByRef write-backs where VB runs them (return/throw/yield, block headers, field initializers) Place ByRef write-backs where VB runs them (return/throw/yield, block headers, field initializers) Nov 12, 2026
A field declaration's hoisted methods were emitted for every variable
group of every declarator, so `A As Integer = Inc(C), B As Integer =
Inc(C)` or `Arr(Inc(C)), Other As Integer` declared a method twice
(CS0111/CS0121). A WithEvents field never emitted the method its
constructor assignment calls (CS0103).

Take the functions from the field scope as they're emitted, and emit
them for WithEvents fields too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
Reverse copy-back order of a call's arguments, and ReadOnly
auto-properties written back in constructors and initializers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
Every caller now references these methods by their final name, so
replacing placeholder ids in the constructor initializers never changed
anything. Pure refactor, no output changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
A generated method like initW() or Inc_v() clashed with a user member of
the same name (CS0111/CS0102/CS0121). Use the unique-name check already
used for hoisted locals. Partial-class initialX() functions keep their
fixed name, since another part calls them by it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
Several declarators in one Dim, lambda parameters in ByRef arguments,
auto-property initializers, and partial-class initializer order.

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

Copy link
Copy Markdown
Owner Author

PRed upstream as icsharpcode#1317

GrahamTheCoder pushed a commit that referenced this pull request Nov 17, 2026
Static locals, auto-property backing fields, every kind of write and
intrinsic operators. #9's gap about re-evaluating a receiver is fixed here,
and two pre-existing gaps are added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
GrahamTheCoder pushed a commit that referenced this pull request Nov 18, 2026
Static locals, auto-property backing fields, every kind of write and
intrinsic operators. #9's gap about re-evaluating a receiver is fixed here,
and two pre-existing gaps are added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
GrahamTheCoder pushed a commit that referenced this pull request Nov 18, 2026
Static locals, auto-property backing fields, every kind of write and
intrinsic operators. #9's gap about re-evaluating a receiver is fixed here,
and two pre-existing gaps are added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
GrahamTheCoder pushed a commit that referenced this pull request Nov 18, 2026
Brings in #9's struct `ref this` local functions, With ref locals and
ByRef parameters passed as ref parameters, and their tests.

Conflicts:
- ByRefTests.cs: both sides added tests at the end; kept both sets, and
  #10's BinaryExpressionRefParameterAsync expectation.
- ByRefArguments/README.md: kept #10's gaps, dropped the Structure CS1673
  gap that #9 fixed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
GrahamTheCoder pushed a commit that referenced this pull request Nov 18, 2026
#9 now passes `ref this`, With ref locals and ByRef parameters to the
write-back local function. Pin that a parameterized property's setter
write-back and its index temporaries go through those: `self.set_Qr(tmp,
...)`, `self[i] = ...` for `Me(i)`, and `withBlock`/`s` for a With over a
structure field or ByRef parameter, in If and While conditions and in an
operand. No converter change was needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
GrahamTheCoder pushed a commit that referenced this pull request Nov 18, 2026
Brings in passing only the ByRef parameters the local function uses, and
copying a With block's class reference instead of taking a ref to it.
No conflicts; #10's expectations are unchanged by them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
GrahamTheCoder pushed a commit that referenced this pull request Nov 18, 2026
Brings in taking the local function's ByRef parameter candidates from the
VB operations, filtered by the names in the C# body, and #9's merge of
master. No conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
GrahamTheCoder pushed a commit that referenced this pull request Nov 18, 2026
…assed test

#9's new test expected `GetH(new H().Q).Q` read and then written back by
calling `GetH` again. This branch's receiver temporaries evaluate it once,
as VB does (UseTwiceRewriter), and write back to the same object:
`var tmp = GetH(new H().Q); ... tmp.Q = argv1;`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019FEr8FVAmHV9bscZ82CChg
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.

VB -> C#: Byref optional parameters to New field initializer can throw nullref VB -> C#: out param is set after return

2 participants