Skip to content

Fix #1065 erasedLub for arrays of primitives. #1067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ object TypeErasure {
}

/** The erased least upper bound is computed as follows
* - if both argument are arrays, an array of the lub of the element types
* - if both argument are arrays of objects, an array of the lub of the element types
* - if both arguments are arrays of same primitives, an array of this primitive
* - if one argument is array of primitives and the other is array of objects, Object
* - if one argument is an array, Object
* - otherwise a common superclass or trait S of the argument classes, with the
* following two properties:
Expand All @@ -217,8 +219,14 @@ object TypeErasure {
*/
def erasedLub(tp1: Type, tp2: Type)(implicit ctx: Context): Type = tp1 match {
case JavaArrayType(elem1) =>
import dotty.tools.dotc.transform.TypeUtils._
tp2 match {
case JavaArrayType(elem2) => JavaArrayType(erasedLub(elem1, elem2))
case JavaArrayType(elem2) =>
if (elem1.isPrimitiveValueType || elem2.isPrimitiveValueType) {
if (elem1.classSymbol eq elem2.classSymbol) // same primitive
JavaArrayType(elem1)
else defn.ObjectType
} else JavaArrayType(erasedLub(elem1, elem2))
case _ => defn.ObjectType
}
case _ =>
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/erasure-array.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://github.com/lampepfl/dotty/issues/1065
package hello

object world {
def mkArray(atype: Int): Array[_ <: AnyVal] = {
(if (atype == 1) new Array[Int](10) else new Array[Float](10))
}

def main(args: Array[String]): Unit = {
println(mkArray(1))
}
}