Skip to content

Commit 692cd5a

Browse files
committed
Fix #1065 erasedLub for arrays of primitives.
Unlike arrays of objects, that can be accessed as an array of a super type of this object, int[] cannot be accessed as an array of primitives.
1 parent 93dd1cf commit 692cd5a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ object TypeErasure {
205205
}
206206

207207
/** The erased least upper bound is computed as follows
208-
* - if both argument are arrays, an array of the lub of the element types
208+
* - if both argument are arrays of objects, an array of the lub of the element types
209+
* - if both arguments are arrays of same primitives, an array of this primitive
210+
* - if one argument is array of primitives and the other is array of objects, Object
209211
* - if one argument is an array, Object
210212
* - otherwise a common superclass or trait S of the argument classes, with the
211213
* following two properties:
@@ -217,8 +219,14 @@ object TypeErasure {
217219
*/
218220
def erasedLub(tp1: Type, tp2: Type)(implicit ctx: Context): Type = tp1 match {
219221
case JavaArrayType(elem1) =>
222+
import dotty.tools.dotc.transform.TypeUtils._
220223
tp2 match {
221-
case JavaArrayType(elem2) => JavaArrayType(erasedLub(elem1, elem2))
224+
case JavaArrayType(elem2) =>
225+
if (elem1.isPrimitiveValueType || elem2.isPrimitiveValueType) {
226+
if (elem1.classSymbol eq elem2.classSymbol) // same primitive
227+
JavaArrayType(elem1)
228+
else defn.ObjectType
229+
} else JavaArrayType(erasedLub(elem1, elem2))
222230
case _ => defn.ObjectType
223231
}
224232
case _ =>

0 commit comments

Comments
 (0)