Skip to content

Commit c770346

Browse files
committed
Merge pull request #127 from retronym/backport/20150727
Backports for two recently fixed bugs.
2 parents ad6c3c0 + d74a615 commit c770346

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Diff for: src/main/scala/scala/async/internal/AnfTransform.scala

+9-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ private[async] trait AnfTransform {
7070
val stats :+ expr = anf.transformToList(tree)
7171
def statsExprUnit =
7272
stats :+ expr :+ localTyper.typedPos(expr.pos)(Literal(Constant(())))
73+
def statsExprThrow =
74+
stats :+ expr :+ localTyper.typedPos(expr.pos)(Throw(Apply(Select(New(gen.mkAttributedRef(defn.IllegalStateExceptionClass)), nme.CONSTRUCTOR), Nil)))
75+
7376
expr match {
7477
case Apply(fun, args) if isAwait(fun) =>
7578
val valDef = defineVal(name.await, expr, tree.pos)
@@ -81,7 +84,7 @@ private[async] trait AnfTransform {
8184
// TODO avoid creating a ValDef for the result of this await to avoid this tree shape altogether.
8285
// This will require some deeper changes to the later parts of the macro which currently assume regular
8386
// tree structure around `await` calls.
84-
gen.mkCast(ref, definitions.UnitTpe)
87+
localTyper.typedPos(tree.pos)(gen.mkCast(ref, definitions.UnitTpe))
8588
else ref
8689
stats :+ valDef :+ atPos(tree.pos)(ref1)
8790

@@ -90,6 +93,8 @@ private[async] trait AnfTransform {
9093
// but add Unit value to bring it into form expected by async transform
9194
if (expr.tpe =:= definitions.UnitTpe) {
9295
statsExprUnit
96+
} else if (expr.tpe =:= definitions.NothingTpe) {
97+
statsExprThrow
9398
} else {
9499
val varDef = defineVar(name.ifRes, expr.tpe, tree.pos)
95100
def branchWithAssign(orig: Tree) = localTyper.typedPos(orig.pos) {
@@ -110,8 +115,9 @@ private[async] trait AnfTransform {
110115
// but add Unit value to bring it into form expected by async transform
111116
if (expr.tpe =:= definitions.UnitTpe) {
112117
statsExprUnit
113-
}
114-
else {
118+
} else if (expr.tpe =:= definitions.NothingTpe) {
119+
statsExprThrow
120+
} else {
115121
val varDef = defineVar(name.matchRes, expr.tpe, tree.pos)
116122
def typedAssign(lhs: Tree) =
117123
localTyper.typedPos(lhs.pos)(Assign(Ident(varDef.symbol), mkAttributedCastPreservingAnnotations(lhs, varDef.symbol.tpe)))

Diff for: src/main/scala/scala/async/internal/TransformUtils.scala

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ private[async] trait TransformUtils {
8383

8484
val NonFatalClass = rootMirror.staticModule("scala.util.control.NonFatal")
8585
val Async_await = asyncBase.awaitMethod(global)(macroApplication.symbol).ensuring(_ != NoSymbol)
86+
val IllegalStateExceptionClass = rootMirror.staticClass("java.lang.IllegalStateException")
87+
8688
}
8789

8890
def isSafeToInline(tree: Tree) = {

Diff for: src/test/scala/scala/async/run/anf/AnfTransformSpec.scala

+42
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,46 @@ class AnfTransformSpec {
405405
val applyImplicitView = tree.collect { case x if x.getClass.getName.endsWith("ApplyImplicitView") => x }
406406
applyImplicitView.map(_.toString) mustBe List("view(a$1)")
407407
}
408+
409+
@Test
410+
def nothingTypedIf(): Unit = {
411+
import scala.async.internal.AsyncId.{async, await}
412+
val result = util.Try(async {
413+
if (true) {
414+
val n = await(1)
415+
if (n < 2) {
416+
throw new RuntimeException("case a")
417+
}
418+
else {
419+
throw new RuntimeException("case b")
420+
}
421+
}
422+
else {
423+
"case c"
424+
}
425+
})
426+
427+
assert(result.asInstanceOf[util.Failure[_]].exception.getMessage == "case a")
428+
}
429+
430+
@Test
431+
def nothingTypedMatch(): Unit = {
432+
import scala.async.internal.AsyncId.{async, await}
433+
val result = util.Try(async {
434+
0 match {
435+
case _ if "".isEmpty =>
436+
val n = await(1)
437+
n match {
438+
case _ if n < 2 =>
439+
throw new RuntimeException("case a")
440+
case _ =>
441+
throw new RuntimeException("case b")
442+
}
443+
case _ =>
444+
"case c"
445+
}
446+
})
447+
448+
assert(result.asInstanceOf[util.Failure[_]].exception.getMessage == "case a")
449+
}
408450
}

0 commit comments

Comments
 (0)