Skip to content

Commit 3423c53

Browse files
committed
Fix top level effect
1 parent ae9d3ca commit 3423c53

File tree

8 files changed

+13
-14
lines changed

8 files changed

+13
-14
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
207207

208208
// coalesce useless states
209209
def optParts(entryState: BlockState, states: Ls[BlockState]): (BlockState, Ls[BlockState]) =
210+
return (entryState, states)
210211
val statesMap = (entryState :: states).map(state => state.id -> state).toMap
211212
def findEdges(state: BlockState) =
212213
var edges: List[BlockState] = Nil
@@ -831,15 +832,15 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
831832
val transform = new BlockTransformerShallow(SymbolSubst()):
832833
override def applyBlock(b: Block): Block = b match
833834
case ResultPlaceholder(res, uid, c, rest) =>
834-
val (doUnwindCall, implct) = doUnwind match
835-
case None => (topLevelCall(LinkState(res, clsSym.asPath, Value.Lit(Tree.IntLit(uid)))), true)
836-
case Some(doUnwind) => (PureCall(doUnwind.get, res.asPath :: Value.Lit(Tree.IntLit(uid)) :: Nil), false)
835+
val doUnwindBlk = doUnwind match
836+
case None => Assign(res, topLevelCall(LinkState(res, clsSym.asPath, Value.Lit(Tree.IntLit(uid)))), End())
837+
case Some(doUnwind) => Return(PureCall(doUnwind.get, res.asPath :: Value.Lit(Tree.IntLit(uid)) :: Nil), false)
837838
blockBuilder
838839
.assign(res, c)
839840
.ifthen(
840841
res.asPath,
841842
Case.Cls(paths.effectSigSym, paths.effectSigPath),
842-
Return(doUnwindCall, implct)
843+
doUnwindBlk
843844
)
844845
.rest(applyBlock(rest))
845846
case _ => super.applyBlock(b)

hkmc2/shared/src/test/mlscript/handlers/BadHandlers.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ h.oops
2020
//│ h = new Handler$h$1();
2121
//│ tmp = handleBlock$();
2222
//│ if (tmp instanceof runtime.EffectSig.class) {
23-
//│ runtime.topLevelEffect(tmp, false)
23+
//│ tmp = runtime.topLevelEffect(tmp, false);
2424
//│ }
2525
//│ tmp
2626

hkmc2/shared/src/test/mlscript/handlers/Debugging.mls

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ fun f() =
224224
print(debug.getLocals())
225225
//│ i = 100
226226

227-
:fixme
228227
f()
229228
//│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])]
230229
//│ > Stack Trace:
@@ -234,4 +233,3 @@ f()
234233
//│ > Stack Trace:
235234
//│ > at f (Debugging.mls:223:3) with locals: j=300
236235
//│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])]
237-
//│ ═══[RUNTIME ERROR] RangeError: Maximum call stack size exceeded

hkmc2/shared/src/test/mlscript/handlers/Effects.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ if true do
241241
//│ h11 = new Handler$h$23();
242242
//│ tmp11 = handleBlock$11();
243243
//│ if (tmp11 instanceof runtime.EffectSig.class) {
244-
//│ runtime.topLevelEffect(tmp11, false)
244+
//│ tmp11 = runtime.topLevelEffect(tmp11, false);
245245
//│ }
246246
//│ tmp11
247247
//│ = 3

hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ str
415415
//│ h11 = new Handler$h1$3();
416416
//│ tmp9 = handleBlock$7();
417417
//│ if (tmp9 instanceof runtime.EffectSig.class) {
418-
//│ runtime.topLevelEffect(tmp9, false)
418+
//│ tmp9 = runtime.topLevelEffect(tmp9, false);
419419
//│ }
420420
//│ tmp10 = tmp9;
421421
//│ } else {

hkmc2/shared/src/test/mlscript/handlers/ReturnInHandler.mls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let l = () =>
4040
return 3
4141
4
4242
//│ ╔══[ERROR] Return statements are not allowed outside of a function.
43-
//│ ║ l.55: return 3
43+
//│ ║ l.40: return 3
4444
//│ ╙── ^
4545
//│ l = [function l]
4646

@@ -58,7 +58,7 @@ let l = () =>
5858
4
5959
l()
6060
//│ ╔══[ERROR] Return statements are not allowed outside of a function.
61-
//│ ║ l.85: return 3
61+
//│ ║ l.57: return 3
6262
//│ ╙── ^
6363

6464

hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ x
1616
//│ x = tmp1;
1717
//│ tmp2 = Predef.print(x);
1818
//│ if (tmp2 instanceof runtime.EffectSig.class) {
19-
//│ runtime.topLevelEffect(tmp2, false)
19+
//│ tmp2 = runtime.topLevelEffect(tmp2, false);
2020
//│ }
2121
//│ tmp = tmp2;
2222
//│ } finally {

hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ hi(0)
7171
//│ });
7272
//│ res = runtime.runStackSafe(5, $_stack$_safe$_body$_);
7373
//│ if (res instanceof runtime.EffectSig.class) {
74-
//│ runtime.topLevelEffect(res, false)
74+
//│ res = runtime.topLevelEffect(res, false);
7575
//│ }
7676
//│ res
7777
//│ = 0
@@ -167,7 +167,7 @@ sum(10000)
167167
//│ });
168168
//│ res1 = runtime.runStackSafe(1000, $_stack$_safe$_body$_1);
169169
//│ if (res1 instanceof runtime.EffectSig.class) {
170-
//│ runtime.topLevelEffect(res1, false)
170+
//│ res1 = runtime.topLevelEffect(res1, false);
171171
//│ }
172172
//│ res1
173173
//│ = 50005000

0 commit comments

Comments
 (0)