fix: Hot reload no longer reports Patched for a lambda or async body that passes a Span or Memory to an internal member and then fails at run time

Merged
HA
11d ago
+1494-25

Summary

A lambda, local function, async method, or iterator body that passed a Span<T>, ReadOnlySpan<T>, or
Memory<T> to a private or internal member was reported Patched, and the call then threw
MethodAccessException when it ran. Hot reload now gives these bodies the same row as the same shape with a type
no referenced assembly copies: Skipped with the existing reason where no accessor can carry the call, or a
working accessor call where one can.

Cause

The transform worker binds edited bodies with accessibility ignored, so that private and internal members
resolve. That also exposes internal types of referenced assemblies to name lookup. UnityCliLoop.0Harmony.dll
carries internal copies of ReadOnlySpan<T>, Span<T>, Memory<T>, ReadOnlyMemory<T>, MemoryExtensions
and more, so in a body those names became ambiguous and an argument of such a type got an error type. The call
then bound to nothing, every accessor check let it through, and the shim kept a direct call the runtime refuses.
Binding with accessibility enforced does not see the internal copies and binds the call normally.

Only assemblies that reference a DLL carrying such internal BCL copies are affected. Harmony is an explicitly
referenced plugin, so that is this package's own assemblies and its tests; a user assembly that references
another DLL with internal BCL copies would hit the same thing.

Change

AccessibilityRules.BoundSymbolOrNull is the one entry point for "the symbol of this node": the
accessibility-ignoring Symbol first, then the existing twin-candidate path (unchanged, only for
OverloadResolutionFailure), then the symbol the accessibility-enforcing semantic model binds. The sites where
a null symbol meant "no check" or "leave the expression as written" now call it:

SiteDecides
AccessibilityRules.BoundSymbolOrNull (5 existing callers)invocation / member access target
ShimBodyRewriter.cs VisitInvocationExpressionrewrite of a call into its accessor delegate
InaccessibleAccessScanner.cs object creation, element access, initializer assignment, assignment leftwhether the body needs an accessor at all
AccessorAccessRegistrar.cs object creation, element access, initializer assignment, assignment leftaccessor registration or refusal
AccessorAccessRegistrar.HasExpandedParamsArgumentreads the call's argument mapping from the enforcing binding when the first binding finds no call
ImplicitCallTargets.FindInaccessibleCallReasonOrNullwhen the first pass meets a missing target, enumerates the compiler's implicit calls again with the enforcing binding

The other GetSymbolInfo calls under TransformWorker~ were reviewed and left: names of fields, properties and
method groups bind without argument types, a call's target is decided at the invocation site, and the remaining
calls belong to other features or already refuse on null.

The enforcing semantic model is created inside the fallback, only when the first two steps found nothing.
Measured on a lambda with 50 such calls: about 370–500 ms per worker run, against 160–215 ms for the same body
with ArraySegment<int> (no fallback), i.e. roughly 4 ms per unbound call. The implicit-call second pass runs
only when the first meets a missing target (also normal for foreach over an array); a lambda with 50 such loops
measured 157–216 ms per worker run with and without it.

Before / after (worker rows; each shape next to its oracle, the same shape with a type no referenced assembly copies)

Shape in a lambda (or async body)BeforeAfter = oracle
internal method, ordinary / extension on a Span / extension with a Span argument / private method, ReadOnlySpan<int> argument; async body; Span<int> local; stackalloc local; _ints.AsSpan()Patched → MethodAccessExceptionSkipped, AccessorRefLikeSignatureNoShape (oracle: user ref struct)
internal method, Memory<int> argument, in a lambda or an async bodyPatched → MethodAccessExceptionPatched through the accessor, returns the edited value (oracle: ArraySegment<int>)
internal params Memory<int>[] method called with one elementPatched → MethodAccessExceptionSkipped, AccessorOptionalOrParamsArgumentNotRewritten
internal constructor, Memory<int> / ReadOnlySpan<int> argumentPatched → MethodAccessExceptionSkipped, AccessorConstructorCallNoShape
private indexer read, Memory<int> / ReadOnlySpan<int> indexPatched → MethodAccessExceptionSkipped, AccessorIndexerNoShape
private indexer write =, compound +=, increment ++, Memory<int> indexPatchedSkipped, AccessorIndexerNoShape
object initializer [memory] = value on an internal indexerPatchedSkipped, AccessorInitializerAssignmentNoShape
the constructor, indexer and initializer shapes above with the Memory<int> built from a captured local (no private member read in the lambda)PatchedSkipped, same reasons
internal method the compiler calls for a Memory<int> operand: collection initializer Add, deconstruction, positional pattern, foreach (GetEnumerator), await (GetAwaiter), query (Select)PatchedSkipped, AccessorImplicitCallNoShape
plain method body, public target, interpolated string, ^1, private Memory<int> field and propertyPatched, runsunchanged
internal member of a file the reload was not given, ReadOnlySpan<int> argumentSkipped (unpassed internal)unchanged

Why no generic "unbound call" backstop

During a run of the whole HotReload test assembly with the fallback in place, every invocation in a closure,
async, or iterator body that neither binding resolved was logged. Apart from nameof and test sources the tests
feed to the worker, they were uses of unpassed internal members (already refused), calls to a public twin class
left unbound by design, and test-infrastructure helpers. A rule that refused every unbound call would catch
nothing that is not already refused or deliberate, so it is not added.

Not in this PR

  • A method group converted to a delegate type that names Memory<T> (for example
    Func<Memory<int>, int> f = PrivateMethod;) is still refused, with a misleading "type is not visible"
    reason from the body type check. It fails closed and is left for a follow-up.
  • An interpolated string handler with an internal AppendFormatted(ArraySegment<int>) was not refused even
    without a twin type, so the handler construct has no oracle here and is not covered.
  • Not measured with BCL-twin-typed arguments: the added-member machinery (added methods, fields, properties,
    events) and RetainedNonPublicMemberUseGuard. They are not accessor checks and are left for the follow-up.

Tests

  • TransformWorkerBclTwinTypeBindingTests: every shape above, each asserting its row and reason next to its
    oracle shape. Without the fix, every twin-typed case fails and the unchanged cases pass.
  • HotReloadBclTwinTypeE2ETests: the issue's ordinary method and extension method, an internal constructor and
    a private indexer keep their compiled value without throwing; a Memory<int> argument in a lambda and in an async
    method returns the edited value through the accessor.
  • Both classes assert in OneTimeSetUp that UnityCliLoop.0Harmony still declares internal
    System.ReadOnlySpan`1 and System.Memory`1, so a Harmony update that drops them fails loudly instead of
    leaving the tests passing without exercising the fix.
  • The whole UnityCLILoop.Tests.Editor.HotReload assembly on a fresh Editor before the rebase: 4179 tests,
    4175 passed, 4 skipped (existing ignores), 0 failed. After rebasing onto the Lane B change, the two new classes
    (29) and TransformWorkerImplicitCallPatternTests / HotReloadImplicitCallPatternE2ETests (68) pass.

Docs

The known-gap sentence about a ref struct passed to a private/internal method is removed from
scope-and-limits.md (row "Private/internal access inside an async/iterator/closure body has no
accessor-delegate shape"), and the generated skill copies are regenerated.

Closes #3542