Skip to content

Commit 46a9783

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 46a9783

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

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

Lines changed: 10 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 app@Apply(fun, args) if isWrappedArray(app) =>
98+
args.head // There's only one argument: the wrapped array
9799
case _ =>
98100
val elemType = tree.tpe.elemType
99101
var elemClass = elemType.classSymbol
@@ -106,6 +108,14 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
106108
// Because of phantomclasses, the Java array's type might not conform to the return type
107109
}
108110

111+
/** Determines whether the given `tree` represents a wrapped array */
112+
private def isWrappedArray(tree: Apply)(implicit ctx: Context): Boolean = {
113+
val elemTpe = tree.tpe.elemType
114+
val wrapMethodName = TreeGen.wrapArrayMethodName(elemTpe)
115+
val wrapMethodSym = defn.ScalaPredefModuleRef.denot.requiredMethod(wrapMethodName)
116+
tree.fun.symbol == wrapMethodSym
117+
}
118+
109119
override def transformTypeApply(tree: TypeApply)(implicit ctx: Context): Tree =
110120
transformTypeOfTree(tree)
111121

0 commit comments

Comments
 (0)