Skip to content

Commit 9d74da5

Browse files
committed
Merge pull request scala#114 from retronym/ticket/74
Avoid compiler warning when awaiting Future[Unit]
2 parents 5aa390e + f3f0589 commit 9d74da5

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/main/scala/scala/async/internal/AnfTransform.scala

+11-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ private[async] trait AnfTransform {
5151
expr match {
5252
case Apply(fun, args) if isAwait(fun) =>
5353
val valDef = defineVal(name.await, expr, tree.pos)
54-
stats :+ valDef :+ atPos(tree.pos)(gen.mkAttributedStableRef(valDef.symbol)).setType(tree.tpe)
54+
val ref = gen.mkAttributedStableRef(valDef.symbol).setType(tree.tpe)
55+
val ref1 = if (ref.tpe =:= definitions.UnitTpe)
56+
// https://github.com/scala/async/issues/74
57+
// Use a cast to hide from "pure expression does nothing" error
58+
//
59+
// TODO avoid creating a ValDef for the result of this await to avoid this tree shape altogether.
60+
// This will require some deeper changes to the later parts of the macro which currently assume regular
61+
// tree structure around `await` calls.
62+
gen.mkCast(ref, definitions.UnitTpe)
63+
else ref
64+
stats :+ valDef :+ atPos(tree.pos)(ref1)
5565

5666
case If(cond, thenp, elsep) =>
5767
// if type of if-else is Unit don't introduce assignment,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
3+
*/
4+
5+
package scala.async
6+
package run
7+
8+
import org.junit.Test
9+
10+
import scala.async.internal.AsyncId
11+
import scala.concurrent.Await
12+
import scala.concurrent.duration._
13+
import scala.language.{postfixOps, reflectiveCalls}
14+
15+
16+
class WarningsSpec {
17+
18+
@Test
19+
// https://github.com/scala/async/issues/74
20+
def noPureExpressionInStatementPositionWarning_t74() {
21+
val tb = mkToolbox(s"-cp ${toolboxClasspath} -Xfatal-warnings")
22+
// was: "a pure expression does nothing in statement position; you may be omitting necessary parentheses"
23+
tb.eval(tb.parse {
24+
"""
25+
| import scala.async.internal.AsyncId._
26+
| async {
27+
| if ("".isEmpty) {
28+
| await(println("hello"))
29+
| ()
30+
| } else 42
31+
| }
32+
""".stripMargin
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)