summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2016-05-18 21:54:24 +0200
committerLukas Rytz <lukas.rytz@gmail.com>2016-05-18 22:39:56 +0200
commita5fab1f588a6042ca924a78d225e85d0acddf5db (patch)
tree50674b5dafcfe4f1db7828e5cff39aa854631e55
parent38ca9dec8807ddce36a988bf13b367f4d6f03b9e (diff)
downloadscala-a5fab1f588a6042ca924a78d225e85d0acddf5db.tar.gz
scala-a5fab1f588a6042ca924a78d225e85d0acddf5db.tar.bz2
scala-a5fab1f588a6042ca924a78d225e85d0acddf5db.zip
SI-9066 fix null.asInstanceOf[Unit]
-rw-r--r--src/compiler/scala/tools/nsc/transform/Erasure.scala4
-rw-r--r--test/junit/scala/BoxUnboxTest.scala12
-rw-r--r--test/junit/scala/issues/RunTest.scala34
3 files changed, 27 insertions, 23 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala
index 7bfe5a4740..5e903946c1 100644
--- a/src/compiler/scala/tools/nsc/transform/Erasure.scala
+++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala
@@ -1141,6 +1141,10 @@ abstract class Erasure extends AddInterfaces
else {
val tree1 = preErase(tree)
tree1 match {
+ case TypeApply(fun, targs @ List(targ)) if fun.symbol == Any_asInstanceOf && targ.tpe == UnitTpe =>
+ // SI-9066 prevent transforming `o.asInstanceOf[Unit]` to `o.asInstanceOf[BoxedUnit]`.
+ // adaptMember will then replace the call by a reference to BoxedUnit.UNIT.
+ treeCopy.TypeApply(tree1, transform(fun), targs).clearType()
case EmptyTree | TypeTree() =>
tree1 setType specialScalaErasure(tree1.tpe)
case ArrayValue(elemtpt, trees) =>
diff --git a/test/junit/scala/BoxUnboxTest.scala b/test/junit/scala/BoxUnboxTest.scala
index eb7a35e98c..88b3037e69 100644
--- a/test/junit/scala/BoxUnboxTest.scala
+++ b/test/junit/scala/BoxUnboxTest.scala
@@ -106,14 +106,14 @@ class BoxUnboxTest {
Unit.unbox({eff(); null}); chk()
assertThrows[ClassCastException](Unit.unbox({eff(); ""})); chk()
- val n1 = null.asInstanceOf[Unit] // SI-9066: should be UNIT, but currently null
- assertThrows[AssertionError](assert(n1 == b)) // should not throw
+ val n1 = null.asInstanceOf[Unit]
+ assert(n1 == b)
- val n2 = null.asInstanceOf[Unit] == b // SI-9066: should be true, but currently false
- assertThrows[AssertionError](assert(n2)) // should not throw
+ val n2 = null.asInstanceOf[Unit] == b
+ assert(n2)
def f(a: Any) = "" + a
- val n3 = f(null.asInstanceOf[Unit]) // "null", should be "()". probably same cause as SI-602.
- assertThrows[AssertionError](assertEquals(n3, "()")) // should not throw
+ val n3 = f(null.asInstanceOf[Unit])
+ assertEquals(n3, "()")
}
}
diff --git a/test/junit/scala/issues/RunTest.scala b/test/junit/scala/issues/RunTest.scala
index b81a3e1d6f..3ebdc8a72f 100644
--- a/test/junit/scala/issues/RunTest.scala
+++ b/test/junit/scala/issues/RunTest.scala
@@ -186,10 +186,10 @@ class RunTest extends ClearAfterClass {
|npe(f1( n3.asInstanceOf[VCI])) + // SI-8097
| f1( n4[VCI]) + // "null"
|"-" +
- | f1(null.asInstanceOf[Unit]) + // "null", SI-9066
- | f1( n1.asInstanceOf[Unit]) + // "null", SI-9066
- | f1( n2.asInstanceOf[Unit]) + // "null", SI-9066
- | f1( n3.asInstanceOf[Unit]) + // "null", SI-9066
+ | f1(null.asInstanceOf[Unit]) +
+ | f1( n1.asInstanceOf[Unit]) +
+ | f1( n2.asInstanceOf[Unit]) +
+ | f1( n3.asInstanceOf[Unit]) +
| f1( n4[Unit]) + // "null"
|"-" +
| f2(null.asInstanceOf[Int]) +
@@ -204,10 +204,10 @@ class RunTest extends ClearAfterClass {
|npe(f2( n3.asInstanceOf[VCI])) + // SI-8097
| f2( n4[VCI]) + // "null"
|"-" +
- | f2(null.asInstanceOf[Unit]) + // "null", SI-9066
- | f2( n1.asInstanceOf[Unit]) + // "null", SI-9066
- | f2( n2.asInstanceOf[Unit]) + // "null", SI-9066
- | f2( n3.asInstanceOf[Unit]) + // "null", SI-9066
+ | f2(null.asInstanceOf[Unit]) +
+ | f2( n1.asInstanceOf[Unit]) +
+ | f2( n2.asInstanceOf[Unit]) +
+ | f2( n3.asInstanceOf[Unit]) +
| f2( n4[Unit]) + // "null"
|"-" +
| f3(null.asInstanceOf[Int]) +
@@ -222,10 +222,10 @@ class RunTest extends ClearAfterClass {
|npe(f3( n3.asInstanceOf[VCI])) + // SI-8097
| f3( n4[VCI]) + // "null"
|"-" +
- | f3(null.asInstanceOf[Unit]) + // "null", SI-9066
- | f3( n1.asInstanceOf[Unit]) + // "null", SI-9066
- | f3( n2.asInstanceOf[Unit]) + // "null", SI-9066
- | f3( n3.asInstanceOf[Unit]) + // "null", SI-9066
+ | f3(null.asInstanceOf[Unit]) +
+ | f3( n1.asInstanceOf[Unit]) +
+ | f3( n2.asInstanceOf[Unit]) +
+ | f3( n3.asInstanceOf[Unit]) +
| f3( n4[Unit]) + // "null"
|"-" +
| f4(null.asInstanceOf[Int]) +
@@ -240,14 +240,14 @@ class RunTest extends ClearAfterClass {
|npe(f5( n3.asInstanceOf[VCI])) + // SI-8097
|npe(f5( n4[VCI])) + // SI-8097
|"-" +
- | f6(null.asInstanceOf[Unit]) + // "null", SI-9066
- | f6( n1.asInstanceOf[Unit]) + // "null", SI-9066
- | f6( n2.asInstanceOf[Unit]) + // "null", SI-9066
- | f6( n3.asInstanceOf[Unit]) + // "null", SI-9066
+ | f6(null.asInstanceOf[Unit]) +
+ | f6( n1.asInstanceOf[Unit]) +
+ | f6( n2.asInstanceOf[Unit]) +
+ | f6( n3.asInstanceOf[Unit]) +
| f6( n4[Unit]) // "null"
""".stripMargin
assertEquals(run[String](code),
- "0000null-0npe0npenull-nullnullnullnullnull-0000null-0npe0npenull-nullnullnullnullnull-0000null-0npe0npenull-nullnullnullnullnull-00000-0npe0npenpe-nullnullnullnullnull")
+ "0000null-0npe0npenull-()()()()null-0000null-0npe0npenull-()()()()null-0000null-0npe0npenull-()()()()null-00000-0npe0npenpe-()()()()null")
}
}