fix: Hot reload skips an edited member that needs unsafe code instead of failing the whole file

Merged
HA
11d ago
+1132-22

What changed

Hot reload compiles patches without unsafe code. An edited method, property accessor or indexer accessor that needed an unsafe context failed the shim compile, and the whole file was reported Failed. Nothing in it was applied, including safe methods edited in the same file.

Such a member is now refused up front with its own Skipped row (MethodTransformUnsafeCode), so the rest of the file still applies. The row reads:

Methods and accessors that need an unsafe context (pointers, function pointers, fixed statements, or unsafe blocks) are skipped; the patch is compiled without unsafe code. Run 'uloop compile'.

  • New UnsafeContextRules in the transform worker decides whether a member needs an unsafe context:
    • a pointer-like return or parameter type in the signature (looking through arrays);
    • in the body, these syntax kinds: unsafe blocks, unsafe local functions, fixed, pointer and function pointer types, &x, *p, p->x. They are matched by kind, never by token, so a & b and a * b still patch;
    • any expression typed as a pointer, which catches var p = GetPointer(); with no pointer syntax;
    • a call or object creation whose target has a pointer parameter, which catches an omitted optional pointer argument;
    • sizeof of a type that is not built in.
  • A pointer type that only names a type is not a reason: typeof(int*), typeof(delegate*<int*, void>) and a type argument such as List<int*[]> compile without an unsafe context.
  • The member's own unsafe modifier and the containing type's unsafe modifier are not reasons by themselves. A safe method inside an unsafe class still patches.
  • A stackalloc converted to Span<T> still patches.
  • The check runs in MethodTransformDecider's hard-skip path, after the struct and generic checks. Ordinary methods, getters, setters, indexer accessors and added members all pass through it.
  • BaseCallRules.IsPointerLike moved into the new helper; BaseCallRules keeps its pointer checks as the stand-in's own precondition on the target signature.
  • scope-and-limits.md lists the new skip next to the generic-method row.

Row change in an existing test: a base call to a method with an int*[] parameter (TransformWorkerBaseCallTests) is now Skipped as MethodTransformUnsafeCode instead of MethodTransformBaseCallArgumentShape. The row is Skipped either way, and the new reason names the real cause: the call itself needs an unsafe context. The test is renamed Run_BaseCallToPointerArrayParameter_IsSkippedAsUnsafeCode.

Measured shapes

These were measured before the fix. Each shape was edited together with one safe method in the same file.

ShapeBeforeAfter
unsafe { } block, unsafe local function, unsafe block in a lambdaFailed (CS0227), safe method not appliedSkipped, safe method patched
unsafe method using pointers, pointer body in an unsafe class, fixed, function pointerFailed (CS0214), safe method not appliedSkipped, safe method patched
Pointer parameter or return typeFailed (CS0214)Skipped
Property getter, setter or indexer getter with pointer codeFailed (CS0214)Skipped
Pointer-typed property getterFailed as an unattributed (shim-compile) row, no row for the safe methodSkipped with its own row, safe method patched
Span<int> s = stackalloc int[4];, sizeof(int), safe method in an unsafe classPatchedPatched (unchanged)

Why unsafe compilation was not allowed instead

  • The target assembly's "allow unsafe code" setting never reaches the patch compile. The compile options are hard-coded, and the saved compilation list drops compiler options, so the flag would need plumbing end to end, including into the AssemblyBuilder fallback.
  • The patch drops the member's and the type's unsafe modifiers, so the shim emitter would have to put them back.
  • Private-member accessor delegates are Func<>/Action<>, and pointer types cannot be generic type arguments, so pointer-typed members would need custom delegate types.
  • Harmony patching of pointer signatures is unverified.

Tests

  • TransformWorkerUnsafeCodeTests (24 cases): every shape above, plus var p = GetPointer();, sizeof of a user struct, a call omitting an optional pointer argument and an added method with pointers are refused. Span stackalloc, sizeof(int), a safe method in an unsafe class, an unsafe method with no pointers, &/* on ints, pointer types in typeof, and pointer types as type arguments are not refused. Each case also checks that a safe method in the same file is still transformed.
  • HotReloadUnsafeCodeE2ETests (3 cases): a pointer body in an unsafe class and a pointer-typed property getter are Skipped while the safe method is patched; a safe method in an unsafe class is patched and reverts.
  • The fixtures live in a new Editor-only test assembly, UnityCLILoop.Tests.Editor.HotReloadUnsafeFixture, which allows unsafe code. The worker input builder takes an optional target assembly name to reach it.

Ran locally with class filters, all passed:

  • At the first commit: TransformWorkerUnsafeCodeTests, HotReloadUnsafeCodeE2ETests, HotReloadWorkerReasonTextTests, TransformWorkerBaseCallTests, TransformWorkerIntroducedTypeTests: 246/246; TransformWorkerClientTests, TransformWorkerIntroducedTypeAccessibilityTests: 207/207
  • After the review fixes: TransformWorkerUnsafeCodeTests, HotReloadUnsafeCodeE2ETests, TransformWorkerBaseCallTests, TransformWorkerClientTests: 210/210

Also passed: check-skill-size, sync-tool-docs --check, the file-length check, and the C# complexity check.

Closes #3548