Skip to content

Commit b897908

Browse files
authored
Transparent Compiler Tests (#16591)
1 parent 9ec3cad commit b897908

23 files changed

+158
-63
lines changed

azure-pipelines.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# CI and PR triggers
1+
# CI and PR triggers
22
trigger:
33
branches:
44
include:
@@ -433,6 +433,10 @@ stages:
433433
vs_release:
434434
_configuration: Release
435435
_testKind: testVs
436+
transparent_compiler_release:
437+
_configuration: Release
438+
_testKind: testCoreclr
439+
436440
${{ if eq(variables['Build.Reason'], 'Flaky, disabled, was PullRequest') }}:
437441
inttests_release:
438442
_configuration: Release
@@ -450,7 +454,15 @@ stages:
450454
env:
451455
NativeToolsOnMachine: true
452456
displayName: Build / Test
453-
condition: ne(variables['_testKind'], 'testIntegration')
457+
condition: and( ne(variables['_testKind'], 'testIntegration'), ne(variables['System.JobName'], 'transparent_compiler_release') )
458+
459+
- script: eng\CIBuild.cmd -compressallmetadata -configuration $(_configuration) -$(_testKind)
460+
env:
461+
TEST_TRANSPARENT_COMPILER: 1
462+
NativeToolsOnMachine: true
463+
displayName: Build / Test Transparent Compiler
464+
condition: and( eq(variables['System.JobName'], 'transparent_compiler_release'), ne(variables['_testKind'], 'testIntegration') )
465+
454466
- script: eng\CIBuild.cmd -compressallmetadata -configuration $(_configuration) -$(_testKind)
455467
env:
456468
NativeToolsOnMachine: true

src/Compiler/Service/FSharpCheckerResults.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ open Internal.Utilities.Hashing
6060

6161
type FSharpUnresolvedReferencesSet = FSharpUnresolvedReferencesSet of UnresolvedAssemblyReference list
6262

63+
[<RequireQualifiedAccess>]
64+
type DocumentSource =
65+
| FileSystem
66+
| Custom of (string -> Async<ISourceText option>)
67+
6368
[<Sealed>]
6469
type DelayedILModuleReader =
6570
val private name: string

src/Compiler/Service/FSharpCheckerResults.fsi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ open FSharp.Compiler.Text
3030

3131
open Internal.Utilities.Collections
3232

33+
[<Experimental "This type is experimental and likely to be removed in the future.">]
34+
[<RequireQualifiedAccess>]
35+
type DocumentSource =
36+
| FileSystem
37+
| Custom of (string -> Async<ISourceText option>)
38+
3339
/// Delays the creation of an ILModuleReader
3440
[<Sealed>]
3541
type DelayedILModuleReader =

src/Compiler/Service/FSharpProjectSnapshot.fs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ type FSharpFileSnapshot(FileName: string, Version: string, GetSource: unit -> Ta
8181
|> Task.FromResult
8282
)
8383

84+
static member CreateFromDocumentSource(fileName: string, documentSource: DocumentSource) =
85+
86+
match documentSource with
87+
| DocumentSource.Custom f ->
88+
let version = DateTime.Now.Ticks.ToString()
89+
90+
FSharpFileSnapshot(
91+
fileName,
92+
version,
93+
fun () ->
94+
task {
95+
match! f fileName |> Async.StartAsTask with
96+
| Some source -> return SourceTextNew.ofISourceText source
97+
| None -> return failwith $"Couldn't get source for file {f}"
98+
}
99+
)
100+
101+
| DocumentSource.FileSystem -> FSharpFileSnapshot.CreateFromFileSystem fileName
102+
84103
member public _.FileName = FileName
85104
member _.Version = Version
86105
member _.GetSource() = GetSource()
@@ -603,13 +622,22 @@ and [<Experimental("This FCS API is experimental and subject to change.")>] FSha
603622
return snapshotAccumulator[options]
604623
}
605624

