Skip to content

Commit 88d5f18

Browse files
committed
Global namespace and nested modules with AutoOpen.
1 parent d8f2579 commit 88d5f18

File tree

4 files changed

+116
-45
lines changed

4 files changed

+116
-45
lines changed

tests/ParallelTypeCheckingTests/Code/TrieApproach/AutoOpenDetection.fs renamed to tests/ParallelTypeCheckingTests/Code/TrieApproach/AlwaysLinkDetection.fs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module ParallelTypeCheckingTests.Code.TrieApproach.AutoOpenDetection
1+
module ParallelTypeCheckingTests.Code.TrieApproach.AlwaysLinkDetection
22

33
open FSharp.Compiler.Syntax
44

@@ -28,14 +28,23 @@ let isAutoOpenAttribute (attribute: SynAttribute) =
2828
| _ -> false
2929

3030
let isAnyAttributeAutoOpen (attributes: SynAttributes) =
31-
List.exists (fun (atl: SynAttributeList) -> List.exists isAutoOpenAttribute atl.Attributes) attributes
31+
not attributes.IsEmpty
32+
&& List.exists (fun (atl: SynAttributeList) -> List.exists isAutoOpenAttribute atl.Attributes) attributes
3233

33-
let hasAutoOpenAttributeInFile (ast: ParsedInput) : bool =
34+
let doesFileHasAutoOpenBehavior (ast: ParsedInput) : bool =
3435
match ast with
3536
| ParsedInput.SigFile (ParsedSigFileInput (contents = contents)) ->
36-
List.exists (fun (SynModuleOrNamespaceSig (attribs = attribs)) -> isAnyAttributeAutoOpen attribs) contents
37+
List.exists
38+
(fun (SynModuleOrNamespaceSig (attribs = attribs; kind = kind)) ->
39+
isAnyAttributeAutoOpen attribs
40+
|| kind = SynModuleOrNamespaceKind.GlobalNamespace)
41+
contents
3742
| ParsedInput.ImplFile (ParsedImplFileInput (contents = contents)) ->
38-
List.exists (fun (SynModuleOrNamespace (attribs = attribs)) -> isAnyAttributeAutoOpen attribs) contents
43+
List.exists
44+
(fun (SynModuleOrNamespace (attribs = attribs; kind = kind)) ->
45+
isAnyAttributeAutoOpen attribs
46+
|| kind = SynModuleOrNamespaceKind.GlobalNamespace)
47+
contents
3948

4049
// ==============================================================================================================================
4150
// ==============================================================================================================================
@@ -50,4 +59,4 @@ let ``detect auto open`` () =
5059
Path.Combine(__SOURCE_DIRECTORY__, "..", "..", "..", "..", "src", "Compiler", "Utilities", "ImmutableArray.fsi")
5160

5261
let ast = parseSourceCode (file, File.ReadAllText(file))
53-
Assert.True(hasAutoOpenAttributeInFile ast)
62+
Assert.True(doesFileHasAutoOpenBehavior ast)

