Skip to content

null unboxing is broken because the backend does not intercept the unbox calls #582

Closed
@smarter

Description

@smarter

This code:

object Test {
  def main(args: Array[String]): Unit = {
    val d: Double = null.asInstanceOf[Double]
    println(d)
  }
}

Is transformed both in scalac and dotty into:

def main(args: String[]): Unit = {
  val d: Double = scala.Double.unbox(null)
  println(scala.Double.box(d))
}

But this is just an illusion: in scalac, the unbox call is actually replaced by BoxesRunTime.unboxToDouble(null). Dotty does not do this replacement and so the call fails with a NullPointerException.

(I still wonder why there even is an implementation in Double.scala since it's both unused and incorrect: https://github.com/scala/scala/blob/2.11.x/src/library/scala/Double.scala#L244)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions