fix: Hot reload skips a lambda or async body whose interpolated string handler, index or range, or await using and await foreach awaiter calls an internal member instead of failing at run time

Merged
HA
11d ago
+1897-15

Summary

  • Hot reload now refuses a lambda or async body that reaches an internal member through an interpolated string handler, an index or range (^1, a..b), or the awaiter that await using and await foreach call. The method keeps running its compiled body instead of being reported Patched and then throwing MethodAccessException.

User Impact

Before, each of these bodies was reported Patched. When it ran, it threw MethodAccessException from the shim's closure or state machine. Each case below was reproduced from both a lambda and an async body:

  • an interpolated string handler whose constructor, AppendLiteral, or AppendFormatted is internal. This includes ref struct handlers, a string converted to a handler-typed local, and an addition such as $"a" + $"{x}";
  • x[^1], x?[^1], or x[1..^1] on a type whose Length or Count is internal, and x[^1] or x[1..^1] whose int indexer getter or Slice is internal;
  • await using on a resource whose public pattern DisposeAsync returns an awaitable with an internal GetAwaiter, IsCompleted, or GetResult. This covers both the statement form and the declaration form;
  • await foreach whose MoveNextAsync, or whose enumerator's DisposeAsync, returns such an awaitable.

Now each of these is Skipped with the implicit-call reason. A range whose given endpoints are ints and whose end is given, such as x[1..2] or x[..2], never reads Length, so an internal Length alone does not refuse it. For example: "inaccessible method 'Length' that the compiler calls for an index or range has no accessor rewrite shape." The compiled body keeps working, and no row fails.

Out of scope:

  • fixed with an internal GetPinnableReference already fails loudly, because shims compile without unsafe code. Unsafe bodies are tracked separately.
  • A query clause that follows a clause that binds to nothing is already Skipped.
  • An internal indexer or Slice in the edited file was already refused by the accessor check.

Next: a follow-up PR covers the same constructs when the internal member lives in a file the reload was not given, plus an internal extension method whose this parameter is a type parameter. Until then, the hot-reload skill reference tells users to run uloop compile for that edit.

Changes

  • The transform worker's implicit-call check gained three cases:
    • Interpolated string handlers: it reads the handler creation operation on the string or on the addition of strings it belongs to, whatever the parent is, and checks the constructor and every append call.
    • The implicit Index and Range support, plain or after ?.: it checks Length/Count when the access reads it, and the indexer (getter, or setter for an assignment target) or Slice.
    • The awaiter of the value DisposeAsync / MoveNextAsync / the enumerator's DisposeAsync returns: the Roslyn version the worker uses has no public API for that awaiter, so await default(R) is bound speculatively at the statement.
  • New construct names in the reason: "an interpolated string handler", "an index or range", "await foreach". A constructor is shown as new Type(...).
  • The hot-reload skill reference lists the new constructs and names the unpassed-file cases that are not caught yet.
  • New fixtures and tests. Each case has a lambda variant and an async variant, plus an all-public control per construct and one end-to-end test per construct family. Two more end-to-end tests patch a lambda that slices [1..2] or [..2] on a type with an internal Length, then run it. The fixtures declare InterpolatedStringHandlerAttribute as internal, because Unity's 4.8 reference profile lacks it. With the attribute internal, every case binds identically under both the accessibility-ignoring and the strict semantic model.

Verification

  • uloop run-tests --filter-type regex over TransformWorkerImplicitCallPatternTests, HotReloadImplicitCallPatternE2ETests, TransformWorkerImplicitCallTests, TransformWorkerImplicitCallLayoutTests, HotReloadWorkerReasonTextTests, HotReloadImplicitCallE2ETests, HotReloadImplicitCallLayoutE2ETests: 297 passed, 0 failed.
  • Before the fix, the new worker tests failed: 34 failed, and the 4 controls passed. The review follow-ups were also red first: 9 tests failed before them, then all tests in the two pattern classes passed (60 after the open-start follow-up).
  • dotnet run --project tools/UnityCliLoop.CodeComplexity -- --root . --max-complexity 15: no findings.
  • check-skill-size and sync-tool-docs --check: pass.

Refs #3543