From b420446c9f82176e7fe4973576fb2d7f22b28893 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Thu, 25 Sep 2014 18:49:51 -0400 Subject: 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.(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. --- .../scala/async/run/toughtype/ToughType.scala | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'src/test') 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 -- cgit v1.2.3