Skip to content

Commit ec8fca7

Browse files
committed
Take impl/sig pairing into account in TrieMapping and DependencyResolution.
1 parent 43881d5 commit ec8fca7

4 files changed

Lines changed: 57 additions & 14 deletions

File tree

tests/ParallelTypeCheckingTests/Code/DependencyResolution.fs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,34 @@ let collectGhostDependencies (fileIndex: int) (trie: TrieNode) (queryTrie: Query
179179
Array.empty)
180180

181181
let mkGraph (files: FileWithAST array) : Graph<int> =
182+
// File pairs to easily retrieve the signature file from a
183+
let implToSig, sigToImpl =
184+
Array.choose
185+
(fun f ->
186+
match f.AST with
187+
| ParsedInput.SigFile _ ->
188+
let implIdx =
189+
files
190+
|> Array.skip (f.Idx + 1)
191+
|> Array.tryFind (fun (implFile: FileWithAST) -> $"{implFile.AST.FileName}i" = f.File)
192+
193+
Option.map (fun (implFile: FileWithAST) -> (implFile.Idx, f.Idx), (f.Idx, implFile.Idx)) implIdx
194+
| ParsedInput.ImplFile _ -> None)
195+
files
196+
|> Array.unzip
197+
|> fun (implToSig, sigToImpl) -> Map.ofArray implToSig, Map.ofArray sigToImpl
198+
182199
// Implementation files backed by signatures should be excluded to construct the trie.
200+
// Signature files should link to the implementation index instead.
183201
let trieInput =
184-
Array.filter
202+
Array.choose
185203
(fun f ->
186204
match f.AST with
187-
| ParsedInput.SigFile _ -> true
188-
| ParsedInput.ImplFile _ -> Array.forall (fun (sigFile: FileWithAST) -> sigFile.File <> $"{f.File}i") files)
205+
| ParsedInput.SigFile _ -> Map.tryFind f.Idx sigToImpl |> Option.map (fun implIdx -> (f, implIdx))
206+
| ParsedInput.ImplFile _ ->
207+
match Map.tryFind f.Idx implToSig with
208+
| Some _ -> None
209+
| None -> Some(f, f.Idx))
189210
files
190211

191212
let trie = TrieMapping.mkTrie trieInput
@@ -195,7 +216,7 @@ let mkGraph (files: FileWithAST array) : Graph<int> =
195216

196217
let filesWithAutoOpen =
197218
Array.choose
198-
(fun f ->
219+
(fun (f, _) ->
199220
if AlwaysLinkDetection.doesFileHasAutoOpenBehavior f.AST then
200221
Some f.Idx
201222
else
@@ -221,11 +242,18 @@ let mkGraph (files: FileWithAST array) : Graph<int> =
221242
else
222243
[| 0 .. (file.Idx - 1) |].Intersect(filesWithAutoOpen).ToArray()
223244

245+
// Automatically add a link from an implementation to its signature file (if present)
246+
let signatureDependency =
247+
match Map.tryFind file.Idx implToSig with
248+
| None -> Array.empty
249+
| Some sigIdx -> Array.singleton sigIdx
250+
224251
let allDependencies =
225252
[|
226253
yield! result.FoundDependencies
227254
yield! ghostDependencies
228255
yield! topLevelAutoOpenFiles
256+
yield! signatureDependency
229257
|]
230258
|> Array.distinct
231259

tests/ParallelTypeCheckingTests/Code/TrieMapping.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let mergeTrieNodes (defaultChildSize: int) (tries: TrieNode array) =
4646
let hs f = HashSet(Seq.singleton f)
4747
let emptyHS () = HashSet(0)
4848

49-
let rec mkTrieNodeFor (file: FileWithAST) : TrieNode =
49+
let rec mkTrieNodeFor (file: FileWithAST, idx: int) : TrieNode =
5050
match file.AST with
5151
| ParsedInput.SigFile (ParsedSigFileInput (contents = contents)) ->
5252
contents
@@ -82,14 +82,14 @@ let rec mkTrieNodeFor (file: FileWithAST) : TrieNode =
8282
TrieNodeInfo.Namespace(
8383
name,
8484
(if hasTypesOrAutoOpenNestedModules then
85-
hs file.Idx
85+
hs idx
8686
else
8787
emptyHS ())
8888
)
8989
else
90-
TrieNodeInfo.Module(name, file.Idx)
90+
TrieNodeInfo.Module(name, idx)
9191

92-
let children = List.choose (mkTrieForNestedSigModule file.Idx) decls
92+
let children = List.choose (mkTrieForNestedSigModule idx) decls
9393

9494
continuation (
9595
Dictionary<_, _>(
@@ -154,14 +154,14 @@ let rec mkTrieNodeFor (file: FileWithAST) : TrieNode =
154154
TrieNodeInfo.Namespace(
155155
name,
156156
(if hasTypesOrAutoOpenNestedModules then
157-
hs file.Idx
157+
hs idx
158158
else
159159
emptyHS ())
160160
)
161161
else
162-
TrieNodeInfo.Module(name, file.Idx)
162+
TrieNodeInfo.Module(name, idx)
163163

164-
let children = List.choose (mkTrieForSynModuleDecl file.Idx) decls
164+
let children = List.choose (mkTrieForSynModuleDecl idx) decls
165165

166166
continuation (
167167
Dictionary<_, _>(
@@ -228,7 +228,7 @@ and mkTrieForNestedSigModule (fileIndex: int) (decl: SynModuleSigDecl) : KeyValu
228228

229229
| _ -> None
230230

231-
let mkTrie (files: FileWithAST array) : TrieNode =
231+
let mkTrie (files: (FileWithAST * int) array) : TrieNode =
232232
mergeTrieNodes 0 (Array.Parallel.map mkTrieNodeFor files)
233233

234234
// ==================================================================================================================================================
@@ -266,7 +266,7 @@ let ``Fantomas Core trie`` () =
266266
|]
267267
|> Array.mapi (fun idx file ->
268268
let ast = parseSourceCode (file, System.IO.File.ReadAllText(file))
269-
{ Idx = idx; File = file; AST = ast })
269+
{ Idx = idx; File = file; AST = ast }, idx)
270270

271271
let trie = mkTrie files
272272
ignore trie

tests/ParallelTypeCheckingTests/Tests/TestCompilation.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,17 @@ let ``Compile a valid scenario using graph-based type-checking`` (scenario: Scen
275275
Method = Method.Graph
276276
Project = project
277277
}
278+
279+
[<TestCaseSource(nameof scenarios)>]
280+
let ``Compile a valid scenario using sequential type-checking`` (scenario: Scenario) =
281+
let project =
282+
scenario.Files
283+
|> Array.map (fun (f: FileInScenario) -> f.FileWithAST.File, f.Content)
284+
|> List.ofArray
285+
|> FProject.Make CompileOutput.Library
286+
287+
compileAValidProject
288+
{
289+
Method = Method.Sequential
290+
Project = project
291+
}

tests/ParallelTypeCheckingTests/Tests/TrieMappingTests.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ let ``Basic trie`` () =
3535
Idx = idx
3636
File = fileName
3737
AST = parseSourceCode (fileName, code)
38-
})
38+
},
39+
idx)
3940

4041
let trie = TrieMapping.mkTrie files
4142

0 commit comments

Comments
 (0)