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 now throwing NPE in 2.10 #8746

Closed
scabug opened this issue Jul 24, 2014 · 3 comments
Closed

null.asInstanceOf now throwing NPE in 2.10 #8746

scabug opened this issue Jul 24, 2014 · 3 comments
Labels

Comments

@scabug
Copy link

scabug commented Jul 24, 2014

This code compiles and runs on Scala 2.9.3, but throws an NPE when run with 2.10. The NPE was thrown for multiple types including Strings, Lists, Ints, and Unit.

object BugTestCase {

  def main(args: Array[String]) {
    doNothing("string")
  }

  def doNothing[T](code: => T): T = {
    null.asInstanceOf
  }
}
Exception in thread "main" java.lang.NullPointerException
	at BugTestCase$.doNothing(QMBugTestCases.scala:12)
	at BugTestCase$.main(QMBugTestCases.scala:8)
	at BugTestCase.main(QMBugTestCases.scala)

we have fixed it by adding an type parameter to asInstanceOf:

  def doNothing[T](code: => T): T = {
    null.asInstanceOf[T]
  }

I imagine that the 2.9 code worked by assuming the type parameter on asInstanceOf, but that's just a guess. We would like to know if this change was intended for 2.10+?

@scabug
Copy link
Author

scabug commented Jul 24, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8746?orig=1
Reporter: Thomas Clay (Tombstone)
Affected Versions: 2.10.4

@scabug
Copy link
Author

scabug commented Dec 6, 2014

@kanielc said:
Does anyone have any idea what the correct behaviour is here? Should it be an error, or should the type be inferred correctly as T?

@scabug
Copy link
Author

scabug commented Dec 6, 2014

@som-snytt said:
Type inference promises to infer the minimal type. That must conform to the expected type but isn't exactly that type.

When you try to handle a Nothing value, it explodes.

Inferring Nothing is especially correct in this case because the method is called doNothing.

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

No branches or pull requests

3 participants