Skip to content

Commit e50cb7e

Browse files
authored
Add missing BlockTraverser case and fix possible mishandling of Value.Rcd (hkust-taco#287)
1 parent 5664756 commit e50cb7e

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ class BlockTransformer(subst: SymbolSubst):
135135
case Value.Rcd(fields) =>
136136
val fields2 = fields.mapConserve:
137137
case arg @ RcdArg(idx, v) =>
138+
val idx2 = idx.mapConserve(applyPath)
138139
val v2 = applyPath(v)
139-
if v2 is v then arg else RcdArg(idx, v2)
140+
if (idx2 is idx) && (v2 is v) then arg else RcdArg(idx2, v2)
140141
if fields2 is fields then v else Value.Rcd(fields2)
141142

142143
def applyLocal(sym: Local): Local = sym.subst

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ class BlockTraverser(subst: SymbolSubst):
2525
case HandleBlockReturn(res) => applyResult(res)
2626
case Match(scrut, arms, dflt, rst) =>
2727
val scrut2 = applyPath(scrut)
28-
arms.map: arm =>
28+
arms.foreach: arm =>
2929
applyCase(arm._1); applySubBlock(arm._2)
30-
dflt.map(applySubBlock)
30+
dflt.foreach(applySubBlock)
3131
applySubBlock(rst)
3232
case Label(lbl, bod, rst) => applyLocal(lbl); applySubBlock(bod); applySubBlock(rst)
3333
case Begin(sub, rst) => applySubBlock(sub); applySubBlock(rst)
3434
case TryBlock(sub, fin, rst) => applySubBlock(sub); applySubBlock(fin); applySubBlock(rst)
3535
case Assign(l, r, rst) => applyLocal(l); applyResult(r); applySubBlock(rst)
3636
case b @ AssignField(l, n, r, rst) =>
37-
applyPath(l); applyResult(r); applySubBlock(rst); b.symbol.map(_.subst)
37+
applyPath(l); applyResult(r); applySubBlock(rst); b.symbol.foreach(_.subst)
3838
case Define(defn, rst) => applyDefn(defn); applySubBlock(rst)
3939
case HandleBlock(l, res, par, args, cls, hdr, bod, rst) =>
4040
applyLocal(l)
4141
applyLocal(res)
4242
applyPath(par)
43-
args.map(applyPath)
44-
hdr.map(applyHandler)
43+
args.foreach(applyPath)
44+
hdr.foreach(applyHandler)
4545
applySubBlock(bod)
4646
applySubBlock(rst)
4747
case AssignDynField(lhs, fld, arrayIdx, rhs, rest) =>
@@ -51,55 +51,57 @@ class BlockTraverser(subst: SymbolSubst):
5151
applySubBlock(rest)
5252

5353
def applyResult(r: Result): Unit = r match
54-
case r @ Call(fun, args) => applyPath(fun); args.map(applyArg)
55-
case Instantiate(cls, args) =>; applyPath(cls); args.map(applyPath)
54+
case r @ Call(fun, args) => applyPath(fun); args.foreach(applyArg)
55+
case Instantiate(cls, args) =>; applyPath(cls); args.foreach(applyPath)
5656
case p: Path => applyPath(p)
5757

5858
def applyPath(p: Path): Unit = p match
5959
case DynSelect(qual, fld, arrayIdx) =>
6060
applyPath(qual); applyPath(fld)
6161
case p @ Select(qual, name) =>
62-
applyPath(qual); p.symbol.map(_.subst)
62+
applyPath(qual); p.symbol.foreach(_.subst)
6363
case v: Value => applyValue(v)
6464

6565
def applyValue(v: Value): Unit = v match
6666
case Value.Ref(l) => l.subst
6767
case Value.This(sym) => sym.subst
6868
case Value.Lit(lit) => ()
6969
case v @ Value.Lam(params, body) => applyLam(v)
70-
case Value.Arr(elems) => elems.map(applyArg)
70+
case Value.Arr(elems) => elems.foreach(applyArg)
71+
case Value.Rcd(fields) => fields.foreach:
72+
case RcdArg(idx, value) => idx.foreach(applyPath); applyPath(value)
7173

7274
def applyLocal(sym: Local): Unit = sym.subst
7375

7476
def applyFunDefn(fun: FunDefn): Unit =
75-
fun.owner.map(_.subst)
77+
fun.owner.foreach(_.subst)
7678
fun.sym.subst
77-
fun.params.map(applyParamList)
79+
fun.params.foreach(applyParamList)
7880
applySubBlock(fun.body)
7981

8082
def applyDefn(defn: Defn): Unit = defn match
8183
case defn: FunDefn => applyFunDefn(defn)
82-
case ValDefn(owner, k, sym, rhs) => owner.map(_.subst); sym.subst; applyPath(rhs)
84+
case ValDefn(owner, k, sym, rhs) => owner.foreach(_.subst); sym.subst; applyPath(rhs)
8385
case ClsLikeDefn(own, isym, sym, k, paramsOpt, auxParams, parentPath, methods,
8486
privateFields, publicFields, preCtor, ctor) =>
85-
own.map(_.subst)
87+
own.foreach(_.subst)
8688
isym.subst
8789
sym.subst
88-
paramsOpt.map(applyParamList)
89-
auxParams.map(applyParamList)
90-
parentPath.map(applyPath)
91-
methods.map(applyFunDefn)
92-
privateFields.map(_.subst)
93-
publicFields.map(applyTermDefinition)
90+
paramsOpt.foreach(applyParamList)
91+
auxParams.foreach(applyParamList)
92+
parentPath.foreach(applyPath)
93+
methods.foreach(applyFunDefn)
94+
privateFields.foreach(_.subst)
95+
publicFields.foreach(applyTermDefinition)
9496
applySubBlock(preCtor)
9597
applySubBlock(ctor)
9698

9799
def applyArg(arg: Arg): Unit =
98100
applyPath(arg.value)
99101

100102
def applyParamList(pl: ParamList): Unit =
101-
pl.params.map(_.sym.subst)
102-
pl.restParam.map(_.sym.subst)
103+
pl.params.foreach(_.sym.subst)
104+
pl.restParam.foreach(_.sym.subst)
103105

104106
def applyCase(cse: Case): Unit = cse match
105107
case Case.Lit(lit) => ()
@@ -111,17 +113,17 @@ class BlockTraverser(subst: SymbolSubst):
111113
def applyHandler(hdr: Handler): Unit =
112114
hdr.sym.subst
113115
hdr.resumeSym.subst
114-
hdr.params.map(applyParamList)
116+
hdr.params.foreach(applyParamList)
115117
applySubBlock(hdr.body)
116118

117119
def applyLam(lam: Value.Lam): Unit =
118120
applyParamList(lam.params)
119121
applySubBlock(lam.body)
120122

121123
def applyTermDefinition(td: TermDefinition): Unit =
122-
td.owner.map(_.subst)
124+
td.owner.foreach(_.subst)
123125
td.sym.subst
124-
td.params.map(applyParamList)
126+
td.params.foreach(applyParamList)
125127
td.resSym.subst
126128

127129
class BlockTraverserShallow(subst: SymbolSubst) extends BlockTraverser(subst):
@@ -138,11 +140,11 @@ class BlockTraverserShallow(subst: SymbolSubst) extends BlockTraverser(subst):
138140
applyLocal(l)
139141
applyLocal(res)
140142
applyPath(par)
141-
args.map(applyPath)
143+
args.foreach(applyPath)
142144
cls.subst
143-
hdr.map(applyHandler)
145+
hdr.foreach(applyHandler)
144146
applySubBlock(rst)
145147
case _ => super.applyBlock(b)
146148

147-
class BlockDataTraverse(subst: SymbolSubst) extends BlockTraverserShallow(subst):
149+
class BlockDataTraverser(subst: SymbolSubst) extends BlockTraverserShallow(subst):
148150
override def applySubBlock(b: Block): Unit = ()

0 commit comments

Comments
 (0)