Skip to content

Commit

Permalink
Remove usage of dummy class symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
AnsonYeung committed Jan 8, 2025
1 parent b8c6120 commit 2393ea5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
showingJSYieldedCompileError = true
outerRaise(d)
case d => outerRaise(d)
given Elaborator.Ctx = curCtx
val low = ltl.givenIn:
new codegen.Lowering
with codegen.LoweringSelSanityChecks(instrument = false)
with codegen.LoweringTraceLog(instrument = false)
with codegen.LoweringHandler(handler.isSet)
given Elaborator.Ctx = curCtx
val jsb = new JSBuilder
with JSBuilderArgNumSanityChecks(instrument = false)
val le = low.program(blk)
Expand All @@ -76,12 +76,12 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
output(s"JS (unsanitized):")
output(jsStr)
if js.isSet && !showingJSYieldedCompileError then
given Elaborator.Ctx = curCtx
val low = ltl.givenIn:
new codegen.Lowering
with codegen.LoweringSelSanityChecks(noSanityCheck.isUnset)
with codegen.LoweringTraceLog(traceJS.isSet)
with codegen.LoweringHandler(handler.isSet)
given Elaborator.Ctx = curCtx
val jsb = new JSBuilder
with JSBuilderArgNumSanityChecks(noSanityCheck.isUnset)
val le = low.program(blk)
Expand Down
17 changes: 8 additions & 9 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import semantics.Elaborator.State
import syntax.{Literal, Tree, ParamBind}
import semantics.*
import scala.annotation.tailrec
import hkmc2.semantics.Elaborator.ctx

object HandlerLowering:

Expand Down Expand Up @@ -62,7 +63,7 @@ object HandlerLowering:

import HandlerLowering.*

class HandlerLowering(using TL, Raise, Elaborator.State):
class HandlerLowering(using TL, Raise, Elaborator.State, Elaborator.Ctx):

private val functionHandlerCtx = HandlerCtx(true, false, state =>
val tmp = freshTmp()
Expand All @@ -73,16 +74,14 @@ class HandlerLowering(using TL, Raise, Elaborator.State):
)
private def handlerCtx(using HandlerCtx): HandlerCtx = summon
private val effectSigPath: Path = State.globalThisSymbol.asPath.selN(Tree.Ident("Predef")).selN(Tree.Ident("__EffectSig")).selN(Tree.Ident("class"))
private val effectSigSym: ClassSymbol = ctx.Builtins.Predef.tree.definedSymbols.get("__EffectSig").get.asCls.get
private val contClsPath: Path = State.globalThisSymbol.asPath.selN(Tree.Ident("Predef")).selN(Tree.Ident("__Cont")).selN(Tree.Ident("class"))
private val retClsPath: Path = State.globalThisSymbol.asPath.selN(Tree.Ident("Predef")).selN(Tree.Ident("__Return")).selN(Tree.Ident("class"))
private val retClsSym: ClassSymbol = ctx.Builtins.Predef.tree.definedSymbols.get("__Return").get.asCls.get
private val handleEffectFun: Path = State.globalThisSymbol.asPath.selN(Tree.Ident("Predef")).selN(Tree.Ident("__handleEffect"))
private val mkEffectPath: Path = State.globalThisSymbol.asPath.selN(Tree.Ident("Predef")).selN(Tree.Ident("__mkEffect"))
private val handleBlockImplPath: Path = State.globalThisSymbol.asPath.selN(Tree.Ident("Predef")).selN(Tree.Ident("__handleBlockImpl"))
private val mapPath: Path = State.globalThisSymbol.asPath.selN(Tree.Ident("Map"))
private val dummyClsSym = ClassSymbol(
Tree.TypeDef(syntax.Cls, Tree.Error(), N, N),
Tree.Ident("Dummy")
)

private def freshTmp(dbgNme: Str = "tmp") = new TempSymbol(N, dbgNme)

Expand Down Expand Up @@ -453,13 +452,13 @@ class HandlerLowering(using TL, Raise, Elaborator.State):
.assign(res, c)
.ifthen(
res.asPath,
Case.Cls(dummyClsSym, effectSigPath),
Case.Cls(effectSigSym, effectSigPath),
ReturnCont(res, uid)
)
.chain(ResumptionPoint(res, uid, _))
.staticif(canRet, _.ifthen(
res.asPath,
Case.Cls(dummyClsSym, retClsPath),
Case.Cls(retClsSym, retClsPath),
blockBuilder.ret(if handlerCtx.isHandleFree then res.asPath.value else res.asPath)
))
.rest(rest)
Expand Down Expand Up @@ -537,12 +536,12 @@ class HandlerLowering(using TL, Raise, Elaborator.State):
.assign(res, c)
.ifthen(
res.asPath,
Case.Cls(dummyClsSym, effectSigPath),
Case.Cls(effectSigSym, effectSigPath),
handlerCtx.linkAndHandle(LinkState(res.asPath, clsSym.asPath, uid))
)
.staticif(canRet && !handlerCtx.isTopLevel, _.ifthen(
res.asPath,
Case.Cls(dummyClsSym, retClsPath),
Case.Cls(retClsSym, retClsPath),
blockBuilder.ret(if handlerCtx.isHandleFree then res.asPath.value else res.asPath)
))
.rest(rest)
Expand Down
2 changes: 1 addition & 1 deletion hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ trait LoweringTraceLog


