From a74468dd49e5bee69ffae99ed5aeceeeeecd56fd Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Sat, 26 Sep 2026 16:41:42 +0200 Subject: [PATCH 1/2] Transparent compiler: key file check results by the upstream results they used The cached type-check result of a file was keyed by the source up to that file, not by the results of the earlier files it was checked against. When an earlier file's result was garbage collected and recomputed, it declared new entities, while cached results of later files still referred to the old ones. Checking a file that combined both reported errors like "This expression was expected to have type 'A.T' but here has type 'A.T'". Each computed result now gets a unique id, the state passed to a file records the ids of the results it was built from, and those ids are part of the file's cache key, so results that depend on a recomputed one are recomputed too. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../.FSharp.Compiler.Service/11.0.200.md | 1 + src/Compiler/Service/TransparentCompiler.fs | 16 ++++++++- src/Compiler/Service/TransparentCompiler.fsi | 8 ++++- .../FSharpChecker/TransparentCompiler.fs | 35 +++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md index b88496f259e..4960ae8f80d 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md +++ b/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md @@ -32,6 +32,7 @@ * Preserve SRTP extension scopes across source files and competing witnesses through imported inline calls. Compile generic extension forwarding, including quotations, in unoptimized builds. Preserve applicable built-in operator priority in imported inline quotations. Do not reuse SRTP specializations across different selected witnesses. ([Issue #20683](https://github.com/dotnet/fsharp/issues/20683), [Issue #20684](https://github.com/dotnet/fsharp/issues/20684), [Issue #20685](https://github.com/dotnet/fsharp/issues/20685), [PR #20717](https://github.com/dotnet/fsharp/pull/20717)) * Transparent compiler: with `enablePartialTypeChecking`, a project that is only referenced by another project is type checked from its signatures. Implementation files that have a signature file are no longer parsed or type checked to produce the referenced assembly. `ParseAndCheckProject` still checks all files. ([PR #20703](https://github.com/dotnet/fsharp/pull/20703)) * With `enablePartialTypeChecking`, a project that is only referenced by another project no longer type checks its implementation files that have a signature file. Producing the referenced assembly only needs the signatures, but finalizing the project also collected the diagnostics of the skipped files, which type checked all of them and kept their results in memory. `ParseAndCheckProject` still reports the diagnostics of these files. ([PR #20699](https://github.com/dotnet/fsharp/pull/20699)) +* Transparent compiler: fix spurious "This expression was expected to have type 'T' but here has type 'T'" errors after the cached result of an earlier file was garbage collected and recomputed while results of later files stayed cached. ([PR #XXXXX](https://github.com/dotnet/fsharp/pull/XXXXX)) ### Changed diff --git a/src/Compiler/Service/TransparentCompiler.fs b/src/Compiler/Service/TransparentCompiler.fs index aa0bb4a5617..99146e47cf2 100644 --- a/src/Compiler/Service/TransparentCompiler.fs +++ b/src/Compiler/Service/TransparentCompiler.fs @@ -70,6 +70,9 @@ type internal TcInfo = stateContainsNodes: Set + /// Ids of the TcIntermediate results that were used to build this state + tcIntermediateIds: Set + sink: TcResultsSinkImpl list } @@ -78,6 +81,9 @@ type internal TcInfo = [] type internal TcIntermediate = { + /// Unique for each computation, so results computed from different upstream results can be told apart + id: int + finisher: Finisher //tcEnvAtEndOfFile: TcEnv @@ -749,6 +755,7 @@ type internal TransparentCompiler sigNameOpt = None graphNode = None stateContainsNodes = Set.empty + tcIntermediateIds = Set.empty sink = [] } @@ -960,6 +967,8 @@ type internal TransparentCompiler let mutable BootstrapInfoIdCounter = 0 + let mutable TcIntermediateIdCounter = 0 + /// Bootstrap info that does not depend source files let ComputeBootstrapInfoStatic (projectSnapshot: ProjectSnapshotBase<_>, tcConfig: TcConfig, assemblyName: string, loadClosureOpt) = let cacheKey = projectSnapshot.BaseCacheKeyWith("BootstrapInfoStatic", assemblyName) @@ -1440,7 +1449,10 @@ type internal TransparentCompiler ignore dependencyGraph - let key = projectSnapshot.FileKey(index).WithExtraVersion(bootstrapInfo.Id) + // The upstream results are part of the key. If one of them is collected and recomputed, it declares new + // entities, and a result computed from the old one must not be combined with it. + let key = + projectSnapshot.FileKey(index).WithExtraVersion((bootstrapInfo.Id, prevTcInfo.tcIntermediateIds)) let _label, _k, _version = key.GetLabel(), key.GetKey(), key.GetVersion() @@ -1526,6 +1538,7 @@ type internal TransparentCompiler return { + id = Interlocked.Increment &TcIntermediateIdCounter finisher = finisher moduleNamesDict = moduleNamesDict tcDiagnosticsRev = [ errHandler.CollectedPhasedDiagnostics ] @@ -1574,6 +1587,7 @@ type internal TransparentCompiler latestCcuSigForFile = Some ccuSigForFile graphNode = Some node stateContainsNodes = tcInfo.stateContainsNodes |> Set.add node + tcIntermediateIds = tcInfo.tcIntermediateIds |> Set.add tcIntermediate.id sink = if collectSinks then tcIntermediate.sink :: tcInfo.sink diff --git a/src/Compiler/Service/TransparentCompiler.fsi b/src/Compiler/Service/TransparentCompiler.fsi index c585d107aa6..8c00bf02c06 100644 --- a/src/Compiler/Service/TransparentCompiler.fsi +++ b/src/Compiler/Service/TransparentCompiler.fsi @@ -47,12 +47,18 @@ type internal TcInfo = stateContainsNodes: Set + /// Ids of the TcIntermediate results that were used to build this state + tcIntermediateIds: Set + sink: TcResultsSinkImpl list } [] type internal TcIntermediate = { + /// Unique for each computation, so results computed from different upstream results can be told apart + id: int + finisher: Finisher /// Disambiguation table for module names @@ -175,7 +181,7 @@ type internal CompilerCaches = member CacheSizes: CacheSizes - member TcIntermediate: AsyncMemoize + member TcIntermediate: AsyncMemoize), TcIntermediate> member ScriptClosure: AsyncMemoize diff --git a/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs b/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs index 76a47afcfe9..74c258b5ff4 100644 --- a/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs +++ b/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs @@ -547,6 +547,41 @@ let ``Changing impl file doesn't invalidate an in-memory referenced project`` () Assert.Equal(0, count) +[] +let ``Recomputing a collected upstream file result does not mix it with cached downstream results`` () = + let project = + SyntheticProject.Create( + { sourceFile "A" [] with Source = "type T = { X: int }" }, + { sourceFile "B" [ "A" ] with Source = "let value : ModuleA.T = { X = 1 }" }, + { sourceFile "C" [ "A"; "B" ] with Source = "let ok : ModuleA.T = ModuleB.value" }) + + // Only the most recently used file result is held strongly, so the result for A can be collected + // while the result for B, which was checked against it, stays cached. + let cacheSizes = + { CacheSizes.Create 100 with + TcIntermediateKeepStrongly = 1 + TcIntermediateKeepWeakly = 100 } + + let checker = + FSharpChecker.Create(useTransparentCompiler = true, transparentCompilerCacheSizes = cacheSizes) + + // Start from an unchecked project, so that checking B is what fills the cache. + let initialContext = { Project = project; Signatures = Map.empty; Cursor = None } + let builder = ProjectWorkflowBuilder(project, initialContext, checker = checker, useTransparentCompiler = true) + + try + builder { + saveAll + checkFile "B" expectOk + withChecker (fun _ -> + GC.Collect() + GC.WaitForPendingFinalizers() + GC.Collect()) + checkFile "C" expectOk + } |> ignore + finally + builder.DeleteProjectDir() + [] [] [] From dc71aa5b335ffd5e58edbe628bd2b793f7047e5c Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Sat, 26 Sep 2026 17:48:50 +0200 Subject: [PATCH 2/2] Release notes: link PR #20643 Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/release-notes/.FSharp.Compiler.Service/11.0.200.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md index 4960ae8f80d..39a68ff77d4 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md +++ b/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md @@ -21,6 +21,7 @@ * `FsiEvaluationSession.EvalInteraction` and `EvalInteractionNonThrowing` evaluate every `;;`-separated interaction in the text they are given, as their documentation has always said, instead of silently discarding everything after the first. ([PR #20396](https://github.com/dotnet/fsharp/pull/20396)) * Fix runtime-async callbacks chosen by a branch rerunning their setup and losing shared state on each call, `Await` inlined into a `with` handler not being rewritten, and `Await` in an object-expression base constructor call not being reported. ([Issue #20577](https://github.com/dotnet/fsharp/issues/20577), [PR #20647](https://github.com/dotnet/fsharp/pull/20647)) * Inline members can now call inline base-class members with optimizations off while retaining debugger step-into. Uneliminated `base` references report FS3925 instead of an internal error. ([Issue #20614](https://github.com/dotnet/fsharp/issues/20614), [PR #20639](https://github.com/dotnet/fsharp/pull/20639)) +* Transparent compiler: fix spurious "This expression was expected to have type 'T' but here has type 'T'" errors after the cached result of an earlier file was garbage collected and recomputed while results of later files stayed cached. ([PR #20643](https://github.com/dotnet/fsharp/pull/20643)) * Prevent `InvalidProgramException` when runtime async awaits a `Task` upcast to `Task`, including through `ConfigureAwait`. ([Issue #20686](https://github.com/dotnet/fsharp/issues/20686), [PR #20726](https://github.com/dotnet/fsharp/pull/20726)) * Struct/reference anonymous-record mismatches now name anonymous records rather than tuples. ([Issue #6110](https://github.com/dotnet/fsharp/issues/6110), [PR #20681](https://github.com/dotnet/fsharp/pull/20681)) * Preserve constraint inference order when explicit generic arguments share a caller type parameter, avoiding spurious FS0001 errors with multiply implemented generic interfaces. ([PR #20491](https://github.com/dotnet/fsharp/pull/20491)) @@ -32,7 +33,6 @@ * Preserve SRTP extension scopes across source files and competing witnesses through imported inline calls. Compile generic extension forwarding, including quotations, in unoptimized builds. Preserve applicable built-in operator priority in imported inline quotations. Do not reuse SRTP specializations across different selected witnesses. ([Issue #20683](https://github.com/dotnet/fsharp/issues/20683), [Issue #20684](https://github.com/dotnet/fsharp/issues/20684), [Issue #20685](https://github.com/dotnet/fsharp/issues/20685), [PR #20717](https://github.com/dotnet/fsharp/pull/20717)) * Transparent compiler: with `enablePartialTypeChecking`, a project that is only referenced by another project is type checked from its signatures. Implementation files that have a signature file are no longer parsed or type checked to produce the referenced assembly. `ParseAndCheckProject` still checks all files. ([PR #20703](https://github.com/dotnet/fsharp/pull/20703)) * With `enablePartialTypeChecking`, a project that is only referenced by another project no longer type checks its implementation files that have a signature file. Producing the referenced assembly only needs the signatures, but finalizing the project also collected the diagnostics of the skipped files, which type checked all of them and kept their results in memory. `ParseAndCheckProject` still reports the diagnostics of these files. ([PR #20699](https://github.com/dotnet/fsharp/pull/20699)) -* Transparent compiler: fix spurious "This expression was expected to have type 'T' but here has type 'T'" errors after the cached result of an earlier file was garbage collected and recomputed while results of later files stayed cached. ([PR #XXXXX](https://github.com/dotnet/fsharp/pull/XXXXX)) ### Changed