Skip to content

Commit 9fb878b

Browse files
committed
add a test
1 parent 2db5e17 commit 9fb878b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@
351351
<Compile Include="FSharpChecker\TransparentCompiler.fs" />
352352
<Compile Include="FSharpChecker\SymbolUse.fs" />
353353
<Compile Include="FSharpChecker\FindReferences.fs" />
354+
<Compile Include="Optimizer\NestedApplications.fs" />
354355
<Compile Include="Attributes\AttributeCtorSetPropAccess.fs" />
355356
</ItemGroup>
356357

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace FSharp.Compiler.ComponentTests.Optimizer
2+
3+
open System.Text
4+
open Xunit
5+
open FSharp.Test
6+
open FSharp.Test.Compiler
7+
open FSharp.Test.Utilities
8+
9+
module private Gen =
10+
let nestedLetApps depth =
11+
// Builds: let v1 = id 0 in let v2 = id v1 in ... in ignore vN
12+
let sb = StringBuilder()
13+
sb.AppendLine("module M") |> ignore
14+
sb.AppendLine("let id x = x") |> ignore
15+
sb.AppendLine("let run () =") |> ignore
16+
for i in 1 .. depth do
17+
if i = 1 then
18+
sb.Append(" let v1 = id 0") |> ignore
19+
else
20+
sb.Append(" in let v").Append(i).Append(" = id v").Append(i-1) |> ignore
21+
sb.AppendLine(" in ()") |> ignore
22+
sb.ToString()
23+
24+
let nestedDirectApps depth =
25+
// Builds: let res = id(id(id(...(0)))) in ignore res
26+
let sb = StringBuilder()
27+
sb.AppendLine("module N") |> ignore
28+
sb.AppendLine("let id x = x") |> ignore
29+
sb.Append("let run () = let res = ") |> ignore
30+
for _ in 1 .. depth do
31+
sb.Append("id (") |> ignore
32+
sb.Append("0") |> ignore
33+
for _ in 1 .. depth do
34+
sb.Append(")") |> ignore
35+
sb.AppendLine(" in ignore res") |> ignore
36+
sb.ToString()
37+
38+
[<Collection(nameof NotThreadSafeResourceCollection)>]
39+
type ``Nested application optimizer``() =
40+
41+
// Moderate depths to keep CI stable while still exercising the quadratic shapes
42+
[<Theory>]
43+
[<InlineData(100)>]
44+
[<InlineData(1000)>]
45+
let ``let-chains of nested apps compile under --optimize+`` depth =
46+
let src = Gen.nestedLetApps depth
47+
FSharp src
48+
|> withOptions [ "--optimize+"; "--times" ]
49+
|> asExe
50+
|> ignoreWarnings
51+
|> compile
52+
|> shouldSucceed
53+
54+
[<Theory>]
55+
[<InlineData(100)>]
56+
[<InlineData(1000)>]
57+
let ``direct nested application compiles under --optimize+`` depth =
58+
let src = Gen.nestedDirectApps depth
59+
FSharp src
60+
|> withOptions [ "--optimize+"; "--times" ]
61+
|> asExe
62+
|> ignoreWarnings
63+
|> compile
64+
|> shouldSucceed

0 commit comments

Comments
 (0)