Spread should always copy arrays.
This commit is contained in:
@@ -2683,14 +2683,24 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
}
|
||||
|
||||
if (hasSpread) {
|
||||
boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
|
||||
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 {
|
||||
String owner;
|
||||
String addDescriptor;
|
||||
String toArrayDescriptor;
|
||||
boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
|
||||
if (arrayOfReferences) {
|
||||
owner = "kotlin/jvm/internal/SpreadBuilder";
|
||||
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"
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class VarArgTest extends CodegenTestCase {
|
||||
@Override
|
||||
@@ -89,7 +90,9 @@ public class VarArgTest extends CodegenTestCase {
|
||||
loadText("private fun asList(vararg elems: String) = elems; fun test(ts: Array<String>) = asList(*ts); ");
|
||||
Method main = generateFunction("test");
|
||||
String[] args = {"mama", "papa"};
|
||||
assertTrue(args == main.invoke(null, new Object[]{ args } ));
|
||||
String[] result = (String []) main.invoke(null, new Object[] {args});
|
||||
assertTrue(args != result);
|
||||
assertTrue(Arrays.equals(args, result));
|
||||
}
|
||||
|
||||
public void testArrayAsVararg2() throws InvocationTargetException, IllegalAccessException {
|
||||
|
||||
+6
@@ -4591,6 +4591,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
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")
|
||||
public void testVarargInFunParam() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/vararg/varargInFunParam.kt");
|
||||
|
||||
Reference in New Issue
Block a user