File tree 2 files changed +31
-3
lines changed
src/dotty/tools/dotc/transform
test/dotty/tools/backend/jvm 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -94,8 +94,8 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
94
94
private def seqToArray (tree : Tree , pt : Type )(implicit ctx : Context ): Tree = tree match {
95
95
case SeqLiteral (elems, elemtpt) =>
96
96
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
99
99
case _ =>
100
100
val elemType = tree.tpe.elemType
101
101
var elemClass = elemType.classSymbol
@@ -108,7 +108,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
108
108
// Because of phantomclasses, the Java array's type might not conform to the return type
109
109
}
110
110
111
- /** Determines whether the given `tree` represents a wrapped array */
111
+ /** Determines whether `tree` is a method call to Predef.wrapXArray */
112
112
private def isWrappedArray (tree : Apply )(implicit ctx : Context ): Boolean = {
113
113
val elemTpe = tree.tpe.elemType
114
114
val wrapMethodName = TreeGen .wrapArrayMethodName(elemTpe)
Original file line number Diff line number Diff line change @@ -187,4 +187,32 @@ class TestBCode extends DottyBytecodeTest {
187
187
diffInstructions(instructions1, instructions2))
188
188
}
189
189
}
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
+ }
190
218
}
You can’t perform that action at this time.
0 commit comments