Skip to content

Commit 1dfddef

Browse files
author
Abel Nieto
committed
Fix #3675: don't unnecessarily wrap arrays when eliminating java varargs
When eliminating varargs in a java method call, we need to convert Seqs into Arrays. However, if the object we're passing is already a wrapped array, we just need to unwrap it. Testing ======= Eliminating varargs in import java.nio.file._ class Test { def test(xs: Array[String]) = { val p4 = Paths.get("Hello", xs: _*) } } now gives val p4: java.nio.file.Path = java.nio.file.Paths.get("Hello", xs)
1 parent 79281da commit 1dfddef

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +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 Apply(fun, args) if TreeGen.isRefOrPrimWrappedArray(tree, tree.tpe.elemType) =>
98+
args.head // There's only one argument: the wrapped array
9799
case _ =>
98100
val elemType = tree.tpe.elemType
99101
var elemClass = elemType.classSymbol

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,15 @@ object TreeGen {
2323
.select(wrapArrayMethodName(elemtp))
2424
.appliedToTypes(if (elemtp.isPrimitiveValueType) Nil else elemtp :: Nil)
2525
.appliedTo(tree)
26+
27+
/** Determines whether the given `tree` an application of `wrapRefArray` or `wrapXArray` */
28+
def isRefOrPrimWrappedArray(tree: Tree, elemTpe: Type)(implicit ctx: Context): Boolean = {
29+
tree match {
30+
case Apply(fun, _) =>
31+
val sname = fun.symbol.name
32+
sname == nme.wrapRefArray || sname == nme.wrapXArray(elemTpe.classSymbol.name)
33+
case _ =>
34+
false
35+
}
36+
}
2637
}

0 commit comments

Comments
 (0)