SURGERY_AUDIT.md raw

Language-Surgery Audit Checklist

Run this after any commit that changes compiler passes, SSA flags, lowering transforms, or runtime conventions. Not exhaustive — a ready-made "suspect this first" list for weird behavior.

The meta-pattern: TinyGo set SSA flags intending to replace a subsystem with its own emission. Every such flag is a fork in the road where the surgery might have taken only one path. Moxie added more forks.

The ten-minute sweep

1. Init chain emission

cross-package init chain. Verify each backend's generated entry point iterates packages in dep order and calls pkg.init() for each.

package. Confirm x is set before main runs.

2. Anonymous functions / closures

automatic closure lifting — backends must handle FreeVars manually.

func() value after the constructor returns.

3. Interface method tables

reach getMethodsString with zero methods? That yields empty signature strings and NPE in transform/interface-lowering.go.

non-empty case?

the compiler.

4. Reflect elimination

in src/ or removed.

must error at link time.

5. Intra-package var order

not backend-level). BareInits does not touch this.

6. Spawn static analysis

CompilePackage before OptimizePackage.

Currently does not (restrict.go:checkSpawnMoveRestriction only walks same-block forward). If the surgery extends to flow analysis, verify dominator-tree traversal.

silently skips Codec checks. Should be a hard error.

if-branch use is currently not flagged.

7. Goroutine / scheduler

restrict.go:isUserPackage still accurate (runtime*, internal/*, os, syscall, unsafe)?

other than "none" reachable? If not, strip the branches.

8. Defer chains

9. SSA pass ordering

after Build() - not inside CompilePackage.

10. Generated-output smoke

Diff the entry point / init-chain structure against a known-good baseline.

When to run

When a check fires

Don't patch symptoms. Trace back to whether the underlying assumption (SSA flag, pass order, backend responsibility) is what it should be. The bug is usually a seam between TinyGo's assumptions and Moxie's surgery, not a single wrong line.