Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/11.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>` 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))
Expand Down
16 changes: 15 additions & 1 deletion src/Compiler/Service/TransparentCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type internal TcInfo =

stateContainsNodes: Set<NodeToTypeCheck>

/// Ids of the TcIntermediate results that were used to build this state
tcIntermediateIds: Set<int>

sink: TcResultsSinkImpl list
}

Expand All @@ -78,6 +81,9 @@ type internal TcInfo =
[<NoEquality; NoComparison>]
type internal TcIntermediate =
{
/// Unique for each computation, so results computed from different upstream results can be told apart
id: int

finisher: Finisher<NodeToTypeCheck, TcState, PartialResult>
//tcEnvAtEndOfFile: TcEnv

Expand Down Expand Up @@ -749,6 +755,7 @@ type internal TransparentCompiler
sigNameOpt = None
graphNode = None
stateContainsNodes = Set.empty
tcIntermediateIds = Set.empty
sink = []
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -1526,6 +1538,7 @@ type internal TransparentCompiler

return
{
id = Interlocked.Increment &TcIntermediateIdCounter
finisher = finisher
moduleNamesDict = moduleNamesDict
tcDiagnosticsRev = [ errHandler.CollectedPhasedDiagnostics ]
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/Compiler/Service/TransparentCompiler.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ type internal TcInfo =

stateContainsNodes: Set<NodeToTypeCheck>

/// Ids of the TcIntermediate results that were used to build this state
tcIntermediateIds: Set<int>

sink: TcResultsSinkImpl list
}

[<NoEquality; NoComparison>]
type internal TcIntermediate =
{
/// Unique for each computation, so results computed from different upstream results can be told apart
id: int

finisher: Finisher<NodeToTypeCheck, TcState, PartialResult>

/// Disambiguation table for module names
Expand Down Expand Up @@ -175,7 +181,7 @@ type internal CompilerCaches =

member CacheSizes: CacheSizes

member TcIntermediate: AsyncMemoize<string * FSharpProjectIdentifier, string * int, TcIntermediate>
member TcIntermediate: AsyncMemoize<string * FSharpProjectIdentifier, string * (int * Set<int>), TcIntermediate>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,41 @@ let ``Changing impl file doesn't invalidate an in-memory referenced project`` ()

Assert.Equal(0, count)

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

[<Theory>]
[<InlineData true>]
[<InlineData false>]
Expand Down
Loading