Skip to content

Commit 7e18110

Browse files
author
Abel Nieto
committed
Add unit test and address review comments
1 parent 46a9783 commit 7e18110

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
9494
private def seqToArray(tree: Tree, pt: Type)(implicit ctx: Context): Tree = tree match {
9595
case SeqLiteral(elems, elemtpt) =>
9696
JavaSeqLiteral(elems, elemtpt)
97-
case app@Apply(fun, args) if isWrappedArray(app) =>
98-
args.head // There's only one argument: the wrapped array
97+
case app@Apply(fun, args) if isWrappedArray(app) => // rewrite a call to `wrapXArray(arr)` to `arr`
98+
args.head
9999
case _ =>
100100
val elemType = tree.tpe.elemType
101101
var elemClass = elemType.classSymbol
@@ -108,7 +108,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
108108
// Because of phantomclasses, the Java array's type might not conform to the return type
109109
}
110110

111-
/** Determines whether the given `tree` represents a wrapped array */
111+
/** Determines whether `tree` is a method call to Predef.wrapXArray */
112112
private def isWrappedArray(tree: Apply)(implicit ctx: Context): Boolean = {
113113
val elemTpe = tree.tpe.elemType
114114
val wrapMethodName = TreeGen.wrapArrayMethodName(elemTpe)

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,32 @@ class TestBCode extends DottyBytecodeTest {
187187
diffInstructions(instructions1, instructions2))
188188
}
189189
}
190+
191+
/** Verifies that arrays are not unnecessarily wrapped when passed to Java varargs methods */
192+
@Test def dontWrapArraysInJavaVarargs = {
193+
val source =
194+
"""
195+
|import java.nio.file._
196+
|class Test {
197+
| def test(xs: Array[String]) = {
198+
| val p4 = Paths.get("Hello", xs: _*)
199+
| }
200+
|}
201+
""".stripMargin
202+
203+
checkBCode(source) { dir =>
204+
val moduleIn = dir.lookupName("Test.class", directory = false)
205+
val moduleNode = loadClassNode(moduleIn.input)
206+
val method = getMethod(moduleNode, "test")
207+
208+
val arrayWrapped = instructionsFromMethod(method).exists { instr: Instruction =>
209+
instr match {
210+
case inv: Invoke => inv.name.contains("wrapRefArray")
211+
case _ => false
212+
}
213+
}
214+
215+
assert(!arrayWrapped, "Arrays should not be wrapped when passed to a Java varargs method\n")
216+
}
217+
}
190218
}

0 commit comments

Comments
 (0)