Skip to content

Commit 788b031

Browse files
committed
Handle while loops as expressions in ANF transform.
Append a `()`, as we do for `Unit` returning `if`-s and `try-s` We don't currently support `await` in try/catch, otherwise I'd write tests for that case, too.
1 parent 6618395 commit 788b031

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/main/scala/scala/async/internal/AnfTransform.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private[async] trait AnfTransform {
6868

6969
def _transformToList(tree: Tree): List[Tree] = trace(tree) {
7070
val stats :+ expr = anf.transformToList(tree)
71+
def statsExprUnit =
72+
stats :+ expr :+ localTyper.typedPos(expr.pos)(Literal(Constant(())))
7173
expr match {
7274
case Apply(fun, args) if isAwait(fun) =>
7375
val valDef = defineVal(name.await, expr, tree.pos)
@@ -77,7 +79,7 @@ private[async] trait AnfTransform {
7779
// if type of if-else is Unit don't introduce assignment,
7880
// but add Unit value to bring it into form expected by async transform
7981
if (expr.tpe =:= definitions.UnitTpe) {
80-
stats :+ expr :+ localTyper.typedPos(expr.pos)(Literal(Constant(())))
82+
statsExprUnit
8183
} else {
8284
val varDef = defineVar(name.ifRes, expr.tpe, tree.pos)
8385
def branchWithAssign(orig: Tree) = localTyper.typedPos(orig.pos) {
@@ -90,12 +92,14 @@ private[async] trait AnfTransform {
9092
val ifWithAssign = treeCopy.If(tree, cond, branchWithAssign(thenp), branchWithAssign(elsep)).setType(definitions.UnitTpe)
9193
stats :+ varDef :+ ifWithAssign :+ gen.mkAttributedStableRef(varDef.symbol).setType(tree.tpe).setPos(tree.pos)
9294
}
95+
case LabelDef(name, params, rhs) =>
96+
statsExprUnit
9397

9498
case Match(scrut, cases) =>
9599
// if type of match is Unit don't introduce assignment,
96100
// but add Unit value to bring it into form expected by async transform
97101
if (expr.tpe =:= definitions.UnitTpe) {
98-
stats :+ expr :+ localTyper.typedPos(expr.pos)(Literal(Constant(())))
102+
statsExprUnit
99103
}
100104
else {
101105
val varDef = defineVar(name.matchRes, expr.tpe, tree.pos)

src/test/scala/scala/async/run/ifelse0/WhileSpec.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,18 @@ class WhileSpec {
6262
}
6363
result mustBe (100)
6464
}
65+
66+
@Test
67+
def whileExpr() {
68+
import AsyncId._
69+
70+
val result = async {
71+
var cond = true
72+
while (cond) {
73+
cond = false
74+
await { 22 }
75+
}
76+
}
77+
result mustBe ()
78+
}
6579
}

0 commit comments

Comments
 (0)