trait LoweringHandler
(instrument: Bool)(using TL, Raise, Elaborator.State)
(instrument: Bool)(using TL, Raise, Elaborator.State, Elaborator.Ctx)
extends Lowering:
override def term(t: st)(k: Result => Block)(using Subst): Block =
if !instrument then return super.term(t)(k)
Expand Down
2 changes: 2 additions & 0 deletions hkmc2/shared/src/test/mlscript/decls/Prelude.mls
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ declare module Predef with
val x = 0
class MatchResult(captures)
class MatchFailure(errors)
class __EffectSig(next, nextHandler, tail, tailHandler, resumed, handler, handlerFun)
class __Return(value)


// TODO: rm
Expand Down
6 changes: 3 additions & 3 deletions hkmc2/shared/src/test/mlscript/parser/Handler.mls
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ handle h = Eff with
fun f()(r) = r(0)
in
foo(h)
//│ Elab: { { handle h = SynthSel(Ref(globalThis:block#5),Ident(Eff)) List(HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$133),Fun,member:f,List(ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(0)),None))))),‹result of member:f›,‹›))); { globalThis:block#5#666(.)foo‹member:foo›(h#666) } } }
//│ Elab: { { handle h = SynthSel(Ref(globalThis:block#5),Ident(Eff)) List(HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$145),Fun,member:f,List(ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(0)),None))))),‹result of member:f›,‹›))); { globalThis:block#5#666(.)foo‹member:foo›(h#666) } } }

:e
(
Expand All @@ -73,7 +73,7 @@ handle h = Eff with
fun f()(r) = r(0)
fun g(a)()()(r) = r(1)
foo(h)
//│ Elab: { handle h = SynthSel(Ref(globalThis:block#5),Ident(Eff)) List(HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$162),Fun,member:f,List(ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(0)),None))))),‹result of member:f›,‹›)), HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$162),Fun,member:g,List(ParamList(‹›,List(Param(‹›,a,None)),None), ParamList(‹›,List(),None), ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(1)),None))))),‹result of member:g›,‹›))); { globalThis:block#5#666(.)foo‹member:foo›(h#666) } }
//│ Elab: { handle h = SynthSel(Ref(globalThis:block#5),Ident(Eff)) List(HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$174),Fun,member:f,List(ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(0)),None))))),‹result of member:f›,‹›)), HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$174),Fun,member:g,List(ParamList(‹›,List(Param(‹›,a,None)),None), ParamList(‹›,List(),None), ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(1)),None))))),‹result of member:g›,‹›))); { globalThis:block#5#666(.)foo‹member:foo›(h#666) } }

:e
handle h = Eff with
Expand Down Expand Up @@ -127,4 +127,4 @@ foo(h)
//│ ╔══[WARNING] Terms in handler block do nothing
//│ ║ l.125: 12345
//│ ╙── ^^^^^
//│ Elab: { handle h = SynthSel(Ref(globalThis:block#5),Ident(Eff)) List(HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$239),Fun,member:f,List(ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(0)),None))))),‹result of member:f›,‹›)), HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$239),Fun,member:g,List(ParamList(‹›,List(Param(‹›,a,None)),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(1)),None))))),‹result of member:g›,‹›))); { globalThis:block#5#666(.)foo‹member:foo›(h#666) } }
//│ Elab: { handle h = SynthSel(Ref(globalThis:block#5),Ident(Eff)) List(HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$251),Fun,member:f,List(ParamList(‹›,List(),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(0)),None))))),‹result of member:f›,‹›)), HandlerTermDefinition(r,TermDefinition(Some(class:Effect$Eff$251),Fun,member:g,List(ParamList(‹›,List(Param(‹›,a,None)),None)),None,Some(App(Ref(r),Tup(List(Fld(‹›,Lit(IntLit(1)),None))))),‹result of member:g›,‹›))); { globalThis:block#5#666(.)foo‹member:foo›(h#666) } }

0 comments on commit 2393ea5

Please sign in to comment.