WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Draft
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
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
28 changes: 12 additions & 16 deletions compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* }
* ```
*/
Expand All @@ -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)
* }
* ```
*/
Expand All @@ -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)
* }
* ```
*/
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions library/src/scala/runtime/Statics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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> T ioobe(int n) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException(String.valueOf(n));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/run/1938.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
8 changes: 4 additions & 4 deletions tests/run/i2314.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading