Skip to content

Commit a74468d

Browse files
michaelglassclaude
andcommitted
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) <noreply@anthropic.com>
1 parent a63c579 commit a74468d

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎docs/release-notes/.FSharp.Compiler.Service/11.0.200.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* 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))
3333
* 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))
3434
* 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))
35+
* 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))
3536

3637
### Changed
3738

‎src/Compiler/Service/TransparentCompiler.fs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ type internal TcInfo =
7070

7171
stateContainsNodes: Set<NodeToTypeCheck>
7272

73+
/// Ids of the TcIntermediate results that were used to build this state
74+
tcIntermediateIds: Set<int>
75+
7376
sink: TcResultsSinkImpl list
7477
}
7578

@@ -78,6 +81,9 @@ type internal TcInfo =
7881
[<NoEquality; NoComparison>]
7982
type internal TcIntermediate =
8083
{
84+
/// Unique for each computation, so results computed from different upstream results can be told apart
85+
id: int
86+
8187
finisher: Finisher<NodeToTypeCheck, TcState, PartialResult>
8288
//tcEnvAtEndOfFile: TcEnv
8389

@@ -749,6 +755,7 @@ type internal TransparentCompiler
749755
sigNameOpt = None
750756
graphNode = None
751757
stateContainsNodes = Set.empty
758+
tcIntermediateIds = Set.empty
752759
sink = []
753760
}
754761

@@ -960,6 +967,8 @@ type internal TransparentCompiler
960967

961968
let mutable BootstrapInfoIdCounter = 0
962969

970+
let mutable TcIntermediateIdCounter = 0
971+
963972
/// Bootstrap info that does not depend source files
964973
let ComputeBootstrapInfoStatic (projectSnapshot: ProjectSnapshotBase<_>, tcConfig: TcConfig, assemblyName: string, loadClosureOpt) =
965974
let cacheKey = projectSnapshot.BaseCacheKeyWith("BootstrapInfoStatic", assemblyName)
@@ -1440,7 +1449,10 @@ type internal TransparentCompiler
14401449

14411450
ignore dependencyGraph
14421451

1443-
let key = projectSnapshot.FileKey(index).WithExtraVersion(bootstrapInfo.Id)
1452+
// The upstream results are part of the key. If one of them is collected and recomputed, it declares new
1453+
// entities, and a result computed from the old one must not be combined with it.
1454+
let key =
1455+
projectSnapshot.FileKey(index).WithExtraVersion((bootstrapInfo.Id, prevTcInfo.tcIntermediateIds))
14441456

14451457
let _label, _k, _version = key.GetLabel(), key.GetKey(), key.GetVersion()
14461458

@@ -1526,6 +1538,7 @@ type internal TransparentCompiler
15261538

15271539
return
15281540
{
1541+
id = Interlocked.Increment &TcIntermediateIdCounter
15291542
finisher = finisher
15301543
moduleNamesDict = moduleNamesDict
15311544
tcDiagnosticsRev = [ errHandler.CollectedPhasedDiagnostics ]
@@ -1574,6 +1587,7 @@ type internal TransparentCompiler
15741587
latestCcuSigForFile = Some ccuSigForFile
15751588
graphNode = Some node
15761589
stateContainsNodes = tcInfo.stateContainsNodes |> Set.add node
1590+
tcIntermediateIds = tcInfo.tcIntermediateIds |> Set.add tcIntermediate.id
15771591
sink =
15781592
if collectSinks then
15791593
tcIntermediate.sink :: tcInfo.sink

‎src/Compiler/Service/TransparentCompiler.fsi‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ type internal TcInfo =
4747

4848
stateContainsNodes: Set<NodeToTypeCheck>
4949

50+
/// Ids of the TcIntermediate results that were used to build this state
51+
tcIntermediateIds: Set<int>
52+
5053
sink: TcResultsSinkImpl list
5154
}
5255

5356
[<NoEquality; NoComparison>]
5457
type internal TcIntermediate =
5558
{
59+
/// Unique for each computation, so results computed from different upstream results can be told apart
60+
id: int
61+
5662
finisher: Finisher<NodeToTypeCheck, TcState, PartialResult>
5763

5864
/// Disambiguation table for module names
@@ -175,7 +181,7 @@ type internal CompilerCaches =
175181

176182
member CacheSizes: CacheSizes
177183

178-
member TcIntermediate: AsyncMemoize<string * FSharpProjectIdentifier, string * int, TcIntermediate>
184+
member TcIntermediate: AsyncMemoize<string * FSharpProjectIdentifier, string * (int * Set<int>), TcIntermediate>
179185

180186
member ScriptClosure: AsyncMemoize<string * FSharpProjectIdentifier, string, LoadClosure>
181187

‎tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,41 @@ let ``Changing impl file doesn't invalidate an in-memory referenced project`` ()
547547

548548
Assert.Equal(0, count)
549549

550+
[<Fact>]
551+
let ``Recomputing a collected upstream file result does not mix it with cached downstream results`` () =
552+
let project =
553+
SyntheticProject.Create(
554+
{ sourceFile "A" [] with Source = "type T = { X: int }" },
555+
{ sourceFile "B" [ "A" ] with Source = "let value : ModuleA.T = { X = 1 }" },
556+
{ sourceFile "C" [ "A"; "B" ] with Source = "let ok : ModuleA.T = ModuleB.value" })
557+
558+
// Only the most recently used file result is held strongly, so the result for A can be collected
559+
// while the result for B, which was checked against it, stays cached.
560+
let cacheSizes =
561+
{ CacheSizes.Create 100 with
562+
TcIntermediateKeepStrongly = 1
563+
TcIntermediateKeepWeakly = 100 }
564+
565+
let checker =
566+
FSharpChecker.Create(useTransparentCompiler = true, transparentCompilerCacheSizes = cacheSizes)
567+
568+
// Start from an unchecked project, so that checking B is what fills the cache.
569+
let initialContext = { Project = project; Signatures = Map.empty; Cursor = None }
570+
let builder = ProjectWorkflowBuilder(project, initialContext, checker = checker, useTransparentCompiler = true)
571+
572+
try
573+
builder {
574+
saveAll
575+
checkFile "B" expectOk
576+
withChecker (fun _ ->
577+
GC.Collect()
578+
GC.WaitForPendingFinalizers()
579+
GC.Collect())
580+
checkFile "C" expectOk
581+
} |> ignore
582+
finally
583+
builder.DeleteProjectDir()
584+
550585
[<Theory>]
551586
[<InlineData true>]
552587
[<InlineData false>]

0 commit comments

Comments
 (0)