tests/ParallelTypeCheckingTests/Code/TrieApproach/DependencyResolution.fs

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ let collectGhostDependencies (fileIndex: int) (trie: TrieNode) (queryTrie: Query
172172

173173
visit trie path
174174

175-
let children = indexesUnderNode node
175+
let children = indexesUnderNode node |> Set.filter (fun idx -> idx < fileIndex)
176176
let intersection = Set.intersect result.FoundDependencies children
177177

178178
if Set.isEmpty intersection then
@@ -213,15 +213,16 @@ let mkGraph (files: FileWithAST array) =
213213
time
214214
"filesWithAutoOpen"
215215
(Array.choose (fun f ->
216-
if AutoOpenDetection.hasAutoOpenAttributeInFile f.AST then
216+
if AlwaysLinkDetection.doesFileHasAutoOpenBehavior f.AST then
217217
Some f.Idx
218218
else
219219
None))
220220
trieInput
221221

222222
time
223223
"link deps"
224-
Array.Parallel.map
224+
// Array.Parallel.map
225+
Array.map
225226
(fun (file: FileWithAST) ->
226227
let fileContent = fileContents.[file.Idx]
227228
let knownFiles = getFileNameBefore files file.Idx
@@ -286,33 +287,33 @@ let ``Fantomas.Core for realzies`` () =
286287
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\obj\Debug\netstandard2.0\.NETStandard,Version=v2.0.AssemblyAttributes.fs"
287288
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\obj\Debug\netstandard2.0\Fantomas.Core.AssemblyInfo.fs"
288289
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AssemblyInfo.fs"
289-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\ISourceTextExtensions.fs"
290-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\RangeHelpers.fs"
291-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstExtensions.fsi"
292-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstExtensions.fs"
293-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\TriviaTypes.fs"
294-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Utils.fs"
295-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\SourceParser.fs"
296-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstTransformer.fsi"
297-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstTransformer.fs"
298-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Version.fs"
299-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Queue.fs"
300-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\FormatConfig.fs"
301-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Defines.fsi"
302-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Defines.fs"
303-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Trivia.fsi"
304-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Trivia.fs"
305-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\SourceTransformer.fs"
306-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Context.fs"
307-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodePrinter.fsi"
308-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodePrinter.fs"
309-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatterImpl.fsi"
310-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatterImpl.fs"
311-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Validation.fs"
312-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Selection.fsi"
313-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Selection.fs"
314-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatter.fsi"
315-
@"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatter.fs"
290+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\ISourceTextExtensions.fs"
291+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\RangeHelpers.fs"
292+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstExtensions.fsi"
293+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstExtensions.fs"
294+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\TriviaTypes.fs"
295+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Utils.fs"
296+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\SourceParser.fs"
297+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstTransformer.fsi"
298+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\AstTransformer.fs"
299+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Version.fs"
300+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Queue.fs"
301+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\FormatConfig.fs"
302+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Defines.fsi"
303+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Defines.fs"
304+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Trivia.fsi"
305+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Trivia.fs"
306+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\SourceTransformer.fs"
307+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Context.fs"
308+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodePrinter.fsi"
309+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodePrinter.fs"
310+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatterImpl.fsi"
311+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatterImpl.fs"
312+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Validation.fs"
313+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Selection.fsi"
314+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\Selection.fs"
315+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatter.fsi"
316+
// @"C:\Users\nojaf\Projects\main-fantomas\src\Fantomas.Core\CodeFormatter.fs"
316317
|]
317318
|> mkGraphAndReport
318319

@@ -917,6 +918,51 @@ open X
917918
"""
918919
(set [| 0; 1 |])
919920
]
921+
// The nested module in this case adds content to the namespace
922+
// Similar if a namespace had a type.
923+
scenario
924+
"Nested module with auto open attribute"
925+
[
926+
sourceFile
927+
"A.fs"
928+
"""
929+
namespace Product
930+
931+
[<AutoOpen>]
932+
module X =
933+
let x: int = 0
934+
"""
935+
Set.empty
936+
sourceFile
937+
"B.fs"
938+
"""
939+
module Product.Feature
940+
941+
let a b = x + b
942+
"""
943+
(set [| 0 |])
944+
]
945+
// Similar to a top level auto open attribute, the global namespace also introduces a link to all the files that come after it.
946+
scenario
947+
"Global namespace always introduces a link"
948+
[
949+
sourceFile
950+
"A.fs"
951+
"""
952+
namespace global
953+
954+
type A = { B : int }
955+
"""
956+
Set.empty
957+
sourceFile
958+
"B.fs"
959+
"""
960+
module Product.Feature
961+
962+
let f a = a.B
963+
"""
964+
(set [| 0 |])
965+
]
920966
]
921967

922968
[<TestCaseSource(nameof codebases)>]

tests/ParallelTypeCheckingTests/Code/TrieApproach/TrieMapping.fs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,21 @@ let rec mkTrieNodeFor (file: FileWithAST) : TrieNode =
5151
| ParsedInput.SigFile (ParsedSigFileInput (contents = contents)) ->
5252
contents
5353
|> List.choose (fun (SynModuleOrNamespaceSig (longId = longId; kind = kind; decls = decls; accessibility = _accessibility)) ->
54-
let hasTypes =
54+
let hasTypesOrAutoOpenNestedModules =
5555
List.exists
5656
(function
5757
| SynModuleSigDecl.Types _ -> true
58+
| SynModuleSigDecl.NestedModule(moduleInfo = SynComponentInfo (attributes = attributes)) ->
59+
AlwaysLinkDetection.isAnyAttributeAutoOpen attributes
5860
| _ -> false)
5961
decls
6062

6163
let isNamespace =
6264
match kind with
6365
| SynModuleOrNamespaceKind.AnonModule
6466
| SynModuleOrNamespaceKind.NamedModule -> false
65-
| SynModuleOrNamespaceKind.DeclaredNamespace -> true
66-
| SynModuleOrNamespaceKind.GlobalNamespace -> failwith "Not quite sure yet how to perceive this"
67+
| SynModuleOrNamespaceKind.DeclaredNamespace
68+
| SynModuleOrNamespaceKind.GlobalNamespace -> true
6769

6870
match longId with
6971
| [] -> None
@@ -77,7 +79,13 @@ let rec mkTrieNodeFor (file: FileWithAST) : TrieNode =
7779

7880
let current =
7981
if isNamespace then
80-
TrieNodeInfo.Namespace(name, (if hasTypes then hs file.Idx else emptyHS ()))
82+
TrieNodeInfo.Namespace(
83+
name,
84+
(if hasTypesOrAutoOpenNestedModules then
85+
hs file.Idx
86+
else
87+
emptyHS ())
88+
)
8189
else
8290
TrieNodeInfo.Module(name, file.Idx)
8391

@@ -114,19 +122,21 @@ let rec mkTrieNodeFor (file: FileWithAST) : TrieNode =
114122
| ParsedInput.ImplFile (ParsedImplFileInput (contents = contents)) ->
115123
contents
116124
|> List.choose (fun (SynModuleOrNamespace (longId = longId; kind = kind; decls = decls; accessibility = _accessibility)) ->
117-
let hasTypes =
125+
let hasTypesOrAutoOpenNestedModules =
118126
List.exists
119127
(function
120128
| SynModuleDecl.Types _ -> true
129+
| SynModuleDecl.NestedModule(moduleInfo = SynComponentInfo (attributes = attributes)) ->
130+
AlwaysLinkDetection.isAnyAttributeAutoOpen attributes
121131
| _ -> false)
122132
decls
123133

124134
let isNamespace =
125135
match kind with
126136
| SynModuleOrNamespaceKind.AnonModule
127137
| SynModuleOrNamespaceKind.NamedModule -> false
128-
| SynModuleOrNamespaceKind.DeclaredNamespace -> true
129-
| SynModuleOrNamespaceKind.GlobalNamespace -> failwith "Not quite sure yet how to perceive this"
138+
| SynModuleOrNamespaceKind.DeclaredNamespace
139+
| SynModuleOrNamespaceKind.GlobalNamespace -> true
130140

131141
match longId with
132142
| [] -> None
@@ -140,7 +150,13 @@ let rec mkTrieNodeFor (file: FileWithAST) : TrieNode =
140150

141151
let current =
142152
if isNamespace then
143-
TrieNodeInfo.Namespace(name, (if hasTypes then hs file.Idx else emptyHS ()))
153+
TrieNodeInfo.Namespace(
154+
name,
155+
(if hasTypesOrAutoOpenNestedModules then
156+
hs file.Idx
157+
else
158+
emptyHS ())
159+
)
144160
else
145161
TrieNodeInfo.Module(name, file.Idx)
146162

tests/ParallelTypeCheckingTests/ParallelTypeCheckingTests.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
<Compile Include="Code\ParallelTypeChecking.fs" />
4040
<Compile Include="Code\TrieApproach\Types.fs" />
4141
<Compile Include="Code\TrieApproach\Continuation.fs" />
42+
<Compile Include="Code\TrieApproach\AlwaysLinkDetection.fs" />
4243
<Compile Include="Code\TrieApproach\TrieMapping.fs" />
4344
<Compile Include="Code\TrieApproach\FileContentMapping.fs" />
44-
<Compile Include="Code\TrieApproach\AutoOpenDetection.fs" />
4545
<Compile Include="Code\TrieApproach\DependencyResolution.fs" />
4646
<Compile Include="Code\TrieApproach\SampleData.fs" />
4747
<Compile Include="Tests\Utils.fs" />

0 commit comments

Comments
 (0)