Skip to content

Commit 88af08e

Browse files
committed
Avoid dead code warning with async(throw T)
By declararing the parameter of `async` as by-name. Fixes #150 (the bug in the original ticket.) (cherry picked from commit 4b1dbee)
1 parent 2e8dbbf commit 88af08e

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/main/scala/scala/async/Async.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object Async {
4242
* Run the block of code `body` asynchronously. `body` may contain calls to `await` when the results of
4343
* a `Future` are needed; this is translated into non-blocking code.
4444
*/
45-
def async[T](body: T)(implicit execContext: ExecutionContext): Future[T] = macro internal.ScalaConcurrentAsync.asyncImpl[T]
45+
def async[T](body: => T)(implicit execContext: ExecutionContext): Future[T] = macro internal.ScalaConcurrentAsync.asyncImpl[T]
4646

4747
/**
4848
* Non-blocking await the on result of `awaitable`. This may only be used directly within an enclosing `async` block.

src/main/scala/scala/async/internal/AsyncId.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object AsyncId extends AsyncBase {
1313
lazy val futureSystem = IdentityFutureSystem
1414
type FS = IdentityFutureSystem.type
1515

16-
def async[T](body: T) = macro asyncIdImpl[T]
16+
def async[T](body: => T) = macro asyncIdImpl[T]
1717

1818
def asyncIdImpl[T: c.WeakTypeTag](c: Context)(body: c.Expr[T]): c.Expr[T] = asyncImpl[T](c)(body)(c.literalUnit)
1919
}

src/test/scala/scala/async/run/WarningsSpec.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ class WarningsSpec {
3131
})
3232
}
3333

34+
@Test
35+
// https://github.com/scala/async/issues/74
36+
def noDeadCodeWarningForAsyncThrow() {
37+
val global = mkGlobal("-cp ${toolboxClasspath} -Yrangepos -Ywarn-dead-code -Xfatal-warnings")
38+
// was: "a pure expression does nothing in statement position; you may be omitting necessary parentheses"
39+
val source =
40+
"""
41+
| class Test {
42+
| import scala.async.Async._
43+
| import scala.concurrent.ExecutionContext.Implicits.global
44+
| async { throw new Error() }
45+
| }
46+
""".stripMargin
47+
val run = new global.Run
48+
val sourceFile = global.newSourceFile(source)
49+
run.compileSources(sourceFile :: Nil)
50+
assert(!global.reporter.hasErrors, global.reporter.asInstanceOf[StoreReporter].infos)
51+
}
52+
3453
@Test
3554
def noDeadCodeWarning() {
3655
val global = mkGlobal("-cp ${toolboxClasspath} -Yrangepos -Ywarn-dead-code -Xfatal-warnings")

0 commit comments

Comments
 (0)