Skip to content

Avoid assigning null to vars of derived value type #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2014

Conversation

retronym
Copy link
Member

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)})

`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.
@retronym
Copy link
Member Author

Review by @phaller

@@ -150,7 +150,6 @@ private[async] trait AnfTransform {

private def defineVal(prefix: String, lhs: Tree, pos: Position): ValDef = {
val sym = currOwner.newTermSymbol(name.fresh(prefix), pos, SYNTHETIC).setInfo(uncheckedBounds(lhs.tpe))
changeOwner(lhs, currentOwner, sym)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

@phaller
Copy link
Contributor

phaller commented Sep 30, 2014

LGTM

retronym added a commit that referenced this pull request Sep 30, 2014
Avoid assigning null to vars of derived value type
@retronym retronym merged commit 61b4c18 into scala:2.10.x Sep 30, 2014
@retronym retronym added this to the 0.9.3 milestone Sep 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants