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 1 commit
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
Prev Previous commit
Avoid masking user exception with ??? for Nothing typed expressions
Code like:

    val x = if (cond) throw new A else throw new B

Was being transformed to:

    val ifRes = ???
    if (cond) ifRes = throw new A else ifRes = throw new B
    val x = ifRes

by way of the use of `gen.mkZero` which throws `???` if the requested type is `Nothing`

This commit special cases `Nothing` typed expressions in a similar manner to `Unit` type expressions.
The example above is now translated to:

    if (cond) throw new A else throw new B
    val x = throw new IllegalStateException()

Fixes #120

(cherry picked from commit 80aaf18)
  • Loading branch information
retronym committed Jul 27, 2015
commit d74a615334de70c13bd9be00b03b4010de980b0b
10 changes: 8 additions & 2 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 @@ -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")
}
}