Skip to content

Commit 881f5b0

Browse files
xperiandriclaude
andcommitted
Compact the PE reference change
The comments are down to the reasons the code cannot say: the field that is held strongly until the first emit is named pinnedUntilEmitted instead of carrying a comment, the test bindings are named for what they are, and the review pointer and the restating comments are gone. The two editor test files share the C# sources and the analyzer reference instead of declaring each their own. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent cca0df7 commit 881f5b0

4 files changed

Lines changed: 33 additions & 64 deletions

File tree

‎tests/fsharp/Compiler/Service/MultiProjectTests.fs‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,6 @@ let y = 1
211211

212212

213213

214-
// Focused counters for https://github.com/dotnet/fsharp/pull/20460#discussion_r3965947159:
215-
// a referenced C# project's `Compilation` is recreated on every solution fork, but its
216-
// in-memory PE reference is only re-emitted when the project's dependent semantic version
217-
// (here, the reference's stamp) actually changes.
218214
let private mkCountedCSharpPEReference (stamp: DateTime) =
219215
let csSrc =
220216
"""
@@ -275,26 +271,23 @@ let test() =
275271
[<Fact>]
276272
let ``Reusing a CSharp reference's stamp avoids re-emitting a recreated Compilation``() =
277273
let stamp = DateTime(2024, 1, 1)
278-
let csRefProj1, emitCount1 = mkCountedCSharpPEReference stamp
279-
let csRefProj2, emitCount2 = mkCountedCSharpPEReference stamp
274+
let csRefProj, emitCount = mkCountedCSharpPEReference stamp
275+
let recreatedRefProj, recreatedEmitCount = mkCountedCSharpPEReference stamp
280276

281-
let fsOptions = projectReferencing csRefProj1
277+
let fsOptions = projectReferencing csRefProj
282278
checkUsesCSharpClass fsOptions
283-
Assert.Equal(1, emitCount1())
279+
Assert.Equal(1, emitCount())
284280

285-
// Same dependent semantic version (stamp unchanged): the checker must reuse its cached
286-
// project build and never touch the recreated Compilation behind the new reference.
287-
checkUsesCSharpClass { fsOptions with ReferencedProjects = [|csRefProj2|] }
288-
Assert.Equal(0, emitCount2())
281+
checkUsesCSharpClass { fsOptions with ReferencedProjects = [|recreatedRefProj|] }
282+
Assert.Equal(0, recreatedEmitCount())
289283

290284
[<Fact>]
291285
let ``Changing a CSharp reference's stamp does re-emit the new Compilation``() =
292-
let csRefProj1, _ = mkCountedCSharpPEReference (DateTime(2024, 1, 1))
293-
let csRefProj2, emitCount2 = mkCountedCSharpPEReference (DateTime(2024, 1, 2))
286+
let csRefProj, _ = mkCountedCSharpPEReference (DateTime(2024, 1, 1))
287+
let changedRefProj, changedEmitCount = mkCountedCSharpPEReference (DateTime(2024, 1, 2))
294288

295-
let fsOptions = projectReferencing csRefProj1
289+
let fsOptions = projectReferencing csRefProj
296290
checkUsesCSharpClass fsOptions
297291

298-
// Different dependent semantic version (stamp changed): the checker must pick up the new reference.
299-
checkUsesCSharpClass { fsOptions with ReferencedProjects = [|csRefProj2|] }
300-
Assert.True(emitCount2() >= 1)
292+
checkUsesCSharpClass { fsOptions with ReferencedProjects = [|changedRefProj|] }
293+
Assert.True(changedEmitCount() >= 1)

‎vsintegration/src/FSharp.Editor/LanguageService/FSharpProjectOptionsManager.fs‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ type private SingleFileCacheEntry =
111111

112112
module internal PEReferenceVersion =
113113

114-
// Source generators come only from analyzer references. Which of those carry one is known to Roslyn's
115-
// out-of-process host alone: asking here would load the generators into Visual Studio.
114+
// A source generator can turn an edit below the declaration level into other declarations. Only Roslyn's
115+
// out-of-process host knows which analyzer references carry one: asking here would load them into Visual Studio.
116116
let private mayRunSourceGenerators (project: Project) =
117117
let solution = project.Solution
118118

@@ -125,38 +125,33 @@ module internal PEReferenceVersion =
125125
|| solution.GetProjectDependencyGraph().GetProjectsThatThisProjectTransitivelyDependsOn(project.Id)
126126
|> Seq.exists hasAnalyzerReferences
127127

128-
/// The version a referenced project's in-memory PE reference is kept for. A source generator can turn an
129-
/// edit that leaves the dependent semantic version standing into different declarations.
130128
let getAsync (referencedProject: Project) (ct: CancellationToken) =
131129
if mayRunSourceGenerators referencedProject then
132130
referencedProject.GetDependentVersionAsync(ct)
133131
else
134132
referencedProject.GetDependentSemanticVersionAsync(ct)
135133

136134
/// <summary>
137-
/// The in-memory PE reference of a referenced project, kept while the project's
138-
/// <c>PEReferenceVersion</c> is unchanged. Roslyn recreates
139-
/// <see cref="T:Microsoft.CodeAnalysis.Compilation"/> instances freely - on every solution fork,
140-
/// and under memory pressure because it holds the final compilation weakly - and a reference
141-
/// created per instance carries a fresh stamp that invalidates every FCS cache keyed on it.
135+
/// The in-memory PE reference of a referenced project, kept while its <c>PEReferenceVersion</c> is unchanged.
136+
/// Roslyn recreates a <see cref="T:Microsoft.CodeAnalysis.Compilation"/> on every solution fork, and a reference per
137+
/// instance has a fresh stamp, which invalidates every FCS cache keyed on it.
142138
/// </summary>
143139
[<Sealed>]
144140
type private PEReferenceCacheEntry(version: VersionStamp, compilation: Compilation) =
145-
// Pinned until the first emit result, so the reader can always be materialised.
146-
let mutable pinned = compilation
141+
let mutable pinnedUntilEmitted = compilation
147142
let latest = WeakReference<Compilation>(compilation)
148143

149144
member _.Version = version
150145

151146
member _.TryGetCompilation() =
152-
match pinned with
147+
match pinnedUntilEmitted with
153148
| null ->
154149
match latest.TryGetTarget() with
155150
| true, compilation -> ValueSome compilation
156151
| _ -> ValueNone
157152
| pinned -> ValueSome pinned
158153

159-
member _.Emitted() = pinned <- null
154+
member _.Emitted() = pinnedUntilEmitted <- null
160155

161156
[<RequireQualifiedAccess>]
162157
type private FSharpProjectOptionsMessage =
@@ -198,8 +193,7 @@ type private FSharpProjectOptionsReactor(checker: FSharpChecker) =
198193
let projectId = referencedProject.Id
199194
let mutable stamp = DateTime.UtcNow
200195

201-
// Getting a C# reference assembly can fail if there are compilation errors that cannot be resolved.
202-
// To mitigate this, we store the last successful compilation of a C# project and re-use it until we get a new successful compilation.
196+
// Emitting can fail on unresolvable compilation errors: fall back to the project's last successful compilation.
203197
let getStream =
204198
fun ct ->
205199
let tryStream (comp: Compilation) =

‎vsintegration/tests/FSharp.Editor.Tests/PEReferenceCacheTests.fs‎

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
/// The in-memory PE references <c>FSharpProjectOptionsManager</c> keeps for the C# projects an F# project
4-
/// references: kept while the C# project's version holds, replaced when it moves, dropped with the project.
53
module FSharp.Editor.Tests.PEReferenceCacheTests
64

7-
// removeLibrary listens to Workspace.WorkspaceChanged, as the manager does: the handlers of that event run in the
8-
// order they were added, while those of its replacement run on another thread, in no order with them.
5+
// removeLibrary listens to the obsolete WorkspaceChanged like the manager: its handlers run in registration order.
96
#nowarn "44"
107

118
open System
12-
open System.Collections.Immutable
139
open System.Threading
1410
open System.Threading.Tasks
1511

@@ -21,25 +17,13 @@ open Microsoft.VisualStudio.FSharp.Editor
2117

2218
open FSharp.Compiler.CodeAnalysis
2319
open FSharp.Editor.Tests.Helpers
20+
open FSharp.Editor.Tests.PEReferenceVersionTests
2421

2522
open Xunit
2623

27-
[<Literal>]
28-
let private Original = "public class Library { public int Value() => 1; }"
29-
30-
[<Literal>]
31-
let private BodyEdit = "public class Library { public int Value() => 2; }"
32-
33-
[<Literal>]
34-
let private DeclarationEdit =
35-
"public class Library { public int Value() => 1; public int Other() => 1; }"
36-
3724
[<Literal>]
3825
let private BodyEditAfterDeclarationEdit =
39-
"public class Library { public int Value() => 2; public int Other() => 1; }"
40-
41-
let private analyzerReference =
42-
AnalyzerImageReference(ImmutableArray<DiagnosticAnalyzer>.Empty) :> AnalyzerReference
26+
"public class Input { public int M() => 2; public int N() => 1; }"
4327

4428
/// An F# project that references the C# project <c>Library</c>.
4529
type private Fixture =
@@ -122,8 +106,7 @@ let private versionOfLibrary (fixture: Fixture) =
122106
let library = fixture.Workspace.CurrentSolution.GetProject fixture.Library.Id
123107
PEReferenceVersion.getAsync library CancellationToken.None
124108

125-
/// Removes the C# project, and completes once Roslyn has raised the removal to the handlers added before the call.
126-
/// The manager listens from the start, so by then it has been told to forget the project.
109+
/// Removes the C# project, and completes once the manager, which listens from the start, has been told to forget it.
127110
let private removeLibrary (fixture: Fixture) =
128111
task {
129112
let workspace = fixture.Workspace
@@ -161,12 +144,9 @@ let ``an edit that leaves the C# declarations alone keeps the PE reference`` ()
161144

162145
editLibrary fixture BodyEdit
163146

164-
// The edit moves the C# project's dependent version, so the options are computed again ...
147+
// The dependent version moved, so the options are computed again, yet they keep the reference.
165148
let! recomputed = optionsOf fixture
166149
Assert.NotSame(options, recomputed)
167-
168-
// ... and since its dependent semantic version stands, they keep the reference they had:
169-
// what FCS cached against the stamp of that reference stays valid.
170150
Assert.Same(reference, peReferenceOf recomputed)
171151
}
172152

‎vsintegration/tests/FSharp.Editor.Tests/PEReferenceVersionTests.fs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
/// The version a referenced C# project's in-memory PE reference is kept for. Only the version is read, which
4-
/// needs neither an F# project nor the editor services: the projects live in a plain workspace.
3+
/// The version a referenced C# project's PE reference is kept for; a plain workspace, no F# project or editor services.
54
module FSharp.Editor.Tests.PEReferenceVersionTests
65

76
open System.Collections.Immutable
@@ -13,13 +12,16 @@ open Microsoft.CodeAnalysis.Text
1312
open Microsoft.VisualStudio.FSharp.Editor
1413

1514
[<Literal>]
16-
let private BodyEdit = "public class Input { public int M() => 2; }"
15+
let Original = "public class Input { public int M() => 1; }"
1716

1817
[<Literal>]
19-
let private DeclarationEdit =
18+
let BodyEdit = "public class Input { public int M() => 2; }"
19+
20+
[<Literal>]
21+
let DeclarationEdit =
2022
"public class Input { public int M() => 1; public int N() => 1; }"
2123

22-
let private analyzerReference =
24+
let analyzerReference =
2325
AnalyzerImageReference(ImmutableArray<DiagnosticAnalyzer>.Empty) :> AnalyzerReference
2426

2527
let private projectNamed (name: string) (solution: Solution) =
@@ -52,7 +54,7 @@ let ``only a declaration edit moves the version, unless a source generator may r
5254
let solution =
5355
workspace.CurrentSolution
5456
.AddProject(dependencyId, "Dependency", "Dependency", LanguageNames.CSharp)
55-
.AddDocument(documentId, "Input.cs", "public class Input { public int M() => 1; }")
57+
.AddDocument(documentId, "Input.cs", Original)
5658
.AddProject(libraryId, "Library", "Library", LanguageNames.CSharp)
5759
.AddProjectReference(libraryId, ProjectReference dependencyId)
5860

0 commit comments

Comments
 (0)