Skip to content

Fix #3340: Always throw when casting to scala.Nothing #5874

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 2 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ class Definitions {
lazy val ClassClass: ClassSymbol = ctx.requiredClass("java.lang.Class")
lazy val BoxedNumberClass: ClassSymbol = ctx.requiredClass("java.lang.Number")
lazy val ClassCastExceptionClass: ClassSymbol = ctx.requiredClass("java.lang.ClassCastException")
lazy val ClassCastExceptionClass_stringConstructor: TermSymbol = ClassCastExceptionClass.info.member(nme.CONSTRUCTOR).suchThat(_.info.firstParamTypes match {
case List(pt) => (pt isRef StringClass)
case _ => false
}).symbol.asTerm
lazy val ArithmeticExceptionClass: ClassSymbol = ctx.requiredClass("java.lang.ArithmeticException")
lazy val ArithmeticExceptionClass_stringConstructor: TermSymbol = ArithmeticExceptionClass.info.member(nme.CONSTRUCTOR).suchThat(_.info.firstParamTypes match {
case List(pt) => (pt isRef StringClass)
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ object TypeTestsCasts {
else if (isDerivedValueClass(testCls)) {
expr // adaptToType in Erasure will do the necessary type adaptation
}
else if (testCls eq defn.NothingClass) {
// In the JVM `x.asInstanceOf[Nothing]` would throw a class cast exception except when `x eq null`.
// To avoid this loophole we execute `x` and then regardless of the result throw a `ClassCastException`
val throwCCE = Throw(New(defn.ClassCastExceptionClass.typeRef, defn.ClassCastExceptionClass_stringConstructor,
Literal(Constant("Cannot cast to scala.Nothing")) :: Nil))
Block(expr :: Nil, throwCCE).withSpan(expr.span)
}
else
derivedTree(expr, defn.Any_asInstanceOf, testType)
}
Expand Down
3 changes: 3 additions & 0 deletions tests/pos/i828.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object X {
val x: Int = null.asInstanceOf[Nothing]
}
5 changes: 5 additions & 0 deletions tests/run/i3340.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Test$.f1(i3340.scala:12)
Test$.f2(i3340.scala:16)
Test$.f3(i3340.scala:20)
Test$.f4(i3340.scala:27)
Test$.f5(i3340.scala:34)
45 changes: 45 additions & 0 deletions tests/run/i3340.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
object Test {
def main(args: Array[String]): Unit = {
printlnStackLine(f1)
printlnStackLine(f2)
printlnStackLine(f3)
printlnStackLine(f4)
printlnStackLine(f5)
}

def f1: Unit = {
val a: Nothing =
null.asInstanceOf[Nothing] // throws here
}

def f2: Unit = {
null.asInstanceOf[Nothing] // throws here
}

def f3: Unit = {
null.asInstanceOf[Nothing] // throws here
()
}


def f4: Unit = {
val n: Any = null
n.asInstanceOf[Nothing] // throws here
()
}

def f5: Unit = {
val n: Any = null
val a: Nothing =
n.asInstanceOf[Nothing] // throws here
()
}

def printlnStackLine(t: => Any): Unit = {
try t
catch {
case e: ClassCastException =>
println(e.getStackTrace.head)
}
}
}
7 changes: 7 additions & 0 deletions tests/run/i4410.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
val a =
try null.asInstanceOf[Nothing]
catch { case e: ClassCastException if e.getMessage == "Cannot cast to scala.Nothing" => /* As expected */ }
def main(args: Array[String]): Unit = {
}
}