Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit a18e42b

Browse files
committed
SI-9298 Fix erasure of value classes in Java
Value classes that appear in signatures of Java defined methods should not be erased to the underlying type. Before this change, we'd get a `ClassCastException`, as the Scala call site would unbox the value class despite the fact the Java recipient would expect the boxed representation. I've tested this for primitive and object wrapped types in parameter and return position.
1 parent f635ba9 commit a18e42b

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

src/reflect/scala/reflect/internal/transform/Erasure.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ trait Erasure {
254254
def mergeParents(parents: List[Type]): Type =
255255
if (parents.isEmpty) ObjectTpe
256256
else parents.head
257+
258+
override protected def eraseDerivedValueClassRef(tref: TypeRef): Type = eraseNormalClassRef(tref)
257259
}
258260

259261
object scalaErasure extends ScalaErasureMap

test/files/run/t9298/Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Test {
2+
public void consume(VC vc) {}
3+
4+
public static void main(String[] args) {
5+
new Client().test();
6+
}
7+
}

test/files/run/t9298/VC.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class VC(val s: String) extends AnyVal
2+
3+
class Client {
4+
def test = new Test().consume(new VC(""))
5+
}

test/files/run/t9298b/Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Test {
2+
public VC identity(VC vc) { return vc; }
3+
4+
public static void main(String[] args) {
5+
new Client().test();
6+
}
7+
}

test/files/run/t9298b/VC.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class VC(val s: Int) extends AnyVal
2+
3+
class Client {
4+
def test = {
5+
val vc: VC = new Test().identity(new VC(42))
6+
assert(vc.s == 42)
7+
}
8+
}

0 commit comments

Comments
 (0)