diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2014-09-25 18:49:51 -0400 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2014-09-29 16:44:14 +1000 |
commit | b420446c9f82176e7fe4973576fb2d7f22b28893 (patch) | |
tree | 8b77368b85b7c00c51697033189ef64d2f2d6e16 /src/test | |
parent | 51579303e83fb134bba3ed7279f92da90103c4f2 (diff) | |
download | scala-async-b420446c9f82176e7fe4973576fb2d7f22b28893.tar.gz scala-async-b420446c9f82176e7fe4973576fb2d7f22b28893.tar.bz2 scala-async-b420446c9f82176e7fe4973576fb2d7f22b28893.zip |
Avoid assigning null to vars of derived value type
`TreeGen#mkZero` returns `q"null"` for derived value classes.
```
scala> class V(val a: String) extends AnyVal
defined class V
scala> showRaw(gen.mkZero(typeOf[V]))
res0: String = Literal(Constant(null))
```
We use this API in async to generate the initial value for
ANF-lifted temporary variables.
However, this leads to NPEs, as after posterasure, we call the
unbox method on a null reference:
```
% cat sandbox/Macro.scala; scalac-hash v2.10.4 sandbox/Macro.scala; scala-hash v2.10.4 -e 'val x = Macros.myMacro'
import scala.reflect.macros.Context
import scala.language.experimental.macros
object Macros {
def macroImpl(c: Context): c.Expr[C] = {
import c.universe._
val e1 = c.Expr[C](Literal(Constant(null)).setType(typeOf[C]))
reify(e1.splice.asInstanceOf[C @annotation.unchecked.uncheckedVariance])
}
def myMacro: C = macro macroImpl
}
class C(val a: String) extends AnyVal
java.lang.NullPointerException
at Main$$anon$1.<init>(scalacmd4059893593754060829.scala:1)
at Main$.main(scalacmd4059893593754060829.scala:1)
at Main.main(scalacmd4059893593754060829.scala)
```
This commit installs a custom version of `mkZero` that instead
returns `q"new C[$..targs](${mkZero(wrappedType)})`.
Thanks to @ewiner for pinpointing the problem.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/scala/scala/async/run/toughtype/ToughType.scala | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/test/scala/scala/async/run/toughtype/ToughType.scala b/src/test/scala/scala/async/run/toughtype/ToughType.scala index 54a53c8..0f56ae0 100644 --- a/src/test/scala/scala/async/run/toughtype/ToughType.scala +++ b/src/test/scala/scala/async/run/toughtype/ToughType.scala @@ -228,8 +228,72 @@ class ToughTypeSpec { case `e` => } } + + @Test def ticket83ValueClass() { + import scala.async.Async._ + import scala.concurrent._, duration._, ExecutionContext.Implicits.global + val f = async { + val uid = new IntWrapper("foo") + await(Future(uid)) + } + val result = Await.result(f, 5.seconds) + result mustEqual (new IntWrapper("foo")) + } + + @Test def ticket86NestedValueClass() { + import ExecutionContext.Implicits.global + + val f = async { + val a = Future.successful(new IntWrapper("42")) + await(await(a).plusStr) + } + val result = Await.result(f, 5.seconds) + result mustEqual "42!" + } + + @Test def ticket86MatchedValueClass(): Unit = { + import ExecutionContext.Implicits.global + + def doAThing(param: IntWrapper) = Future(None) + + val fut = async { + Option(new IntWrapper("value!")) match { + case Some(valueHolder) => + await(doAThing(valueHolder)) + case None => + None + } + } + + val result = Await.result(fut, 5.seconds) + result mustBe None + } + + @Test def ticket86MatchedParameterizedValueClass(): Unit = { + import ExecutionContext.Implicits.global + + def doAThing(param: ParamWrapper[String]) = Future(None) + + val fut = async { + Option(new ParamWrapper("value!")) match { + case Some(valueHolder) => + await(doAThing(valueHolder)) + case None => + None + } + } + + val result = Await.result(fut, 5.seconds) + result mustBe None + } } +class IntWrapper(val value: String) extends AnyVal { + def plusStr = Future.successful(value + "!") +} +class ParamWrapper[T](val value: T) extends AnyVal + + trait A trait B |