Skip to content
16 changes: 2 additions & 14 deletions src/Compiler/Optimize/LowerStateMachines.fs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@ let RepresentBindingAsStateVar g (bind: Binding) (resBody: StateMachineConversio
let isExpandVar g (v: Val) =
isReturnsResumableCodeTy g v.TauType

// We allow a prefix of bindings prior to the state machine, e.g.
// task { .. }
// becomes
// let builder@ = task
// ....
let isStateMachineBindingVar g (v: Val) =
isExpandVar g v ||
(let nm = v.LogicalName
(nm.StartsWithOrdinal("builder@") || v.IsMemberThisVal))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why it was needed before?
Are the recent state machine bug fixes the reason why we no longer need this special casing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the recent state machine bug fixes the reason why we no longer need this special casing?

No, this is completely independent. I did a checkout of old commit and applied this fix to check it.

Do we know why it was needed before?

Ha, this is not entirely clear. That's why I'm a bit wary about this.


type env =
{
ResumableCodeDefns: ValMap<Expr>
Expand All @@ -128,7 +118,7 @@ type env =
let rec IsStateMachineExpr g overallExpr =
match overallExpr with
// 'let' binding of initial code
| Expr.Let (defn, bodyExpr, m, _) when isStateMachineBindingVar g defn.Var ->
| Expr.Let (defn, bodyExpr, m, _) when isExpandVar g defn.Var ->
match IsStateMachineExpr g bodyExpr with
| None -> None
| Some altExpr as r ->
Expand Down Expand Up @@ -179,7 +169,7 @@ type LowerStateMachine(g: TcGlobals, outerResumableCodeDefns: ValMap<Expr>) =

match expr with
// Bind 'let __expand_ABC = bindExpr in bodyExpr'
| Expr.Let (defn, bodyExpr, _, _) when isStateMachineBindingVar g defn.Var ->
| Expr.Let (defn, bodyExpr, _, _) when isExpandVar g defn.Var ->
if sm_verbose then printfn "binding %A --> %A..." defn.Var defn.Expr
let envR = { env with ResumableCodeDefns = env.ResumableCodeDefns.Add defn.Var defn.Expr }
BindResumableCodeDefinitions envR bodyExpr
Expand Down Expand Up @@ -361,7 +351,6 @@ type LowerStateMachine(g: TcGlobals, outerResumableCodeDefns: ValMap<Expr>) =
TryReduceExpr env f (args2 @ args) (fun f2 -> remake (Expr.App (f2, _fty, _tyargs, args2, _m)))

| _ ->
//let (env, expr) = BindResumableCodeDefinitions env expr
match TryReduceApp env expr args with
| Some expandedExpr ->
if sm_verbose then printfn "reduction = %A, args = %A --> %A..." expr args expandedExpr
Expand Down Expand Up @@ -408,7 +397,6 @@ type LowerStateMachine(g: TcGlobals, outerResumableCodeDefns: ValMap<Expr>) =
// Detect a state machine with a single method override
[<return: Struct>]
let (|ExpandedStateMachineInContext|_|) inputExpr =
// All expanded resumable code state machines e.g. 'task { .. }' begin with a bind of @builder or 'defn'
// Seed the env with any expand-var definitions from outer scopes (e.g. across lambda boundaries)
let initialEnv = { env.Empty with ResumableCodeDefns = outerResumableCodeDefns }
let env, expr = BindResumableCodeDefinitions initialEnv inputExpr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,34 @@ if result[0].x <> 1 then failwith $"unexpected result {result[0]}"
|> asExe
|> compileExeAndRun
|> shouldSucceed

[<Fact>]
let ``Debug-mode: mixing resumable and standard computation expressions compiles``() =
FSharp """
module ReproMixedBuilders
open System.Threading.Tasks

type TaskMaybeBuilder() =

member inline _.Zero() = Task.FromResult None

member inline _.Delay([<InlineIfLambda>] f) = task { return! f () }

member inline _.Bind(value, [<InlineIfLambda>] f) =
task {
match value with
| None -> return None
| Some result -> return! f result
}

let taskMaybe = TaskMaybeBuilder()

let trigger() =
taskMaybe {
do! None
}
"""
|> withDebug
|> withNoOptimize
|> compile
|> shouldSucceed
Loading
Loading