fix: Hot reload skips an edited member that needs unsafe code instead of failing the whole file
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
UnsafeContextRulesin 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:
unsafeblocks,unsafelocal functions,fixed, pointer and function pointer types,&x,*p,p->x. They are matched by kind, never by token, soa & banda * bstill 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;
sizeofof 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 asList<int*[]>compile without an unsafe context. - The member's own
unsafemodifier and the containing type'sunsafemodifier are not reasons by themselves. A safe method inside anunsafeclass still patches. - A
stackallocconverted toSpan<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.IsPointerLikemoved into the new helper;BaseCallRuleskeeps its pointer checks as the stand-in's own precondition on the target signature.scope-and-limits.mdlists 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.
| Shape | Before | After |
|---|---|---|
unsafe { } block, unsafe local function, unsafe block in a lambda | Failed (CS0227), safe method not applied | Skipped, safe method patched |
unsafe method using pointers, pointer body in an unsafe class, fixed, function pointer | Failed (CS0214), safe method not applied | Skipped, safe method patched |
| Pointer parameter or return type | Failed (CS0214) | Skipped |
| Property getter, setter or indexer getter with pointer code | Failed (CS0214) | Skipped |
| Pointer-typed property getter | Failed as an unattributed (shim-compile) row, no row for the safe method | Skipped with its own row, safe method patched |
Span<int> s = stackalloc int[4];, sizeof(int), safe method in an unsafe class | Patched | Patched (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
AssemblyBuilderfallback. - The patch drops the member's and the type's
unsafemodifiers, 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, plusvar p = GetPointer();,sizeofof a user struct, a call omitting an optional pointer argument and an added method with pointers are refused.Spanstackalloc,sizeof(int), a safe method in anunsafeclass, anunsafemethod with no pointers,&/*on ints, pointer types intypeof, 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 anunsafeclass and a pointer-typed property getter are Skipped while the safe method is patched; a safe method in anunsafeclass 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