Spread should always copy arrays.
Introduce a special (package private) utility class ArraysUtilJVM to fix Array<T>.asList() issues.
This commit is contained in:
@@ -2685,14 +2685,24 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (hasSpread) {
|
if (hasSpread) {
|
||||||
|
boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
|
||||||
if (size == 1) {
|
if (size == 1) {
|
||||||
gen(arguments.get(0).getArgumentExpression(), type);
|
// Arrays.copyOf(array, newLength)
|
||||||
|
ValueArgument argument = arguments.get(0);
|
||||||
|
Type arrayType = arrayOfReferences ? Type.getType("[Ljava/lang/Object;")
|
||||||
|
: Type.getType("[" + elementType.getDescriptor());
|
||||||
|
gen(argument.getArgumentExpression(), type);
|
||||||
|
v.dup();
|
||||||
|
v.arraylength();
|
||||||
|
v.invokestatic("java/util/Arrays", "copyOf", Type.getMethodDescriptor(arrayType, arrayType, Type.INT_TYPE), false);
|
||||||
|
if (arrayOfReferences) {
|
||||||
|
v.checkcast(type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
String owner;
|
String owner;
|
||||||
String addDescriptor;
|
String addDescriptor;
|
||||||
String toArrayDescriptor;
|
String toArrayDescriptor;
|
||||||
boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
|
|
||||||
if (arrayOfReferences) {
|
if (arrayOfReferences) {
|
||||||
owner = "kotlin/jvm/internal/SpreadBuilder";
|
owner = "kotlin/jvm/internal/SpreadBuilder";
|
||||||
addDescriptor = "(Ljava/lang/Object;)V";
|
addDescriptor = "(Ljava/lang/Object;)V";
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
|
fun <T> copyArray(vararg data: T): Array<out T> = data
|
||||||
|
|
||||||
|
inline fun <reified T> reifiedCopyArray(vararg data: T): Array<out T> = data
|
||||||
|
|
||||||
|
fun copyIntArray(vararg data: Int): IntArray = data
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
val sarr = arrayOf("OK")
|
||||||
|
val sarr2 = copyArray(*sarr)
|
||||||
|
sarr[0] = "Array was not copied"
|
||||||
|
assertEquals(sarr2[0], "OK", "Failed: Array<String>")
|
||||||
|
|
||||||
|
var rsarr = arrayOf("OK")
|
||||||
|
var rsarr2 = reifiedCopyArray(*rsarr)
|
||||||
|
rsarr[0] = "Array was not copied"
|
||||||
|
assertEquals(rsarr2[0], "OK", "Failed: Array<String>, reified copy")
|
||||||
|
|
||||||
|
val iarr = IntArray(1)
|
||||||
|
iarr[0] = 1
|
||||||
|
val iarr2 = copyIntArray(*iarr)
|
||||||
|
iarr[0] = 42
|
||||||
|
assertEquals(iarr2[0], 1, "Failed: IntArray")
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
@@ -91,7 +91,8 @@ public class VarArgTest extends CodegenTestCase {
|
|||||||
Method main = generateFunction("test");
|
Method main = generateFunction("test");
|
||||||
String[] args = {"mama", "papa"};
|
String[] args = {"mama", "papa"};
|
||||||
String[] result = (String []) main.invoke(null, new Object[] {args});
|
String[] result = (String []) main.invoke(null, new Object[] {args});
|
||||||
assertTrue(args == result);
|
assertTrue(args != result);
|
||||||
|
assertTrue(Arrays.equals(args, result));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayAsVararg2() throws InvocationTargetException, IllegalAccessException {
|
public void testArrayAsVararg2() throws InvocationTargetException, IllegalAccessException {
|
||||||
|
|||||||
+6
@@ -4651,6 +4651,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
|||||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithStdlib/vararg"), Pattern.compile("^(.+)\\.kt$"), true);
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithStdlib/vararg"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("spreadCopiesArray.kt")
|
||||||
|
public void testSpreadCopiesArray() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/vararg/spreadCopiesArray.kt");
|
||||||
|
doTestWithStdlib(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("varargInFunParam.kt")
|
@TestMetadata("varargInFunParam.kt")
|
||||||
public void testVarargInFunParam() throws Exception {
|
public void testVarargInFunParam() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/vararg/varargInFunParam.kt");
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/vararg/varargInFunParam.kt");
|
||||||
|
|||||||
@@ -10165,7 +10165,7 @@ public fun ShortArray.asSequence(): Sequence<Short> {
|
|||||||
*/
|
*/
|
||||||
@kotlin.jvm.JvmVersion
|
@kotlin.jvm.JvmVersion
|
||||||
public fun <T> Array<out T>.asList(): List<T> {
|
public fun <T> Array<out T>.asList(): List<T> {
|
||||||
return Arrays.asList(*this)
|
return ArraysUtilJVM.asList(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2015 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kotlin;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class ArraysUtilJVM {
|
||||||
|
static <T> List<T> asList(T[] array) {
|
||||||
|
return Arrays.asList(array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -221,7 +221,7 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
returns("List<T>")
|
returns("List<T>")
|
||||||
body(ArraysOfObjects) {
|
body(ArraysOfObjects) {
|
||||||
"""
|
"""
|
||||||
return Arrays.asList(*this)
|
return ArraysUtilJVM.asList(this)
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user