Skip to content

Commit 1d2c641

Browse files
committed
Add a "no-boxing" test for specialised tuples
1 parent 23e4361 commit 1d2c641

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class SpecializeTuples extends MiniPhase:
3232
end transformApply
3333

3434
override def transformSelect(tree: Select)(using Context): Tree = tree match
35-
case Select(qual, nme._1) if isAppliedSpecializableTuple(qual.tpe.widen)
36-
=> Select(qual, nme._1.specializedName(qual.tpe.widen.argInfos.slice(0, 1)))
37-
case Select(qual, nme._2) if isAppliedSpecializableTuple(qual.tpe.widen)
38-
=> Select(qual, nme._2.specializedName(qual.tpe.widen.argInfos.slice(1, 2)))
35+
case Select(qual, nme._1) if isAppliedSpecializableTuple(qual.tpe.widen) =>
36+
Select(qual, nme._1.specializedName(qual.tpe.widen.argInfos.slice(0, 1)))
37+
case Select(qual, nme._2) if isAppliedSpecializableTuple(qual.tpe.widen) =>
38+
Select(qual, nme._2.specializedName(qual.tpe.widen.argInfos.slice(1, 2)))
3939
case _ => tree
4040

4141
private def isAppliedSpecializableTuple(tp: Type)(using Context) = tp match
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package dotty.tools
2+
package dotc
3+
package transform
4+
5+
import org.junit.Test
6+
7+
import dotty.tools.backend.jvm.DottyBytecodeTest
8+
9+
import scala.jdk.CollectionConverters.*
10+
11+
class SpecializeTuplesTests extends DottyBytecodeTest {
12+
@Test def noBoxing = {
13+
given source: String =
14+
"""|class Test {
15+
| def foo: (Int, Int) = (1, 1)
16+
| def bar: Int = foo._1
17+
|}""".stripMargin
18+
19+
checkBCode(source) { dir =>
20+
assertNoBoxing("foo", findClass("Test", dir).methods)
21+
assertNoBoxing("bar", findClass("Test", dir).methods)
22+
}
23+
}
24+
25+
@Test def boxing = {
26+
// Pick a tuple type that isn't specialised.
27+
given source: String =
28+
"""|class Test {
29+
| def t: (Boolean, Byte, Short, Char, Int, Long, Float, Double, AnyRef) = (true, 8, 16, 'c', 32, 64L, 32.0f, 64.0, this)
30+
| def _1 = t._1
31+
| def _2 = t._2
32+
| def _3 = t._3
33+
| def _4 = t._4
34+
| def _5 = t._5
35+
| def _6 = t._6
36+
| def _7 = t._7
37+
| def _8 = t._8
38+
| def _9 = t._9
39+
|}""".stripMargin
40+
41+
checkBCode(source) { dir =>
42+
val methods = findClass("Test", dir).methods
43+
assertBoxing("t", methods)
44+
assertBoxing("_1", methods)
45+
assertBoxing("_2", methods)
46+
assertBoxing("_3", methods)
47+
assertBoxing("_4", methods)
48+
assertBoxing("_5", methods)
49+
assertBoxing("_6", methods)
50+
assertBoxing("_7", methods)
51+
assertBoxing("_8", methods)
52+
assertNoBoxing("_9", methods)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)