Skip to content

Add ResetCompilerGeneratedNameState to compiler-generated name generators#20017

Draft
NatElkins wants to merge 1 commit into
dotnet:mainfrom
NatElkins:fix-generated-name-counter-reset
Draft

Add ResetCompilerGeneratedNameState to compiler-generated name generators#20017
NatElkins wants to merge 1 commit into
dotnet:mainfrom
NatElkins:fix-generated-name-counter-reset

Conversation

@NatElkins

Copy link
Copy Markdown
Contributor

Summary

Adds an internal ResetCompilerGeneratedNameState method to the compiler-generated name generators (NiceNameGenerator, StableNiceNameGenerator, and an aggregate on CompilerGlobalState) that restores the fresh-process generated-name layout.

Why

Compiler-generated occurrence names (name@line-N) are allocated from counters on CompilerGlobalState that accumulate across compilations. When a warm checker re-emits the same project in-process, an unchanged closure gets a different occurrence suffix than the previous emit (for example data@10 in one emit and data@10-3 in the next). Consumers that need to align generated names across successive compilations then cannot match them.

The concrete consumer is F# hot reload (#19941): Edit-and-Continue delta emission from a warm checker needs each emit to lay out generated names exactly as a fresh process would, otherwise unchanged closures appear renamed and the delta mapping fails. Measured in the hot reload spike: a one-method edit produced an ambiguous synthesized-type mapping purely because of this drift.

There is intentionally no in-tree caller in this PR; the caller lands with the hot reload emit path in #19941. This PR carries the primitive plus tests so it can be reviewed on its own.

What

  • NiceNameGenerator.ResetCompilerGeneratedNameState() clears the per-(name, file) occurrence counters.
  • StableNiceNameGenerator.ResetCompilerGeneratedNameState() clears the cached stable names and the inner generator's counters.
  • CompilerGlobalState.ResetCompilerGeneratedNameState() resets all three generator fields.
  • Callers must ensure no compilation is concurrently generating names (documented on the aggregate).

Tests

CompilerGlobalStateTests.fs (new): proves names drift across repeated generation without reset; that replaying the same call sequence after reset reproduces the exact original names; that the stable-name cache itself is cleared (same (name, uniq) re-queried with a different range is recomputed, not served stale); and that the aggregate reset reaches all three generators.

Verified locally: FSharp.Compiler.Service.fsproj builds clean, the new test class passes 3/3, fantomas --check clean.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

❗ Release notes required

You can open this PR in browser to add release notes: open in github.dev


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/11.0.100.md

NatElkins added a commit to NatElkins/fsharp that referenced this pull request Jul 1, 2026
…ethod CDI emission

Adds an internal AbstractIL module implementing, byte for byte, the three Portable PDB
CustomDebugInformation blob formats Roslyn persists per method for Edit and Continue
(EnC Local Slot Map, EnC Lambda and Closure Map, EnC State Machine State Map), with
serializers, deserializers, a portable PDB read-back helper, and an occurrence-key
packing helper for deterministic syntax-offset slots.

Plumbs an optional methodCustomDebugInfoRows side channel through the IL binary writer
options into the portable PDB generator so a compilation can attach CDI rows to named
methods. Names that do not identify exactly one method row are dropped. All existing
writer call sites pass an empty map, so emitted PDBs are byte-identical to before.

No in-tree caller populates the map yet; the consumer is the F# hot reload work in
dotnet#19941, following the same pattern as dotnet#20017 (land isolated, test-covered
infrastructure first, wire the feature later).

Tests: blob round-trips, Roslyn golden-byte encodings, cross-validation against
CDI blobs emitted by a real Roslyn compilation, fail-closed occurrence-key packing
(including an int32-overflow regression where a wrapped negative key previously
escaped the bound check), and end-to-end synthetic PDB emission proving correct
MethodDef parenting, zero rows for an empty map, and no rows for absent or
ambiguous names.
NatElkins added a commit to NatElkins/fsharp that referenced this pull request Jul 1, 2026
Adds an internal, standalone ECMA-335 Edit-and-Continue metadata delta writer to
AbstractIL: delta #- table stream and heap construction (DeltaMetadataTables,
DeltaMetadataSerializer, DeltaTableLayout, DeltaIndexSizing), ECMA-335 II.24.2.6
coded-index encoding (DeltaMetadataEncoding), EncLog/EncMap emission, generation GUID
chaining, user-string and standalone-signature token calculators (IlxDeltaStreams),
and the coordinating writer (FSharpDeltaMetadataWriter) over a plain row-description
input model (DeltaMetadataTypes, ILDeltaHandles, ILMetadataHeaps).

