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

Commit d8a2440

Browse files
committed
use IndexOutOfBoundsException constructor with Int
1 parent 7eba9a7 commit d8a2440

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,11 @@ class Definitions {
673673
@tu lazy val JavaCloneableClass: ClassSymbol = requiredClass("java.lang.Cloneable")
674674
@tu lazy val NullPointerExceptionClass: ClassSymbol = requiredClass("java.lang.NullPointerException")
675675
@tu lazy val IndexOutOfBoundsException: ClassSymbol = requiredClass("java.lang.IndexOutOfBoundsException")
676+
@tu lazy val IndexOutOfBoundsExceptionType: Type = IndexOutOfBoundsException.typeRef
677+
@tu lazy val IndexOutOfBoundsException_IntConstructor: TermSymbol = IndexOutOfBoundsException.info.member(nme.CONSTRUCTOR).suchThat(_.info.firstParamTypes match {
678+
case List(pt) => pt.isRef(IntClass)
679+
case _ => false
680+
}).symbol.asTerm
676681
@tu lazy val ClassClass: ClassSymbol = requiredClass("java.lang.Class")
677682
@tu lazy val BoxedNumberClass: ClassSymbol = requiredClass("java.lang.Number")
678683
@tu lazy val ClassCastExceptionClass: ClassSymbol = requiredClass("java.lang.ClassCastException")

compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
189189
* def productElement(index: Int): Any = index match {
190190
* case 0 => this._1
191191
* case 1 => this._2
192-
* case _ => throw new IndexOutOfBoundsException(index.toString)
192+
* case _ => throw new IndexOutOfBoundsException(index)
193193
* }
194194
* ```
195195
*/
@@ -215,7 +215,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
215215
* def productElement(index: Int): Any = index match {
216216
* case 0 => this.x
217217
* case 1 => this.y
218-
* case _ => throw new IndexOutOfBoundsException(index.toString)
218+
* case _ => throw new IndexOutOfBoundsException(index)
219219
* }
220220
* ```
221221
*/
@@ -242,7 +242,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
242242
* def productElementName(index: Int): String = index match {
243243
* case 0 => "x"
244244
* case 1 => "y"
245-
* case _ => throw new IndexOutOfBoundsException(index.toString)
245+
* case _ => throw new IndexOutOfBoundsException(index)
246246
* }
247247
* ```
248248
*/
@@ -255,20 +255,16 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
255255
Match(index, (cases :+ generateIOBECase(index)).toList)
256256
}
257257

258+
/** Generate a tree equivalent to the following source level code:
259+
* ```scala
260+
* case _: Int => throw new IndexOutOfBoundsException(index)
261+
* ```
262+
*/
258263
def generateIOBECase(index: Tree): CaseDef = {
259-
val ioob = defn.IndexOutOfBoundsException.typeRef
260-
// Second constructor of ioob that takes a String argument
261-
def filterStringConstructor(s: Symbol): Boolean = s.info match {
262-
case m: MethodType if s.isConstructor && m.paramInfos.size == 1 =>
263-
m.paramInfos.head.stripNull() == defn.StringType
264-
case _ => false
265-
}
266-
val constructor = ioob.typeSymbol.info.decls.find(filterStringConstructor(_)).asTerm
267-
val stringIndex = Apply(Select(index, nme.toString_), Nil)
268-
val error = Throw(New(ioob, constructor, List(stringIndex)))
269-
270-
// case _ => throw new IndexOutOfBoundsException(i.toString)
271-
CaseDef(Underscore(defn.IntType), EmptyTree, error)
264+
val rhs = New(defn.IndexOutOfBoundsExceptionType)
265+
.select(defn.IndexOutOfBoundsException_IntConstructor)
266+
.appliedTo(index)
267+
CaseDef(Underscore(defn.IntType), EmptyTree, Throw(rhs))
272268
}
273269

274270
/** The class

0 commit comments

Comments
 (0)