diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 8d305eef16e1..ca5a6c61a84e 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -673,6 +673,11 @@ class Definitions { @tu lazy val JavaCloneableClass: ClassSymbol = requiredClass("java.lang.Cloneable") @tu lazy val NullPointerExceptionClass: ClassSymbol = requiredClass("java.lang.NullPointerException") @tu lazy val IndexOutOfBoundsException: ClassSymbol = requiredClass("java.lang.IndexOutOfBoundsException") + @tu lazy val IndexOutOfBoundsExceptionType: Type = IndexOutOfBoundsException.typeRef + @tu lazy val IndexOutOfBoundsException_IntConstructor: TermSymbol = IndexOutOfBoundsException.info.member(nme.CONSTRUCTOR).suchThat(_.info.firstParamTypes match { + case List(pt) => pt.isRef(IntClass) + case _ => false + }).symbol.asTerm @tu lazy val ClassClass: ClassSymbol = requiredClass("java.lang.Class") @tu lazy val BoxedNumberClass: ClassSymbol = requiredClass("java.lang.Number") @tu lazy val ClassCastExceptionClass: ClassSymbol = requiredClass("java.lang.ClassCastException") diff --git a/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala b/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala index f54e69d971f8..01fcf095f46a 100644 --- a/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala +++ b/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala @@ -189,7 +189,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) { * def productElement(index: Int): Any = index match { * case 0 => this._1 * case 1 => this._2 - * case _ => throw new IndexOutOfBoundsException(index.toString) + * case _ => throw new IndexOutOfBoundsException(index) * } * ``` */ @@ -215,7 +215,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) { * def productElement(index: Int): Any = index match { * case 0 => this.x * case 1 => this.y - * case _ => throw new IndexOutOfBoundsException(index.toString) + * case _ => throw new IndexOutOfBoundsException(index) * } * ``` */ @@ -242,7 +242,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) { * def productElementName(index: Int): String = index match { * case 0 => "x" * case 1 => "y" - * case _ => throw new IndexOutOfBoundsException(index.toString) + * case _ => throw new IndexOutOfBoundsException(index) * } * ``` */ @@ -255,20 +255,16 @@ class SyntheticMembers(thisPhase: DenotTransformer) { Match(index, (cases :+ generateIOBECase(index)).toList) } + /** Generate a tree equivalent to the following source level code: + * ```scala + * case _: Int => throw new IndexOutOfBoundsException(index) + * ``` + */ def generateIOBECase(index: Tree): CaseDef = { - val ioob = defn.IndexOutOfBoundsException.typeRef - // Second constructor of ioob that takes a String argument - def filterStringConstructor(s: Symbol): Boolean = s.info match { - case m: MethodType if s.isConstructor && m.paramInfos.size == 1 => - m.paramInfos.head.stripNull() == defn.StringType - case _ => false - } - val constructor = ioob.typeSymbol.info.decls.find(filterStringConstructor(_)).asTerm - val stringIndex = Apply(Select(index, nme.toString_), Nil) - val error = Throw(New(ioob, constructor, List(stringIndex))) - - // case _ => throw new IndexOutOfBoundsException(i.toString) - CaseDef(Underscore(defn.IntType), EmptyTree, error) + val rhs = New(defn.IndexOutOfBoundsExceptionType) + .select(defn.IndexOutOfBoundsException_IntConstructor) + .appliedTo(index) + CaseDef(Underscore(defn.IntType), EmptyTree, Throw(rhs)) } /** The class diff --git a/library/src/scala/runtime/Statics.java b/library/src/scala/runtime/Statics.java index a3cc9e8ac45e..9808b9d13ad2 100644 --- a/library/src/scala/runtime/Statics.java +++ b/library/src/scala/runtime/Statics.java @@ -125,10 +125,10 @@ public static int anyHash(Object x) { private static int anyHashNumber(Number x) { if (x instanceof java.lang.Long) return longHash(((java.lang.Long)x).longValue()); - + if (x instanceof java.lang.Double) return doubleHash(((java.lang.Double)x).doubleValue()); - + if (x instanceof java.lang.Float) return floatHash(((java.lang.Float)x).floatValue()); @@ -148,6 +148,7 @@ public static void releaseFence() throws Throwable { * Used by the synthetic `productElement` and `productElementName` methods in case classes. * Delegating the exception-throwing to this function reduces the bytecode size of the case class. */ + @Deprecated public static final T ioobe(int n) throws IndexOutOfBoundsException { throw new IndexOutOfBoundsException(String.valueOf(n)); } diff --git a/tests/run/1938.scala b/tests/run/1938.scala index 95e94678d988..24986f5b3268 100644 --- a/tests/run/1938.scala +++ b/tests/run/1938.scala @@ -39,7 +39,8 @@ object Test { l.productElement(23) ??? } catch { - case e: IndexOutOfBoundsException => assert(e.getMessage == "23") + case e: IndexOutOfBoundsException => assert(e.getMessage.contains("23")) + case _ => assert(false) } } } diff --git a/tests/run/i2314.scala b/tests/run/i2314.scala index 37e52a1368c5..a5fc2c029b35 100644 --- a/tests/run/i2314.scala +++ b/tests/run/i2314.scala @@ -18,15 +18,15 @@ object Test { a.productElement(-1) ??? } catch { - case e: IndexOutOfBoundsException => - assert(e.getMessage == "-1") + case e: IndexOutOfBoundsException => assert(e.getMessage().contains("-1")) + case _ => assert(false) } try { a.productElement(2) ??? } catch { - case e: IndexOutOfBoundsException => - assert(e.getMessage == "2") + case e: IndexOutOfBoundsException => assert(e.getMessage().contains("2")) + case _ => assert(false) } val b = B(1, "s")