Add bytecode tests for array for-loop iteration.

This commit is contained in:
Mark Punzalan
2019-03-28 15:39:25 -07:00
committed by max-kammerer
parent 7afe121238
commit ba0e016c4e
5 changed files with 54 additions and 10 deletions
@@ -212,22 +212,19 @@ internal class ArrayIterationHandler(private val context: CommonBackendContext)
with(context.createIrBuilder(call.symbol, call.startOffset, call.endOffset)) { with(context.createIrBuilder(call.symbol, call.startOffset, call.endOffset)) {
// Consider the case like: // Consider the case like:
// //
// ``` // for (elem in A) { f(elem) }`
// for (elem in A) { f(elem) }`
// ```
// //
// If we lower it to: // If we lower it to:
// //
// ``` // for (i in A.indices) { f(A[i]) }
// for (i in A.indices) { f(A[i]) }
// ```
// //
// ...then we will break program behaviour if `A` is an expression with side-effect. Instead, we lower it to: // ...then we will break program behaviour if `A` is an expression with side-effect. Instead, we lower it to:
// //
// ``` // val a = A
// val a = A // for (i in a.indices) { f(a[i]) }
// for (i in a.indices) { f(a[i]) } //
// ``` // This also ensures that the semantics of re-assignment of array variables used in the loop is consistent with the semantics
// proposed in https://youtrack.jetbrains.com/issue/KT-21354.
val arrayReference = scope.createTemporaryVariable( val arrayReference = scope.createTemporaryVariable(
call.dispatchReceiver!!, nameHint = "array", call.dispatchReceiver!!, nameHint = "array",
origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE
@@ -0,0 +1,11 @@
fun array() = arrayOfNulls<Any>(4)
fun f() {
for (i in array()) {
}
}
// 0 iterator
// 1 INVOKESTATIC .*\.array \(\)
// 1 ARRAYLENGTH
// 1 IF_ICMP
@@ -0,0 +1,16 @@
fun intArray() = intArrayOf(0, 0, 0, 0)
fun longArray() = longArrayOf(0, 0, 0, 0)
fun f() {
for (i in intArray()) {
}
for (i in longArray()) {
}
}
// 0 iterator
// 1 INVOKESTATIC .*\.intArray \(\)
// 1 INVOKESTATIC .*\.longArray \(\)
// 2 ARRAYLENGTH
// 2 IF_ICMP
@@ -1627,11 +1627,21 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInCharSequence.kt"); runTest("compiler/testData/codegen/bytecodeText/forLoop/forInCharSequence.kt");
} }
@TestMetadata("forInObjectArray.kt")
public void testForInObjectArray() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInObjectArray.kt");
}
@TestMetadata("forInOptimizableUnsignedRange.kt") @TestMetadata("forInOptimizableUnsignedRange.kt")
public void testForInOptimizableUnsignedRange() throws Exception { public void testForInOptimizableUnsignedRange() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInOptimizableUnsignedRange.kt"); runTest("compiler/testData/codegen/bytecodeText/forLoop/forInOptimizableUnsignedRange.kt");
} }
@TestMetadata("forInPrimitiveArray.kt")
public void testForInPrimitiveArray() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInPrimitiveArray.kt");
}
@TestMetadata("forInRangeSpecializedToUntil.kt") @TestMetadata("forInRangeSpecializedToUntil.kt")
public void testForInRangeSpecializedToUntil() throws Exception { public void testForInRangeSpecializedToUntil() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt"); runTest("compiler/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt");
@@ -1637,11 +1637,21 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInCharSequence.kt"); runTest("compiler/testData/codegen/bytecodeText/forLoop/forInCharSequence.kt");
} }
@TestMetadata("forInObjectArray.kt")
public void testForInObjectArray() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInObjectArray.kt");
}
@TestMetadata("forInOptimizableUnsignedRange.kt") @TestMetadata("forInOptimizableUnsignedRange.kt")
public void testForInOptimizableUnsignedRange() throws Exception { public void testForInOptimizableUnsignedRange() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInOptimizableUnsignedRange.kt"); runTest("compiler/testData/codegen/bytecodeText/forLoop/forInOptimizableUnsignedRange.kt");
} }
@TestMetadata("forInPrimitiveArray.kt")
public void testForInPrimitiveArray() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInPrimitiveArray.kt");
}
@TestMetadata("forInRangeSpecializedToUntil.kt") @TestMetadata("forInRangeSpecializedToUntil.kt")
public void testForInRangeSpecializedToUntil() throws Exception { public void testForInRangeSpecializedToUntil() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt"); runTest("compiler/testData/codegen/bytecodeText/forLoop/forInRangeSpecializedToUntil.kt");