Skip to content

Fix #6506: Hide internal Tuple.fromArray implementation #6510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2019
Merged
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
17 changes: 13 additions & 4 deletions library/src-3.x/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sealed trait Tuple extends Any {
val t = asInstanceOf[Tuple4[_, _, _, _]]
Tuple5(x, t._1, t._2, t._3, t._4).asInstanceOf[Result]
case Some(n) =>
fromArray[H *: this.type](cons$Array(x, toArray))
knownTupleFromArray[H *: this.type](cons$Array(x, toArray))
case _ =>
runtime.DynamicTuple.dynamic_*:[This, H](this, x)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ sealed trait Tuple extends Any {
}

inline def genericConcat[T <: Tuple](xs: Tuple, ys: Tuple): Tuple =
fromArray[T](xs.toArray ++ ys.toArray)
knownTupleFromArray[T](xs.toArray ++ ys.toArray)

inline def size[This >: this.type <: Tuple]: Size[This] = {
type Result = Size[This]
Expand Down Expand Up @@ -164,7 +164,7 @@ object Tuple {
elems1
}

inline def fromArray[T <: Tuple](xs: Array[Object]): T =
private[scala] inline def knownTupleFromArray[T <: Tuple](xs: Array[Object]): T =
inline constValue[BoundedSize[T]] match {
case 0 => ().asInstanceOf[T]
case 1 => Tuple1(xs(0)).asInstanceOf[T]
Expand All @@ -191,6 +191,15 @@ object Tuple {
case 22 => Tuple22(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20), xs(21)).asInstanceOf[T]
case _ => TupleXXL(xs).asInstanceOf[T]
}

def fromArray[T](xs: Array[T]): Tuple = {
val xs2 = xs match {
case xs: Array[Object] => xs
case xs => xs.map(_.asInstanceOf[Object])
}
runtime.DynamicTuple.dynamicFromArray[Tuple](xs2)
}

}

sealed trait NonEmptyTuple extends Tuple {
Expand Down Expand Up @@ -240,7 +249,7 @@ sealed trait NonEmptyTuple extends Tuple {
val t = asInstanceOf[Tuple5[_, _, _, _, _]]
Tuple4(t._2, t._3, t._4, t._5).asInstanceOf[Result]
case Some(n) if n > 5 =>
fromArray[Result](toArray.tail)
knownTupleFromArray[Result](toArray.tail)
case None =>
runtime.DynamicTuple.dynamicTail[This](this)
}
Expand Down
3 changes: 3 additions & 0 deletions tests/pos/i6506.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test {
val t: Tuple = Tuple.fromArray(Array("1", "2", "3"))
}
78 changes: 78 additions & 0 deletions tests/run/Tuple-fromArray.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
()
(0)
(0,1)
(0,1,2)
(0,1,2,3)
(0,1,2,3,4)
(0,1,2,3,4,5)
(0,1,2,3,4,5,6)
(0,1,2,3,4,5,6,7)
(0,1,2,3,4,5,6,7,8)
(0,1,2,3,4,5,6,7,8,9)
(0,1,2,3,4,5,6,7,8,9,10)
(0,1,2,3,4,5,6,7,8,9,10,11)
(0,1,2,3,4,5,6,7,8,9,10,11,12)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
()
(x0)
(x0,x1)
(x0,x1,x2)
(x0,x1,x2,x3)
(x0,x1,x2,x3,x4)
(x0,x1,x2,x3,x4,x5)
(x0,x1,x2,x3,x4,x5,x6)
(x0,x1,x2,x3,x4,x5,x6,x7)
(x0,x1,x2,x3,x4,x5,x6,x7,x8)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23)
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24)
()
(0)
(0,x1)
(0,x1,2)
(0,x1,2,x3)
(0,x1,2,x3,4)
(0,x1,2,x3,4,x5)
(0,x1,2,x3,4,x5,6)
(0,x1,2,x3,4,x5,6,x7)
(0,x1,2,x3,4,x5,6,x7,8)
(0,x1,2,x3,4,x5,6,x7,8,x9)
(0,x1,2,x3,4,x5,6,x7,8,x9,10)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20,x21)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20,x21,22)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20,x21,22,x23)
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20,x21,22,x23,24)
20 changes: 20 additions & 0 deletions tests/run/Tuple-fromArray.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import scala.reflect.ClassTag

object Test {
def main(args: Array[String]): Unit = {

def testArray[T: ClassTag](n: Int, elem: Int => T): Unit = {
val t: Tuple = Tuple.fromArray(Array.tabulate(n)(elem))
println(t)
}

for (i <- 0 to 25)
testArray(i, j => j)

for (i <- 0 to 25)
testArray(i, j => ("x" + j))

for (i <- 0 to 25)
testArray(i, j => if (j % 2 == 0) j else ("x" + j))
}
}