From 2393ea506eb61a750a5b34323c4249421104d8ed Mon Sep 17 00:00:00 2001 From: Anson Yeung Date: Wed, 8 Jan 2025 20:48:57 +0800 Subject: [PATCH] Remove usage of dummy class symbol --- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 4 ++-- .../scala/hkmc2/codegen/HandlerLowering.scala | 17 ++++++++--------- .../src/main/scala/hkmc2/codegen/Lowering.scala | 2 +- .../shared/src/test/mlscript/decls/Prelude.mls | 2 ++ .../shared/src/test/mlscript/parser/Handler.mls | 6 +++--- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 5fa130c5e..9bf0e6384 100644 --- a/hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -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) @@ -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) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index 78088c7a6..427354663 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -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: @@ -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() @@ -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) @@ -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) @@ -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) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 0ba418b06..eff0842f0 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -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) diff --git a/hkmc2/shared/src/test/mlscript/decls/Prelude.mls b/hkmc2/shared/src/test/mlscript/decls/Prelude.mls index 10f50b438..ae9814103 100644 --- a/hkmc2/shared/src/test/mlscript/decls/Prelude.mls +++ b/hkmc2/shared/src/test/mlscript/decls/Prelude.mls @@ -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 diff --git a/hkmc2/shared/src/test/mlscript/parser/Handler.mls b/hkmc2/shared/src/test/mlscript/parser/Handler.mls index 10fc272ae..cf111a328 100644 --- a/hkmc2/shared/src/test/mlscript/parser/Handler.mls +++ b/hkmc2/shared/src/test/mlscript/parser/Handler.mls @@ -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 ( @@ -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 @@ -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) } }