From 692cd5a349f44d9862d607ed69eada0bef04bb23 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Mon, 8 Feb 2016 15:02:58 +0100 Subject: 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. --- src/dotty/tools/dotc/core/TypeErasure.scala | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/dotty/tools/dotc/core/TypeErasure.scala b/src/dotty/tools/dotc/core/TypeErasure.scala index e4f91914b..26cac4f72 100644 --- a/src/dotty/tools/dotc/core/TypeErasure.scala +++ b/src/dotty/tools/dotc/core/TypeErasure.scala @@ -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: @@ -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 _ => -- cgit v1.2.3 From e384b3af6859c7e2d8481288e293ee9d86a377b9 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Mon, 8 Feb 2016 15:04:07 +0100 Subject: Test that #1065 is fixed. --- tests/pos/erasure-array.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/pos/erasure-array.scala diff --git a/tests/pos/erasure-array.scala b/tests/pos/erasure-array.scala new file mode 100644 index 000000000..63240e980 --- /dev/null +++ b/tests/pos/erasure-array.scala @@ -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)) + } +} -- cgit v1.2.3