aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2015-07-07 09:59:24 +1000
committerJason Zaugg <jzaugg@gmail.com>2015-07-07 09:59:24 +1000
commit30cc2c51036bb0d1bc3c53ff600bfbe62a8be542 (patch)
tree47878152146195ddebfec389877dfcef09bf8c23
parentd7914c3879cbe832b640c07a24ee6941fa545928 (diff)
parente1377aa06ffe817d15315bbce3a6a636f2dd1f0e (diff)
downloadscala-async-30cc2c51036bb0d1bc3c53ff600bfbe62a8be542.tar.gz
scala-async-30cc2c51036bb0d1bc3c53ff600bfbe62a8be542.tar.bz2
scala-async-30cc2c51036bb0d1bc3c53ff600bfbe62a8be542.zip
Merge pull request #115 from retronym/backport/74v0.9.4_2.10
Avoid compiler warning when awaiting Future[Unit]
-rw-r--r--src/main/scala/scala/async/internal/AnfTransform.scala12
-rw-r--r--src/test/scala/scala/async/run/WarningsSpec.scala35
2 files changed, 46 insertions, 1 deletions
diff --git a/src/main/scala/scala/async/internal/AnfTransform.scala b/src/main/scala/scala/async/internal/AnfTransform.scala
index bf66fde..3c8c837 100644
--- a/src/main/scala/scala/async/internal/AnfTransform.scala
+++ b/src/main/scala/scala/async/internal/AnfTransform.scala
@@ -73,7 +73,17 @@ private[async] trait AnfTransform {
expr match {
case Apply(fun, args) if isAwait(fun) =>
val valDef = defineVal(name.await, expr, tree.pos)
- stats :+ valDef :+ atPos(tree.pos)(gen.mkAttributedStableRef(valDef.symbol)).setType(tree.tpe)
+ val ref = gen.mkAttributedStableRef(valDef.symbol).setType(tree.tpe)
+ val ref1 = if (ref.tpe =:= definitions.UnitTpe)
+ // https://github.com/scala/async/issues/74
+ // Use a cast to hide from "pure expression does nothing" error
+ //
+ // 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)
+ else ref
+ stats :+ valDef :+ atPos(tree.pos)(ref1)
case If(cond, thenp, elsep) =>
// if type of if-else is Unit don't introduce assignment,
diff --git a/src/test/scala/scala/async/run/WarningsSpec.scala b/src/test/scala/scala/async/run/WarningsSpec.scala
new file mode 100644
index 0000000..3a7843a
--- /dev/null
+++ b/src/test/scala/scala/async/run/WarningsSpec.scala
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
+ */
+
+package scala.async
+package run
+
+import org.junit.Test
+
+import scala.async.internal.AsyncId
+import scala.concurrent.Await
+import scala.concurrent.duration._
+import scala.language.{postfixOps, reflectiveCalls}
+
+
+class WarningsSpec {
+
+ @Test
+ // https://github.com/scala/async/issues/74
+ def noPureExpressionInStatementPositionWarning_t74() {
+ val tb = mkToolbox(s"-cp ${toolboxClasspath} -Xfatal-warnings")
+ // was: "a pure expression does nothing in statement position; you may be omitting necessary parentheses"
+ tb.eval(tb.parse {
+ """
+ | import scala.async.internal.AsyncId._
+ | async {
+ | if ("".isEmpty) {
+ | await(println("hello"))
+ | ()
+ | } else 42
+ | }
+ """.stripMargin
+ })
+ }
+}