606-
static member internal GetFileSnapshotFromDisk _ fileName =
607-
FSharpFileSnapshot.CreateFromFileSystem fileName |> async.Return
608-
609-
static member FromOptions(options: FSharpProjectOptions) =
610-
FSharpProjectSnapshot.FromOptions(options, FSharpProjectSnapshot.GetFileSnapshotFromDisk)
625+
static member FromOptions(options: FSharpProjectOptions, documentSource: DocumentSource) =
626+
FSharpProjectSnapshot.FromOptions(
627+
options,
628+
fun _ fileName ->
629+
FSharpFileSnapshot.CreateFromDocumentSource(fileName, documentSource)
630+
|> async.Return
631+
)
611632

612-
static member FromOptions(options: FSharpProjectOptions, fileName: string, fileVersion: int, sourceText: ISourceText) =
633+
static member FromOptions
634+
(
635+
options: FSharpProjectOptions,
636+
fileName: string,
637+
fileVersion: int,
638+
sourceText: ISourceText,
639+
documentSource: DocumentSource
640+
) =
613641

614642
let getFileSnapshot _ fName =
615643
if fName = fileName then
@@ -619,7 +647,7 @@ and [<Experimental("This FCS API is experimental and subject to change.")>] FSha
619647
fun () -> Task.FromResult(SourceTextNew.ofISourceText sourceText)
620648
)
621649
else
622-
FSharpFileSnapshot.CreateFromFileSystem fName
650+
FSharpFileSnapshot.CreateFromDocumentSource(fName, documentSource)
623651
|> async.Return
624652

625653
FSharpProjectSnapshot.FromOptions(options, getFileSnapshot)

src/Compiler/Service/TransparentCompiler.fs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ type internal TransparentCompiler
325325
useSyntaxTreeCache
326326
) as self =
327327

328+
let documentSource =
329+
match getSource with
330+
| Some getSource -> DocumentSource.Custom getSource
331+
| None -> DocumentSource.FileSystem
332+
328333
// Is having just one of these ok?
329334
let lexResourceManager = Lexhelp.LexResourceManager()
330335

@@ -1474,8 +1479,6 @@ type internal TransparentCompiler
14741479
let tcSymbolUses = sink.GetSymbolUses()
14751480
let tcOpenDeclarations = sink.GetOpenDeclarations()
14761481

1477-
let tcDependencyFiles = [] // TODO add as a set to TcIntermediate
1478-
14791482
// TODO: Apparently creating diagnostics can produce further diagnostics. So let's capture those too. Hopefully there is a more elegant solution...
14801483
// Probably diagnostics need to be evaluated during typecheck anyway for proper formatting, which might take care of this too.
14811484
let extraLogger = CapturingDiagnosticsLogger("DiagnosticsWhileCreatingDiagnostics")
@@ -1535,7 +1538,7 @@ type internal TransparentCompiler
15351538
projectSnapshot.IsIncompleteTypeCheckEnvironment,
15361539
None,
15371540
projectSnapshot.ToOptions(),
1538-
Array.ofList tcDependencyFiles,
1541+
Array.ofList tcInfo.tcDependencyFiles,
15391542
creationDiags,
15401543
parseResults.Diagnostics,
15411544
tcDiagnostics,
@@ -1934,7 +1937,7 @@ type internal TransparentCompiler
19341937
) : NodeCode<FSharpCheckFileAnswer> =
19351938
node {
19361939
let! snapshot =
1937-
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText)
1940+
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText, documentSource)
19381941
|> NodeCode.AwaitAsync
19391942

19401943
ignore parseResults
@@ -1955,7 +1958,7 @@ type internal TransparentCompiler
19551958
) : NodeCode<FSharpCheckFileAnswer option> =
19561959
node {
19571960
let! snapshot =
1958-
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText)
1961+
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText, documentSource)
19591962
|> NodeCode.AwaitAsync
19601963

