Realsig codegen

From dotnet/dotnet · View interactive version

Realsig codegen is an AI agent skill from the GitHub repository dotnet/dotnet. Debug and fix --realsig+ (RealInternalSignature) codegen bugs in IlxGen — MethodAccessException / FieldAccessException / TypeAccessException at runtime, IL `private` vs `assembly` visibility, closure and TLR-lift placement (cloc / NestedTypeRefForCompLoc / effectiveCloc / moduleCloc). Use when a pro

How to use this skill

Skill file

--realsig+ Codegen (IlxGen)

Mental model

Key code locations (src/Compiler/CodeGen/IlxGen.fs)

Gotcha: the optimizer hides the bug in minimal repros

A trivial private member (e.g. = 1) is inlined away by the F# optimizer before codegen, so the call site disappears and the crash vanishes under --optimize+. To force a faithful repro, make the member non-inlinable: read mutable state (backing + 1) or mark it [<NoCompilerInlining>]. NOTE: [<NoCompilerInlining>] is the F# optimizer attribute; [<MethodImpl(MethodImplOptions.NoInlining)>] is the JIT attribute — for compiler-inlining experiments use NoCompilerInlining.

Repro methodology

  1. Use a shipped SDK fsc as a still-broken control: & "C:\Program Files\dotnet\sdk\<ver>\FSharp\fsc.dll" (or dotnet <fsc.dll>). Compile the same source with --realsig+ and --realsig-; the bug is the delta.
  2. Always pass --optimize+ (and a non-inlinable private) so the call survives to runtime.
  3. Inspect IL with ildasm and read nesting by indentation: a .class … C at 2-space indent with a child .class … h@N at 4-space indent = nested (good). Same indent = sibling (the bug). Confirm with the full type name in field refs, e.g. M/C/h@8 (nested) vs M/h@8 (sibling).
  4. Minimal repro shape (crashes only under --realsig+):
type C() =
    static let mutable backing = 0
    static member Set v = backing <- v
    static member private Secret() = backing + 1   // non-inlinable -> survives to runtime
type C with                                          // intrinsic augmentation
    member _.Run() =
        let rec h n = if n = 0 then C.Secret() else h (n - 1)
        h 5
  1. Compile and run (runtimeconfig pins the shared runtime):
dotnet <fsc.dll> --target:exe --out:X.dll --realsig+ --optimize+ -r:FSharp.Core.dll X.fs
# X.runtimeconfig.json:
# {"runtimeOptions":{"tfm":"net10.0","framework":{"name":"Microsoft.NETCore.App","version":"10.0.9"}}}
dotnet X.dll

Instrumentation pattern

dprintf is not in scope in GetIlxClosureFreeVars; use eprintfn to stderr. To pin where two cases diverge, log the closure's enclosing cloc and the member identity, and capture a stack trace guarded by a predicate:

// in GenMethodForBinding, after `let m = v.Range`
eprintfn "MFBDBG v=%s ext=%b cloc=[%s] apparent=%s"
    v.LogicalName v.IsExtensionMember
    (String.concat "/" eenv.cloc.Enclosing)
    (match v.ApparentEnclosingEntity with Parent e -> e.LogicalName | ParentNone -> "<none>")
if v.LogicalName = "Run" then eprintfn "STACK:\n%s" System.Environment.StackTrace

Rebuild FCS + fsc Release, compile a minimal intrinsic-vs-augmentation pair, diff the logs. Remove all instrumentation before committing.

Worked example: #19933 (PR #19955)

Members declared in an intrinsic augmentation (type C with member ...) reached GenMethodForBinding with only the module in eenv.cloc (the augmentation is a separate definition group, so the type was not in the realsig dict-routing path that intrinsic members use), so their closures nested in the module as siblings of C → MethodAccessException under realsig+. Fix: normalize eenv.cloc to mspec.MethodRef.DeclaringTypeRef for every non-extension member under g.realsig at the top of GenMethodForBinding (idempotent for members that already have it; skip v.IsExtensionMember — real extension members live in their own module). Gating on g.realsig avoids perturbing realsig- IL baselines. One fix covers let rec, task/async, and quotation-splice closures because all go through the same NestedTypeRefForCompLoc eenv.cloc site.

Diagnostics reality (don't mis-cite)

Tests

Build/run on Windows (when the cwd is content-excluded)

If the powershell tool rejects commands because it validates the first token against the repo path, invoke executables by absolute path: & "C:\Program Files\Git\cmd\git.exe" …, & "C:\Program Files\dotnet\dotnet.exe" build …. Run the built component-test dll directly: dotnet exec <…\FSharp.Compiler.ComponentTests.dll> --filter-class "*Pattern*" (xUnit v3 simple filters: --filter-class / --filter-method / --filter-namespace, * wildcard). Ensure the matching shared runtime exists under <repo>\.dotnet\shared\Microsoft.NETCore.App\.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.