Skip to content
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

null.asInstanceOf[ValueClass] results in NullPointerException #5866

Closed
scabug opened this issue Jun 2, 2012 · 4 comments
Closed

null.asInstanceOf[ValueClass] results in NullPointerException #5866

scabug opened this issue Jun 2, 2012 · 4 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Jun 2, 2012

case class Foo(d: Double) extends AnyVal
null.asInstanceOf[Foo]
java.lang.NullPointerException
	at .<init>(<console>:10)
	at .<clinit>(<console>)
	at .<init>(<console>:7)
	at .<clinit>(<console>)
	at $print(<console>)

This behavior is problematic because code in the standard library relies on the fact that casting null to a type returns the "default" value of this type (as specified in §4.2):

grep -r "null.asInstanceOf" *
compiler/scala/tools/nsc/interpreter/AbstractOrMissingHandler.scala:  def apply[T]() = new AbstractOrMissingHandler[T](Console println _, null.asInstanceOf[T])
compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala:    def readConstantValue[T](name: String, default: T = null.asInstanceOf[T]): T = {
compiler/scala/tools/nsc/transform/SpecializeTypes.scala:   *        super.this(null.asInstanceOf[Int], null.asInstanceOf[Int])
continuations/library/scala/util/continuations/package.scala:    new ControlContext((f:A=>B,g:Exception=>B) => fun(f), null.asInstanceOf[A])
continuations/library/scala/util/continuations/ControlContext.scala:      new ControlContext((k:(A1 => B)) => fun((x:A) => k(f(x))), null.asInstanceOf[A1])
continuations/library/scala/util/continuations/ControlContext.scala:          new ControlContext((k: A1 => B, thr: Exception => B) => thr(ex).asInstanceOf[C], null.asInstanceOf[A1])
continuations/library/scala/util/continuations/ControlContext.scala:      }, null.asInstanceOf[A1])
continuations/library/scala/util/continuations/ControlContext.scala:          new ControlContext((k: A1 => B1, thr: Exception => B1) => thr(ex).asInstanceOf[C], null.asInstanceOf[A1])
continuations/library/scala/util/continuations/ControlContext.scala:      }, null.asInstanceOf[A1])
continuations/library/scala/util/continuations/ControlContext.scala:      new ControlContext(fun1, null.asInstanceOf[A1])
continuations/library/scala/util/continuations/ControlContext.scala:          new ControlContext((k: A => B, thr: Exception => B) => thr(ex).asInstanceOf[C], null.asInstanceOf[A])
continuations/library/scala/util/continuations/ControlContext.scala:      new ControlContext(fun1, null.asInstanceOf[A])
library/scala/collection/convert/Wrappers.scala:        case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case ex: ClassCastException => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:        case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case ex: ClassCastException => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:        Some(null.asInstanceOf[B])
library/scala/collection/convert/Wrappers.scala:    override def empty: Repr = null.asInstanceOf[Repr]
library/scala/collection/convert/Wrappers.scala:      case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:        case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case ex: ClassCastException => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:        case None => null.asInstanceOf[B]
library/scala/collection/convert/Wrappers.scala:      case ex: ClassCastException => null.asInstanceOf[B]
library/scala/collection/concurrent/TrieMap.scala:    removehc(k, null.asInstanceOf[V], hc)
library/scala/collection/parallel/ParIterableLike.scala:    @volatile var result: R = null.asInstanceOf[R]
library/scala/collection/parallel/ParIterableLike.scala:    @volatile var result: R = null.asInstanceOf[R]
library/scala/collection/parallel/ParIterableLike.scala:    @volatile var result: R1 = null.asInstanceOf[R1]
library/scala/collection/parallel/ParIterableLike.scala:    @volatile var result: U = null.asInstanceOf[U]
library/scala/collection/parallel/ParIterableLike.scala:    @volatile var result: S = null.asInstanceOf[S]
library/scala/collection/parallel/ParIterableLike.scala:    @volatile var result: U = null.asInstanceOf[U]
library/scala/collection/parallel/ParIterableLike.scala:    @volatile var result: U = null.asInstanceOf[U]
library/scala/collection/mutable/UnrolledBuffer.scala:        array(idx) = null.asInstanceOf[T] // TODO find a way to assign a default here!!
library/scala/collection/TraversableViewLike.scala:      var result: B = null.asInstanceOf[B]
library/scala/concurrent/default/TaskImpl.scala.disabled:      var res: T = null.asInstanceOf[T]
library/scala/concurrent/impl/ExecutionContextImpl.scala:        var result: T = null.asInstanceOf[T]
library/scala/sys/PropImpl.scala:  protected def zero: T = null.asInstanceOf[T]
swing/scala/swing/ComboBox.scala:      private var selected: A = if (items.isEmpty) null.asInstanceOf[A] else items(0)

See also the spec clarifications decided in the second part of #4437 (which I tried to reflect in the spec https://github.com/soc/scala-dist/commit/39337a8e620ea59830fc89a323b17b6ce9917975#L2L4039 ):

Outdated spec: asInstanceOf[T] returns the null object itself if T conforms to scala.AnyRef, and throws a NullPointerException otherwise.

Proposed update: asInstanceOf[T] returns the default value of that type, as specified in 4.2.

(4.2 probably needs some update in the line of "the default value of a value class is an instance of the value class with the default value of its enclosing member".

Casting from null should be made to behave like existing AnyVal and AnyRef classes, so that NPE won't happen in surprising special cases.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jun 2, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5866?orig=1
Reporter: @soc
Affected Versions: 2.10.0
See #4437

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 1, 2012

@soc said:
Fixed in 5589eeeb9a.

@scabug scabug closed this Aug 1, 2012
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Oct 28, 2013

@retronym said:
Residual problem:

class Foo(val d: Double) extends AnyVal {
  override def toString = s"Foo($d)"
}

class Bar(val d: String) extends AnyVal {
  override def toString = s"Foo($d)"
}

object Test {
  def main(args: Array[String]): Unit = {
    val f: Foo = {val n: Any = null; n.asInstanceOf[Foo]}
    println(f)

    val b: Bar = {val n: Any = null; n.asInstanceOf[Bar]}
    println(b)
  }
}
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented May 18, 2016

@lrytz said:
created follow-up #9782

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.