19611964
ignore parseResults
@@ -2007,7 +2010,10 @@ type internal TransparentCompiler
20072010
) : NodeCode<seq<range>> =
20082011
node {
20092012
ignore canInvalidateProject
2010-
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
2013+
2014+
let! snapshot =
2015+
FSharpProjectSnapshot.FromOptions(options, documentSource)
2016+
|> NodeCode.AwaitAsync
20112017

20122018
return! this.FindReferencesInFile(fileName, snapshot.ProjectSnapshot, symbol, userOpName)
20132019
}
@@ -2020,7 +2026,10 @@ type internal TransparentCompiler
20202026

20212027
member this.GetAssemblyData(options: FSharpProjectOptions, fileName, userOpName: string) : NodeCode<ProjectAssemblyDataResult> =
20222028
node {
2023-
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
2029+
let! snapshot =
2030+
FSharpProjectSnapshot.FromOptions(options, documentSource)
2031+
|> NodeCode.AwaitAsync
2032+
20242033
return! this.GetAssemblyData(snapshot.ProjectSnapshot, fileName, userOpName)
20252034
}
20262035

@@ -2039,7 +2048,9 @@ type internal TransparentCompiler
20392048
userOpName: string
20402049
) : NodeCode<FSharpParseFileResults * FSharpCheckFileResults> =
20412050
node {
2042-
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
2051+
let! snapshot =
2052+
FSharpProjectSnapshot.FromOptions(options, documentSource)
2053+
|> NodeCode.AwaitAsync
20432054

20442055
match! this.ParseAndCheckFileInProject(fileName, snapshot.ProjectSnapshot, userOpName) with
20452056
| parseResult, FSharpCheckFileAnswer.Succeeded checkResult -> return parseResult, checkResult
@@ -2053,7 +2064,10 @@ type internal TransparentCompiler
20532064
userOpName: string
20542065
) : NodeCode<FSharpParseFileResults> =
20552066
node {
2056-
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
2067+
let! snapshot =
2068+
FSharpProjectSnapshot.FromOptions(options, documentSource)
2069+
|> NodeCode.AwaitAsync
2070+
20572071
return! this.ParseFile(fileName, snapshot.ProjectSnapshot, userOpName)
20582072
}
20592073

@@ -2068,7 +2082,7 @@ type internal TransparentCompiler
20682082
ignore builder
20692083

20702084
let! snapshot =
2071-
FSharpProjectSnapshot.FromOptions(options, fileName, 1, sourceText)
2085+
FSharpProjectSnapshot.FromOptions(options, fileName, 1, sourceText, documentSource)
20722086
|> NodeCode.AwaitAsync
20732087

20742088
match! this.ParseAndCheckFileInProject(fileName, snapshot.ProjectSnapshot, "GetCachedCheckFileResult") with
@@ -2118,7 +2132,11 @@ type internal TransparentCompiler
21182132
) : NodeCode<EditorServices.SemanticClassificationView option> =
21192133
node {
21202134
ignore userOpName
2121-
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
2135+
2136+
let! snapshot =
2137+
FSharpProjectSnapshot.FromOptions(options, documentSource)
2138+
|> NodeCode.AwaitAsync
2139+
21222140
return! ComputeSemanticClassification(fileName, snapshot.ProjectSnapshot)
21232141
}
21242142

@@ -2141,7 +2159,7 @@ type internal TransparentCompiler
21412159
) : NodeCode<FSharpParseFileResults * FSharpCheckFileAnswer> =
21422160
node {
21432161
let! snapshot =
2144-
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText)
2162+
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText, documentSource)
21452163
|> NodeCode.AwaitAsync
21462164

21472165
return! this.ParseAndCheckFileInProject(fileName, snapshot.ProjectSnapshot, userOpName)
@@ -2153,7 +2171,11 @@ type internal TransparentCompiler
21532171
member this.ParseAndCheckProject(options: FSharpProjectOptions, userOpName: string) : NodeCode<FSharpCheckProjectResults> =
21542172
node {
21552173
ignore userOpName
2156-
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
2174+
2175+
let! snapshot =
2176+
FSharpProjectSnapshot.FromOptions(options, documentSource)
2177+
|> NodeCode.AwaitAsync
2178+
21572179
return! ComputeParseAndCheckProject snapshot.ProjectSnapshot
21582180
}
21592181

src/Compiler/Service/service.fs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ open FSharp.Compiler.Text.Range
3737
open FSharp.Compiler.TcGlobals
3838
open FSharp.Compiler.BuildGraph
3939

40-
[<RequireQualifiedAccess>]
41-
type DocumentSource =
42-
| FileSystem
43-
| Custom of (string -> Async<ISourceText option>)
44-
4540
/// Callback that indicates whether a requested result has become obsolete.
4641
[<NoComparison; NoEquality>]
4742
type IsResultObsolete = IsResultObsolete of (unit -> bool)

src/Compiler/Service/service.fsi

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ open FSharp.Compiler.Syntax
1919
open FSharp.Compiler.Text
2020
open FSharp.Compiler.Tokenization
2121

22-
[<Experimental "This type is experimental and likely to be removed in the future.">]
23-
[<RequireQualifiedAccess>]
24-
type DocumentSource =
25-
| FileSystem
26-
| Custom of (string -> Async<ISourceText option>)
27-
2822
/// Used to parse and check F# source code.
2923
[<Sealed; AutoSerializable(false)>]
3024
type public FSharpChecker =

tests/FSharp.Compiler.ComponentTests/CompilerService/AsyncMemoize.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type internal EventRecorder<'a, 'b, 'c when 'a : equality and 'b : equality>(mem
5454
let actual = events |> Seq.toArray
5555
Assert.Equal<_ array>(expected, actual)
5656

57+
member _.Sequence = events |> Seq.map id
58+
5759

5860
[<Fact>]
5961
let ``Basics``() =
@@ -63,10 +65,8 @@ let ``Basics``() =
6365
return key * 2
6466
}
6567

66-
let eventLog = ConcurrentBag()
67-
6868
let memoize = AsyncMemoize<int, int, int>()
69-
memoize.OnEvent(fun (e, (_label, k, _version)) -> eventLog.Add (e, k))
69+
let events = EventRecorder(memoize)
7070

7171
let result =
7272
seq {
@@ -84,7 +84,9 @@ let ``Basics``() =
8484

8585
Assert.Equal<int array>(expected, result)
8686

87-
let groups = eventLog |> Seq.groupBy snd |> Seq.toList
87+
(waitUntil (events.CountOf Finished) 3).Wait()
88+
89+
let groups = events.Sequence |> Seq.groupBy snd |> Seq.toList
8890
Assert.Equal(3, groups.Length)
8991
for key, events in groups do
9092
Assert.Equal<Set<(JobEvent * int)>>(Set [ Requested, key; Started, key; Finished, key ], Set events)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ let fuzzingTest seed (project: SyntheticProject) = task {
651651
[<InlineData(SignatureFiles.Some)>]
652652
let Fuzzing signatureFiles =
653653
654-
let seed = 1106087513
654+
let seed = System.Random().Next()
655655
let rng = System.Random(int seed)
656656
657657
let fileCount = 30
@@ -711,7 +711,6 @@ type GiraffeTheoryAttribute() =
711711
[<InlineData false>]
712712
let GiraffeFuzzing signatureFiles =
713713
let seed = System.Random().Next()
714-
//let seed = 1044159179
715714

716715
let giraffeDir = if signatureFiles then giraffeSignaturesDir else giraffeDir
717716
let giraffeTestsDir = if signatureFiles then giraffeSignaturesTestsDir else giraffeTestsDir

tests/FSharp.Compiler.ComponentTests/TypeChecks/TyparNameTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module TyparNameTests =
1414
(additionalFile: SourceCodeFileKind)
1515
: string array =
1616
let typeCheckResult =
17-
cUnit |> withAdditionalSourceFile additionalFile |> typecheckProject false false
17+
cUnit |> withAdditionalSourceFile additionalFile |> typecheckProject false CompilerAssertHelpers.UseTransparentCompiler
1818

1919
assert (Array.isEmpty typeCheckResult.Diagnostics)
2020

0 commit comments

Comments
 (0)