From fd70bb9222d9e76d6b794d1e50da0ba0fab438cb Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 8 Feb 2016 15:09:13 +0100 Subject: [PATCH] Fix bug in erased lub of Java array types. The lub of Int[] and Float[] is Object, not Object[]. Fixes #1065. --- src/dotty/tools/dotc/core/TypeErasure.scala | 8 ++++++-- tests/run/i1065.scala | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 tests/run/i1065.scala diff --git a/src/dotty/tools/dotc/core/TypeErasure.scala b/src/dotty/tools/dotc/core/TypeErasure.scala index e4f91914bbc2..4b1014f47a4d 100644 --- a/src/dotty/tools/dotc/core/TypeErasure.scala +++ b/src/dotty/tools/dotc/core/TypeErasure.scala @@ -218,8 +218,12 @@ object TypeErasure { def erasedLub(tp1: Type, tp2: Type)(implicit ctx: Context): Type = tp1 match { case JavaArrayType(elem1) => tp2 match { - case JavaArrayType(elem2) => JavaArrayType(erasedLub(elem1, elem2)) - case _ => defn.ObjectType + case JavaArrayType(elem2) => + val lub = erasedLub(elem1, elem2) + if ((elem1 <:< lub) && (elem2 <:< lub)) JavaArrayType(lub) + else defn.ObjectType + case _ => + defn.ObjectType } case _ => tp2 match { diff --git a/tests/run/i1065.scala b/tests/run/i1065.scala new file mode 100644 index 000000000000..1a099d7bd645 --- /dev/null +++ b/tests/run/i1065.scala @@ -0,0 +1,9 @@ +object Test { + def mkArray(atype: Int): Array[_ <: AnyVal] = { + (if (atype == 1) new Array[Int](10) else new Array[Float](10)) + } + + def main(args: Array[String]): Unit = { + assert(mkArray(1).length == 10) + } +}