Skip to content

Commit

Permalink
Make async blackbox / throw a meaningful exception from await
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Jun 30, 2020
1 parent f600550 commit 4dc06cd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/main/scala/scala/async/Async.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package scala.async
import scala.language.experimental.macros
import scala.concurrent.{ExecutionContext, Future}
import scala.annotation.compileTimeOnly
import scala.reflect.macros.whitebox
import scala.reflect.macros.blackbox

/**
* Async blocks provide a direct means to work with [[scala.concurrent.Future]].
Expand Down Expand Up @@ -60,11 +60,9 @@ object Async {
* in the `onComplete` handler of `awaitable`, and will *not* block a thread.
*/
@compileTimeOnly("[async] `await` must be enclosed in an `async` block")
def await[T](awaitable: Future[T]): T = ??? // No implementation here, as calls to this are translated to `onComplete` by the macro.
def await[T](awaitable: Future[T]): T = throw new UnsupportedOperationException("Calls to Async.await should have been treated as compiler intrinsics and rewritten!")

def asyncImpl[T: c.WeakTypeTag](c: whitebox.Context)
(body: c.Tree)
(execContext: c.Tree): c.Tree = {
def asyncImpl[T: c.WeakTypeTag](c: blackbox.Context)(body: c.Tree)(execContext: c.Tree): c.Tree = {
import c.universe._
if (!c.compilerSettings.contains("-Xasync")) {
c.abort(c.macroApplication.pos, "The async requires the compiler option -Xasync (supported only by Scala 2.12.12+ / 2.13.3+)")
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/scala/async/FutureSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class FutureSpec {
val a = await(future0.mapTo[Int])
val b = await((Future { (a * 2).toString }).mapTo[Int])
val c = await(Future { (7 * 2).toString })
b + "-" + c
b.toString + "-" + c
}

Await.result(future1, defaultTimeout) mustBe ("10-14")
Expand All @@ -129,7 +129,7 @@ class FutureSpec {
Res(a: Int) <- asyncReq(Req("Hello"))
Res(b: Int) <- asyncReq(Req(a))
Res(c: Int) <- asyncReq(Req(7))
} yield b + "-" + c
} yield b.toString + "-" + c

Await.result(future1, defaultTimeout) mustBe ("10-14")
intercept[NoSuchElementException] { Await.result(future2, defaultTimeout) }
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/scala/async/TestUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object TestUtil {
}

implicit class objectops(obj: Any) {
def mustBe(other: Any): Unit = assert(obj == other, obj + " is not " + other)
def mustBe(other: Any): Unit = assert(obj == other, obj.toString + " is not " + other)

def mustEqual(other: Any): Unit = mustBe(other)
}
Expand Down

0 comments on commit 4dc06cd

Please sign in to comment.