@@ -4,6 +4,7 @@ namespace Language
4
4
5
5
open Xunit
6
6
open FSharp.Test .Compiler
7
+ open FSharp.Test
7
8
8
9
module StateMachineTests =
9
10
@@ -160,4 +161,83 @@ let main _ =
160
161
|> ignoreWarnings
161
162
|> withOptimize
162
163
|> compileExeAndRun
163
- |> shouldSucceed
164
+ |> shouldSucceed
165
+
166
+ [<Fact>]
167
+ let ``State machine defined as top level value is statically compiled`` () =
168
+ Fsx """
169
+ let test = task { return 42 }
170
+ if test.Result <> 42 then failwith "expected 42"
171
+
172
+ task { printfn "Hello, World!"; return 42 }
173
+ """
174
+ |> runFsi
175
+ |> shouldSucceed
176
+
177
+ [<Fact>]
178
+ let ``State machine defined as top level has a generated MoveNext method`` () =
179
+ FSharp """
180
+ module TestStateMachine
181
+ let test = task { return 42 }
182
+ """
183
+ |> compile
184
+ |> verifyIL [ """
185
+ .method public strict virtual instance void MoveNext() cil managed
186
+ {
187
+ .override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
188
+
189
+ .maxstack 4
190
+ .locals init (int32 V_0,
191
+ class [runtime]System.Exception V_1,
192
+ bool V_2,
193
+ class [runtime]System.Exception V_3)
194
+ IL_0000: ldarg.0
195
+ IL_0001: ldfld int32 TestStateMachine/test@3::ResumptionPoint
196
+ IL_0006: stloc.0
197
+ .try
198
+ {
199
+ IL_0007: ldarg.0
200
+ IL_0008: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> TestStateMachine/test@3::Data
201
+ IL_000d: ldc.i4.s 42
202
+ IL_000f: stfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
203
+ IL_0014: ldc.i4.1
204
+ IL_0015: stloc.2
205
+ IL_0016: ldloc.2
206
+ IL_0017: brfalse.s IL_0036
207
+
208
+ IL_0019: ldarg.0
209
+ IL_001a: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> TestStateMachine/test@3::Data
210
+ IL_001f: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
211
+ IL_0024: ldarg.0
212
+ IL_0025: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> TestStateMachine/test@3::Data
213
+ IL_002a: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
214
+ IL_002f: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetResult(!0)
215
+ IL_0034: leave.s IL_0042
216
+
217
+ IL_0036: leave.s IL_0042
218
+
219
+ }
220
+ catch [runtime]System.Object
221
+ {
222
+ IL_0038: castclass [runtime]System.Exception
223
+ IL_003d: stloc.3
224
+ IL_003e: ldloc.3
225
+ IL_003f: stloc.1
226
+ IL_0040: leave.s IL_0042
227
+
228
+ }
229
+ IL_0042: ldloc.1
230
+ IL_0043: stloc.3
231
+ IL_0044: ldloc.3
232
+ IL_0045: brtrue.s IL_0048
233
+
234
+ IL_0047: ret
235
+
236
+ IL_0048: ldarg.0
237
+ IL_0049: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> TestStateMachine/test@3::Data
238
+ IL_004e: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
239
+ IL_0053: ldloc.3
240
+ IL_0054: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetException(class [netstandard]System.Exception)
241
+ IL_0059: ret
242
+ }
243
+ """ ]
0 commit comments