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..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)) 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() + [] [] []