Skip to content

Commit e8f3224

Browse files
committed
Fix compatibility of Java with value classes
This avoids getting a runtime error when calling a Java-defined method whose signature contains value classes. It happened because we erased the value classes in this signature even though it comes from a classfile. Amusingly, this problem also exists in scalac: <https://issues.scala-lang.org/browse/SI-9298>
1 parent c3a7f46 commit e8f3224

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/dotty/tools/dotc/core/TypeErasure.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,15 @@ object TypeErasure {
145145
* - For companion methods : the erasure of their type with semiEraseVCs = false.
146146
* The signature of these methods are used to keep a
147147
* link between companions and should not be semi-erased.
148+
* - For Java-defined symbols: : the erasure of their type with isJava = true,
149+
* semiEraseVCs = false. Semi-erasure never happens in Java.
148150
* - For all other symbols : the semi-erasure of their types, with
149151
* isJava, isConstructor set according to symbol.
150152
*/
151153
def transformInfo(sym: Symbol, tp: Type)(implicit ctx: Context): Type = {
152-
val semiEraseVCs = !sym.isCompanionMethod
153-
val erase = erasureFn(sym is JavaDefined, semiEraseVCs, sym.isConstructor, wildcardOK = false)
154+
val isJava = sym is JavaDefined
155+
val semiEraseVCs = !isJava && !sym.isCompanionMethod
156+
val erase = erasureFn(isJava, semiEraseVCs, sym.isConstructor, wildcardOK = false)
154157

155158
def eraseParamBounds(tp: PolyType): Type =
156159
tp.derivedPolyType(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package t9298;
2+
3+
class JUse {
4+
public static Meter jm() {
5+
return new Meter(2);
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package t9298
2+
3+
class Meter(val x: Int) extends AnyVal
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// TODO: this should be a run test once we have run tests
2+
3+
package t9298
4+
5+
object Use {
6+
def main(args: Array[String]): Unit = {
7+
val x: Meter = JUse.jm
8+
}
9+
}

0 commit comments

Comments
 (0)