Skip to content

Commit b5b7bab

Browse files
committed
Fix #6506: Hide internal Tuple.fromArray implementation
Renamed old `fromArray` to `knowTupleFromArray` and added a `fromArray` that a user can call.
1 parent 9b96317 commit b5b7bab

File tree

4 files changed

+114
-4
lines changed

4 files changed

+114
-4
lines changed

library/src-3.x/scala/Tuple.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ sealed trait Tuple extends Any {
4747
val t = asInstanceOf[Tuple4[_, _, _, _]]
4848
Tuple5(x, t._1, t._2, t._3, t._4).asInstanceOf[Result]
4949
case Some(n) =>
50-
fromArray[H *: this.type](cons$Array(x, toArray))
50+
knowTupleFromArray[H *: this.type](cons$Array(x, toArray))
5151
case _ =>
5252
runtime.DynamicTuple.dynamic_*:[This, H](this, x)
5353
}
@@ -93,7 +93,7 @@ sealed trait Tuple extends Any {
9393
}
9494

9595
inline def genericConcat[T <: Tuple](xs: Tuple, ys: Tuple): Tuple =
96-
fromArray[T](xs.toArray ++ ys.toArray)
96+
knowTupleFromArray[T](xs.toArray ++ ys.toArray)
9797

9898
inline def size[This >: this.type <: Tuple]: Size[This] = {
9999
type Result = Size[This]
@@ -164,7 +164,7 @@ object Tuple {
164164
elems1
165165
}
166166

167-
inline def fromArray[T <: Tuple](xs: Array[Object]): T =
167+
private[scala] inline def knowTupleFromArray[T <: Tuple](xs: Array[Object]): T =
168168
inline constValue[BoundedSize[T]] match {
169169
case 0 => ().asInstanceOf[T]
170170
case 1 => Tuple1(xs(0)).asInstanceOf[T]
@@ -191,6 +191,15 @@ object Tuple {
191191
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]
192192
case _ => TupleXXL(xs).asInstanceOf[T]
193193
}
194+
195+
def fromArray[T](xs: Array[T]): Tuple = {
196+
val xs2 = xs match {
197+
case xs: Array[Object] => xs
198+
case xs => xs.map(_.asInstanceOf[Object])
199+
}
200+
runtime.DynamicTuple.dynamicFromArray[Tuple](xs2)
201+
}
202+
194203
}
195204

196205
sealed trait NonEmptyTuple extends Tuple {
@@ -240,7 +249,7 @@ sealed trait NonEmptyTuple extends Tuple {
240249
val t = asInstanceOf[Tuple5[_, _, _, _, _]]
241250
Tuple4(t._2, t._3, t._4, t._5).asInstanceOf[Result]
242251
case Some(n) if n > 5 =>
243-
fromArray[Result](toArray.tail)
252+
knowTupleFromArray[Result](toArray.tail)
244253
case None =>
245254
runtime.DynamicTuple.dynamicTail[This](this)
246255
}

tests/pos/i6506.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
val t: Tuple = Tuple.fromArray(Array("1", "2", "3"))
3+
}

tests/run/Tuple-fromArray.check

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
()
2+
(0)
3+
(0,1)
4+
(0,1,2)
5+
(0,1,2,3)
6+
(0,1,2,3,4)
7+
(0,1,2,3,4,5)
8+
(0,1,2,3,4,5,6)
9+
(0,1,2,3,4,5,6,7)
10+
(0,1,2,3,4,5,6,7,8)
11+
(0,1,2,3,4,5,6,7,8,9)
12+
(0,1,2,3,4,5,6,7,8,9,10)
13+
(0,1,2,3,4,5,6,7,8,9,10,11)
14+
(0,1,2,3,4,5,6,7,8,9,10,11,12)
15+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13)
16+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)
17+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
18+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
19+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)
20+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)
21+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)
22+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
23+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
24+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
25+
(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
26+
(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)
27+
()
28+
(x0)
29+
(x0,x1)
30+
(x0,x1,x2)
31+
(x0,x1,x2,x3)
32+
(x0,x1,x2,x3,x4)
33+
(x0,x1,x2,x3,x4,x5)
34+
(x0,x1,x2,x3,x4,x5,x6)
35+
(x0,x1,x2,x3,x4,x5,x6,x7)
36+
(x0,x1,x2,x3,x4,x5,x6,x7,x8)
37+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
38+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)
39+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)
40+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)
41+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)
42+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)
43+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15)
44+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16)
45+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17)
46+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18)
47+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19)
48+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20)
49+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21)
50+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22)
51+
(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23)
52+
(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)
53+
()
54+
(0)
55+
(0,x1)
56+
(0,x1,2)
57+
(0,x1,2,x3)
58+
(0,x1,2,x3,4)
59+
(0,x1,2,x3,4,x5)
60+
(0,x1,2,x3,4,x5,6)
61+
(0,x1,2,x3,4,x5,6,x7)
62+
(0,x1,2,x3,4,x5,6,x7,8)
63+
(0,x1,2,x3,4,x5,6,x7,8,x9)
64+
(0,x1,2,x3,4,x5,6,x7,8,x9,10)
65+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11)
66+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12)
67+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13)
68+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14)
69+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15)
70+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16)
71+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17)
72+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18)
73+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19)
74+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20)
75+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20,x21)
76+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20,x21,22)
77+
(0,x1,2,x3,4,x5,6,x7,8,x9,10,x11,12,x13,14,x15,16,x17,18,x19,20,x21,22,x23)
78+
(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)

tests/run/Tuple-fromArray.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.reflect.ClassTag
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
6+
def testArray[T: ClassTag](n: Int, elem: Int => T): Unit = {
7+
val t: Tuple = Tuple.fromArray(Array.tabulate(n)(elem))
8+
println(t)
9+
}
10+
11+
for (i <- 0 to 25)
12+
testArray(i, j => j)
13+
14+
for (i <- 0 to 25)
15+
testArray(i, j => ("x" + j))
16+
17+
for (i <- 0 to 25)
18+
testArray(i, j => if (j % 2 == 0) j else ("x" + j))
19+
}
20+
}

0 commit comments

Comments
 (0)