Skip to content

Avoid masking user exception with ??? for Nothing typed expressions #122

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 1 commit 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
9 changes: 7 additions & 2 deletions src/main/scala/scala/async/internal/AnfTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ private[async] trait AnfTransform {
val stats :+ expr = anf.transformToList(tree)
def statsExprUnit =
stats :+ expr :+ api.typecheck(atPos(expr.pos)(Literal(Constant(()))))
def statsExprThrow =
stats :+ expr :+ api.typecheck(atPos(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 @@ -68,6 +70,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) = api.typecheck(atPos(orig.pos) {
Expand All @@ -88,8 +92,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) =
api.typecheck(atPos(lhs.pos)(Assign(Ident(varDef.symbol), mkAttributedCastPreservingAnnotations(lhs, tpe(varDef.symbol)))))
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/scala/async/internal/TransformUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private[async] trait TransformUtils {

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

// `while(await(x))` ... or `do { await(x); ... } while(...)` contain an `If` that loops;
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) mustStartWith List("view(a$macro$")
}

@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")
}
}