Skip to content

Commit 61ddff4

Browse files
committed
Merge pull request #532 from smarter/fix/vc-java-compat
Fix compatibility of Java with value classes
2 parents 3b3a51f + e8f3224 commit 61ddff4

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,15 @@ object SymDenotations {
370370
final def isAnonymousModuleVal(implicit ctx: Context) =
371371
this.symbol.is(ModuleVal) && (initial.asSymDenotation.name startsWith nme.ANON_CLASS)
372372

373+
/** Is this a companion class method or companion object method?
374+
* These methods are generated by Symbols#synthesizeCompanionMethod
375+
* and used in SymDenotations#companionClass and
376+
* SymDenotations#companionModule .
377+
*/
378+
final def isCompanionMethod(implicit ctx: Context) =
379+
name.toTermName == nme.COMPANION_CLASS_METHOD ||
380+
name.toTermName == nme.COMPANION_MODULE_METHOD
381+
373382
/** Is symbol a primitive value class? */
374383
def isPrimitiveValueClass(implicit ctx: Context) = defn.ScalaValueClasses contains symbol
375384

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,18 @@ object TypeErasure {
142142
* - For $asInstanceOf : [T]T
143143
* - For $isInstanceOf : [T]Boolean
144144
* - For all abstract types : = ?
145-
* - For COMPANION_CLASS_METHOD : the erasure of their type with semiEraseVCs = false,
146-
* this is needed to keep [[SymDenotation#companionClass]]
147-
* working after erasure for value classes.
145+
* - For companion methods : the erasure of their type with semiEraseVCs = false.
146+
* The signature of these methods are used to keep a
147+
* 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.name ne nme.COMPANION_CLASS_METHOD
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(

src/dotty/tools/dotc/transform/ValueClasses.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object ValueClasses {
2525
!d.isConstructor &&
2626
!d.is(SuperAccessor) &&
2727
!d.is(Macro) &&
28-
!(d.name eq nme.COMPANION_MODULE_METHOD)
28+
!d.isCompanionMethod
2929

3030
/** The member that of a derived value class that unboxes it. */
3131
def valueClassUnbox(d: ClassDenotation)(implicit ctx: Context): Symbol =
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)