Skip to content

Commit 0c4eb33

Browse files
committed
Merge pull request #1067 from dotty-staging/fix-1065
Fix #1065 erasedLub for arrays of primitives.
2 parents 8aeee17 + e384b3a commit 0c4eb33

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-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 _ =>

tests/pos/erasure-array.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// https://github.com/lampepfl/dotty/issues/1065
2+
package hello
3+
4+
object world {
5+
def mkArray(atype: Int): Array[_ <: AnyVal] = {
6+
(if (atype == 1) new Array[Int](10) else new Array[Float](10))
7+
}
8+
9+
def main(args: Array[String]): Unit = {
10+
println(mkArray(1))
11+
}
12+
}

0 commit comments

Comments
 (0)