The writer's inputs are row records (names, tokens, signatures, RVAs) plus heap
offsets; it has no dependency on any semantic diffing or session machinery. It
compiles with no in-tree consumer by design: the consumer is the F# hot reload work
in dotnet#19941, following the same upstreaming pattern as dotnet#20017 and dotnet#20018
(land isolated, test-covered infrastructure first, wire the feature in a later PR).

One line of ilwrite.fsi is touched to expose the pre-existing markerForUnicodeBytes
so the delta writer reuses the exact string-marker logic of the full writer. No
behavior change for any existing code path.

Tests (130): coded-index encodings asserted against the production definitions and
ECMA-335 II.24.2.6 order, System.Reflection.Metadata reader parity over emitted
deltas, EncLog/EncMap correctness, stream layout, heap and index sizing,
multi-generation heap-offset chaining asserted against computed expected values,
standalone-signature rows asserted at baseline+1 from a real seeded baseline, and
serializer failure paths.
@NatElkins

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 20017 in repo dotnet/fsharp

NatElkins added a commit to NatElkins/fsharp that referenced this pull request Jul 2, 2026
Per-edit hot reload currently requires an external 'dotnet build -t:Compile' to
refresh the obj assembly the delta emitter reads, making every edit pay a full
MSBuild+fsc invocation on top of the in-process check (measured ~7s/edit vs
~4.7s for a plain build). This adds an experimental, flag-gated path that
produces the same on-disk obj DLL and PDB in-process from the checked project
the session already has, collapsing the two compiles into one (spike-measured
~3.0s/edit with a correct one-method delta).

- FSharpChecker.CompileFromCheckedProject (internal): emits the assembly from
  cached typecheck results (finalize CCU, dedupe QualifiedNameOfFile, optimize,
  IlxGen, write DLL + portable PDB to the requested path), adapted from the
  dotnet#19267 prototype. Before codegen it clears leaked hot-reload
  closure-name state and resets the compiler-generated-name counters
  (ResetCompilerGeneratedNameState) so the emit lays out generated names exactly
  like a fresh-process build; without this an unchanged closure drifts to a new
  occurrence suffix and the delta mapping degenerates.
- FSharpCheckProjectResults.CompilationData and TcImports.NormalizeAssemblyRef
  (internal) supply the compilation inputs, from the same prototype.
- ResetCompilerGeneratedNameState on the name generators, matching
  dotnet#20017 so the branch converges with main when that PR lands.
- Session wiring: when FSHARP_HOTRELOAD_INPROCESS_COMPILE is truthy,
  EmitHotReloadDelta runs the in-process compile after session-active
  validation and before the output freshness pipeline, failing closed as
  DeltaEmissionFailed on any compile error. With the flag unset the code path
  is unchanged beyond a single environment read. The external build remains
  the default; dotnet-watch integration opts in separately.
- The emitted portable PDB keeps the sibling-PDB freshness contract: sequence
  points for line-shifted, unedited methods come from the new compile, not a
  stale external-build PDB (regression-tested; the test fails without the PDB
  emission).

Verified: service HotReload suite 414 passed 0 failed (including the new
flag on/off contract test asserting one updated method with no external
rebuild, a +1 LineUpdate for an unedited shifted method, and the unchanged
stale-output refusal with the flag off); component HotReload suite 229 passed
0 failed (flag-off neutrality).
…tors

Compiler-generated occurrence names (name@line-N) are allocated from process-wide
counters on CompilerGlobalState that accumulate across compilations. When a warm
checker re-emits the same project in-process, an unchanged closure therefore gets a
different occurrence suffix than the previous emit, so consumers that align generated
names across compilations (Edit-and-Continue delta emission, dotnet#19941)
cannot match them.

Add an internal ResetCompilerGeneratedNameState to NiceNameGenerator (clears the
per-(name, file) occurrence counters), StableNiceNameGenerator (clears the cached
stable names and the inner counters), and an aggregate on CompilerGlobalState that
resets all three generators, restoring the fresh-process name layout. Callers must
ensure no compilation is concurrently generating names.

No in-tree caller yet; the consumer is the hot reload emit path in dotnet#19941.
Covered by unit tests proving drift without reset, exact replay after reset, and that
the stable-name cache itself is cleared.
@NatElkins NatElkins force-pushed the fix-generated-name-counter-reset branch from 748ff7c to 40b666b Compare July 2, 2026 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

1 participant