Skip to content

Backports for two recently fixed bugs. #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/main/scala/scala/async/internal/AnfTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ private[async] trait AnfTransform {
val stats :+ expr = anf.transformToList(tree)
def statsExprUnit =
stats :+ expr :+ localTyper.typedPos(expr.pos)(Literal(Constant(())))
def statsExprThrow =
stats :+ expr :+ localTyper.typedPos(expr.pos)(Throw(Apply(Select(New(gen.mkAttributedRef(defn.IllegalStateExceptionClass)), nme.CONSTRUCTOR), Nil)))

expr match {
case Apply(fun, args) if isAwait(fun) =>
val valDef = defineVal(name.await, expr, tree.pos)
Expand All @@ -81,7 +84,7 @@ private[async] trait AnfTransform {
// TODO avoid creating a ValDef for the result of this await to avoid this tree shape altogether.
// This will require some deeper changes to the later parts of the macro which currently assume regular
// tree structure around `await` calls.
gen.mkCast(ref, definitions.UnitTpe)
localTyper.typedPos(tree.pos)(gen.mkCast(ref, definitions.UnitTpe))
else ref
stats :+ valDef :+ atPos(tree.pos)(ref1)

Expand All @@ -90,6 +93,8 @@ private[async] trait AnfTransform {
// but add Unit value to bring it into form expected by async transform
if (expr.tpe =:= definitions.UnitTpe) {
statsExprUnit
} else if (expr.tpe =:= definitions.NothingTpe) {
statsExprThrow
} else {
val varDef = defineVar(name.ifRes, expr.tpe, tree.pos)
def branchWithAssign(orig: Tree) = localTyper.typedPos(orig.pos) {
Expand All @@ -110,8 +115,9 @@ private[async] trait AnfTransform {
// but add Unit value to bring it into form expected by async transform
if (expr.tpe =:= definitions.UnitTpe) {
statsExprUnit
}
else {
} else if (expr.tpe =:= definitions.NothingTpe) {
statsExprThrow
} else {
val varDef = defineVar(name.matchRes, expr.tpe, tree.pos)
def typedAssign(lhs: Tree) =
localTyper.typedPos(lhs.pos)(Assign(Ident(varDef.symbol), mkAttributedCastPreservingAnnotations(lhs, varDef.symbol.tpe)))
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/scala/async/internal/TransformUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ private[async] trait TransformUtils {

val NonFatalClass = rootMirror.staticModule("scala.util.control.NonFatal")
val Async_await = asyncBase.awaitMethod(global)(macroApplication.symbol).ensuring(_ != NoSymbol)
val IllegalStateExceptionClass = rootMirror.staticClass("java.lang.IllegalStateException")

}

def isSafeToInline(tree: Tree) = {
Expand Down
42 changes: 42 additions & 0 deletions src/test/scala/scala/async/run/anf/AnfTransformSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,46 @@ class AnfTransformSpec {
val applyImplicitView = tree.collect { case x if x.getClass.getName.endsWith("ApplyImplicitView") => x }
applyImplicitView.map(_.toString) mustBe List("view(a$1)")
}

@Test
def nothingTypedIf(): Unit = {
import scala.async.internal.AsyncId.{async, await}
val result = util.Try(async {
if (true) {
val n = await(1)
if (n < 2) {
throw new RuntimeException("case a")
}
else {
throw new RuntimeException("case b")
}
}
else {
"case c"
}
})

assert(result.asInstanceOf[util.Failure[_]].exception.getMessage == "case a")
}

@Test
def nothingTypedMatch(): Unit = {
import scala.async.internal.AsyncId.{async, await}
val result = util.Try(async {
0 match {
case _ if "".isEmpty =>
val n = await(1)
n match {
case _ if n < 2 =>
throw new RuntimeException("case a")
case _ =>
throw new RuntimeException("case b")
}
case _ =>
"case c"
}
})

assert(result.asInstanceOf[util.Failure[_]].exception.getMessage == "case a")
}
}