Fix issue getting the size property from Collection bounded type

parameters, when lowering for-loops over Collection.indices.
This commit is contained in:
Mark Punzalan
2019-11-06 13:51:51 -08:00
committed by max-kammerer
parent 30679ebfaf
commit 21178a4f1a
13 changed files with 131 additions and 3 deletions
@@ -518,8 +518,11 @@ internal class CollectionIndicesHandler(context: CommonBackendContext) : Indices
parameterCount { it == 0 }
}
// The lowering operates on subtypes of Collection. Therefore, the IrType could be
// a type parameter bounded by Collection. When that is the case, we cannot get
// the class from the type and instead uses the Collection getter.
override val IrType.sizePropertyGetter
get() = getClass()!!.getPropertyGetter("size")!!.owner
get() = getClass()?.getPropertyGetter("size")?.owner ?: context.ir.symbols.collection.getPropertyGetter("size")!!.owner
}
internal class CharSequenceIndicesHandler(context: CommonBackendContext) : IndicesHandler(context) {
@@ -0,0 +1,17 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
fun <T: CharSequence> test(s: T): Int {
var result = 0
for (i in s.indices) {
result = result * 10 + (i + 1)
}
return result
}
fun box(): String {
val test = test("abcd")
if (test != 1234) return "Fail: $test"
return "OK"
}
@@ -0,0 +1,20 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
import kotlin.test.assertEquals
fun <T : Collection<*>> sumIndices(c: T): Int {
var sum = 0
for (i in c.indices) {
sum += i
}
return sum
}
fun box(): String {
val list = listOf(0, 0, 0, 0)
val sum = sumIndices(list)
assertEquals(6, sum)
return "OK"
}
@@ -1,4 +1,4 @@
fun <T: CharSequence> test(sequence: T) {
fun <T : CharSequence> test(sequence: T) {
var s = ""
for (c in sequence) {
s += c
@@ -8,3 +8,5 @@ fun <T: CharSequence> test(sequence: T) {
// 0 iterator
// 0 hasNext
// 0 nextChar
// 1 INVOKEINTERFACE java/lang/CharSequence\.charAt \(I\)C
// 1 INVOKEINTERFACE java/lang/CharSequence\.length \(\)I
@@ -1,4 +1,4 @@
fun <T: CharSequence> test(s: T): Int {
fun <T : CharSequence> test(s: T): Int {
var result = 0
for (i in s.indices) {
result = result * 10 + (i + 1)
@@ -14,6 +14,7 @@ fun <T: CharSequence> test(s: T): Int {
// 0 getEnd
// 0 getFirst
// 0 getLast
// 1 INVOKEINTERFACE java/lang/CharSequence\.length \(\)I
// JVM_TEMPLATES
// 0 IF_ICMPGT
@@ -0,0 +1,25 @@
fun <T : Collection<*>> test(c: T) {
var sum = 0
for (i in c.indices) {
sum += i
}
}
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 1 INVOKEINTERFACE java/util/Collection\.size \(\)I
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// JVM_TEMPLATES
// 1 IF_ICMPGE
// 1 IF
// JVM_IR_TEMPLATES
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 2 IF
@@ -19835,6 +19835,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt");
}
@TestMetadata("forInCharSequenceTypeParameterIndices.kt")
public void testForInCharSequenceTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt");
}
@TestMetadata("forInCollectionImplicitReceiverIndices.kt")
public void testForInCollectionImplicitReceiverIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt");
@@ -19845,6 +19850,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt");
}
@TestMetadata("forInCollectionTypeParameterIndices.kt")
public void testForInCollectionTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt");
}
@TestMetadata("forInNonOptimizedIndices.kt")
public void testForInNonOptimizedIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt");
@@ -1925,6 +1925,11 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt");
}
@TestMetadata("forInCollectionTypeParameterIndices.kt")
public void testForInCollectionTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionTypeParameterIndices.kt");
}
@TestMetadata("forInNonOptimizedIndices.kt")
public void testForInNonOptimizedIndices() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInNonOptimizedIndices.kt");
@@ -19835,6 +19835,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt");
}
@TestMetadata("forInCharSequenceTypeParameterIndices.kt")
public void testForInCharSequenceTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt");
}
@TestMetadata("forInCollectionImplicitReceiverIndices.kt")
public void testForInCollectionImplicitReceiverIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt");
@@ -19845,6 +19850,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt");
}
@TestMetadata("forInCollectionTypeParameterIndices.kt")
public void testForInCollectionTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt");
}
@TestMetadata("forInNonOptimizedIndices.kt")
public void testForInNonOptimizedIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt");
@@ -18654,6 +18654,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt");
}
@TestMetadata("forInCharSequenceTypeParameterIndices.kt")
public void testForInCharSequenceTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt");
}
@TestMetadata("forInCollectionImplicitReceiverIndices.kt")
public void testForInCollectionImplicitReceiverIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt");
@@ -18664,6 +18669,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt");
}
@TestMetadata("forInCollectionTypeParameterIndices.kt")
public void testForInCollectionTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt");
}
@TestMetadata("forInNonOptimizedIndices.kt")
public void testForInNonOptimizedIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt");
@@ -1880,6 +1880,11 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionIndices.kt");
}
@TestMetadata("forInCollectionTypeParameterIndices.kt")
public void testForInCollectionTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInCollectionTypeParameterIndices.kt");
}
@TestMetadata("forInNonOptimizedIndices.kt")
public void testForInNonOptimizedIndices() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInIndices/forInNonOptimizedIndices.kt");
@@ -15585,6 +15585,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt");
}
@TestMetadata("forInCharSequenceTypeParameterIndices.kt")
public void testForInCharSequenceTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt");
}
@TestMetadata("forInCollectionImplicitReceiverIndices.kt")
public void testForInCollectionImplicitReceiverIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt");
@@ -15595,6 +15600,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt");
}
@TestMetadata("forInCollectionTypeParameterIndices.kt")
public void testForInCollectionTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt");
}
@TestMetadata("forInNonOptimizedIndices.kt")
public void testForInNonOptimizedIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt");
@@ -16740,6 +16740,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceIndices.kt");
}
@TestMetadata("forInCharSequenceTypeParameterIndices.kt")
public void testForInCharSequenceTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCharSequenceTypeParameterIndices.kt");
}
@TestMetadata("forInCollectionImplicitReceiverIndices.kt")
public void testForInCollectionImplicitReceiverIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionImplicitReceiverIndices.kt");
@@ -16750,6 +16755,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionIndices.kt");
}
@TestMetadata("forInCollectionTypeParameterIndices.kt")
public void testForInCollectionTypeParameterIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInCollectionTypeParameterIndices.kt");
}
@TestMetadata("forInNonOptimizedIndices.kt")
public void testForInNonOptimizedIndices() throws Exception {
runTest("compiler/testData/codegen/box/ranges/forInIndices/forInNonOptimizedIndices.kt");