[IR] Create primitive iterators in IrBuiltIns

This change is required to fix stdlib compilation with enabled
linking via signature. All primitive iterators are considered to be
builtins and are created during compile time as deserialized
declarations (at least in K1). But if we meet the definition of some
primitive iterator in code, for example, during stdlib compilation,
we will end up with two different descriptors (deserialized and lazy)
that describe the same entity. Because of that we have conflicts in
symbol table:
* For descriptors, we will end up with multiple IR declarations
that describe the same class, but with different descriptors. With
some magic compilation still works.
* For signatures, we will end up with only one IR declaration in the
table, but it will have wrong deserialized (instead of lazy)
descriptor.

In the end, this change allows us to initialize iterators in advance
with correct descriptor.

#KT-56230
This commit is contained in:
Ivan Kylchik
2023-08-02 18:53:30 +02:00
committed by Space Team
parent 782d5e86f6
commit 5f2de9dbff
19 changed files with 289 additions and 16 deletions
@@ -334,23 +334,32 @@ class IrBuiltInsOverFir(
private fun primitiveIterator(primitiveType: PrimitiveType) =
loadClass(ClassId(StandardClassIds.BASE_COLLECTIONS_PACKAGE, Name.identifier("${primitiveType.typeName}Iterator")))
private val booleanIterator by primitiveIterator(PrimitiveType.BOOLEAN)
private val charIterator by primitiveIterator(PrimitiveType.CHAR)
private val byteIterator by primitiveIterator(PrimitiveType.BYTE)
private val shortIterator by primitiveIterator(PrimitiveType.SHORT)
private val intIterator by primitiveIterator(PrimitiveType.INT)
private val longIterator by primitiveIterator(PrimitiveType.LONG)
private val floatIterator by primitiveIterator(PrimitiveType.FLOAT)
private val doubleIterator by primitiveIterator(PrimitiveType.DOUBLE)
private val _booleanIterator by primitiveIterator(PrimitiveType.BOOLEAN)
private val _charIterator by primitiveIterator(PrimitiveType.CHAR)
private val _byteIterator by primitiveIterator(PrimitiveType.BYTE)
private val _shortIterator by primitiveIterator(PrimitiveType.SHORT)
private val _intIterator by primitiveIterator(PrimitiveType.INT)
private val _longIterator by primitiveIterator(PrimitiveType.LONG)
private val _floatIterator by primitiveIterator(PrimitiveType.FLOAT)
private val _doubleIterator by primitiveIterator(PrimitiveType.DOUBLE)
private val _booleanArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.BOOLEAN, booleanIterator)
private val _charArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.CHAR, charIterator)
private val _byteArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.BYTE, byteIterator)
private val _shortArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.SHORT, shortIterator)
private val _intArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.INT, intIterator)
private val _longArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.LONG, longIterator)
private val _floatArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.FLOAT, floatIterator)
private val _doubleArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.DOUBLE, doubleIterator)
override val booleanIterator: IrClassSymbol get() = _booleanIterator.klass
override val charIterator: IrClassSymbol get() = _charIterator.klass
override val byteIterator: IrClassSymbol get() = _byteIterator.klass
override val shortIterator: IrClassSymbol get() = _shortIterator.klass
override val intIterator: IrClassSymbol get() = _intIterator.klass
override val longIterator: IrClassSymbol get() = _longIterator.klass
override val floatIterator: IrClassSymbol get() = _floatIterator.klass
override val doubleIterator: IrClassSymbol get() = _doubleIterator.klass
private val _booleanArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.BOOLEAN, _booleanIterator)
private val _charArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.CHAR, _charIterator)
private val _byteArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.BYTE, _byteIterator)
private val _shortArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.SHORT, _shortIterator)
private val _intArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.INT, _intIterator)
private val _longArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.LONG, _longIterator)
private val _floatArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.FLOAT, _floatIterator)
private val _doubleArray by createPrimitiveArrayClass(kotlinIrPackage, PrimitiveType.DOUBLE, _doubleIterator)
override val booleanArray: IrClassSymbol get() = _booleanArray.klass
override val charArray: IrClassSymbol get() = _charArray.klass
@@ -4342,6 +4342,18 @@ public class FirLightTreeBlackBoxInlineCodegenWithBytecodeInlinerTestGenerated e
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class FirLightTreeBlackBoxInlineCodegenWithIrInlinerTestGenerated extends
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class FirLightTreeSerializeCompileKotlinAgainstInlineKotlinTestGenerated
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR_SERIALIZE, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class FirPsiBlackBoxInlineCodegenWithBytecodeInlinerTestGenerated extends
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class FirPsiBlackBoxInlineCodegenWithIrInlinerTestGenerated extends Abstr
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class FirPsiSerializeCompileKotlinAgainstInlineKotlinTestGenerated extend
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR_SERIALIZE, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -425,6 +425,27 @@ class IrBuiltInsOverDescriptors(
override val primitiveIrTypesWithComparisons = listOf(charType, byteType, shortType, intType, floatType, longType, doubleType)
override val primitiveFloatingPointIrTypes = listOf(floatType, doubleType)
override val byteIterator = getPrimitiveIterator(PrimitiveType.BYTE)
override val charIterator = getPrimitiveIterator(PrimitiveType.CHAR)
override val shortIterator = getPrimitiveIterator(PrimitiveType.SHORT)
override val intIterator = getPrimitiveIterator(PrimitiveType.INT)
override val longIterator = getPrimitiveIterator(PrimitiveType.LONG)
override val floatIterator = getPrimitiveIterator(PrimitiveType.FLOAT)
override val doubleIterator = getPrimitiveIterator(PrimitiveType.DOUBLE)
override val booleanIterator = getPrimitiveIterator(PrimitiveType.BOOLEAN)
private fun getPrimitiveIterator(kind: PrimitiveType): IrClassSymbol {
val iteratorName = FqName("kotlin.collections.${kind.typeName}Iterator")
return builtIns.getBuiltInClassByFqName(iteratorName).let {
val iteratorIrSymbol = it.toIrSymbol()
it.unsubstitutedMemberScope
.getContributedFunctions(Name.identifier("next"), NoLookupLocation.FROM_BACKEND)
.single()
.toIrSymbol()
iteratorIrSymbol
}
}
override val byteArray = builtIns.getPrimitiveArrayClassDescriptor(PrimitiveType.BYTE).toIrSymbol()
override val charArray = builtIns.getPrimitiveArrayClassDescriptor(PrimitiveType.CHAR).toIrSymbol()
override val shortArray = builtIns.getPrimitiveArrayClassDescriptor(PrimitiveType.SHORT).toIrSymbol()
@@ -102,6 +102,15 @@ abstract class IrBuiltIns {
abstract val primitiveIrTypesWithComparisons: List<IrType>
abstract val primitiveFloatingPointIrTypes: List<IrType>
abstract val byteIterator: IrClassSymbol
abstract val charIterator: IrClassSymbol
abstract val shortIterator: IrClassSymbol
abstract val intIterator: IrClassSymbol
abstract val longIterator: IrClassSymbol
abstract val floatIterator: IrClassSymbol
abstract val doubleIterator: IrClassSymbol
abstract val booleanIterator: IrClassSymbol
abstract val byteArray: IrClassSymbol
abstract val charArray: IrClassSymbol
abstract val shortArray: IrClassSymbol
@@ -0,0 +1,31 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_K2: JVM_IR
// IGNORE_BACKEND_K2_MULTI_MODULE: JVM_IR JVM_IR_SERIALIZE
// ALLOW_KOTLIN_PACKAGE
// Note: order of files is important
// MODULE: kotlin_stdlib
// FILE: _Arrays.kt
package kotlin.collections
public inline fun ByteArray.customFirst(predicate: (Byte) -> Boolean): Byte {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("Array contains no element matching the predicate.")
}
// FILE: PrimitiveIterators.kt
package kotlin.collections
public abstract class ByteIterator : Iterator<Byte> {
override final fun next() = nextByte()
/** Returns the next value in the sequence without boxing. */
public abstract fun nextByte(): Byte
}
// Module: main(kotlin_stdlib)
// FILE: main.kt
fun box(): String {
byteArrayOf(1, 2, 3).customFirst { it == 1.toByte() }
return "OK"
}
@@ -0,0 +1,35 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_K2: JVM_IR
// IGNORE_BACKEND_K2_MULTI_MODULE: JVM_IR JVM_IR_SERIALIZE
// ALLOW_KOTLIN_PACKAGE
// Note: order of files is important
// MODULE: kotlin_stdlib
// FILE: _Arrays.kt
package kotlin.collections
public inline fun ByteArray.customFirst(predicate: (Byte) -> Boolean): Byte {
val iter = this.iterator()
while (iter.hasNext()) {
val element = iter.next()
if (predicate(element)) return element
}
throw NoSuchElementException("Array contains no element matching the predicate.")
}
// FILE: PrimitiveIterators.kt
package kotlin.collections
public abstract class ByteIterator : Iterator<Byte> {
override final fun next() = nextByte()
/** Returns the next value in the sequence without boxing. */
public abstract fun nextByte(): Byte
}
// MODULE: main(kotlin_stdlib)
// FILE: main.kt
fun box(): String {
byteArrayOf(1, 2, 3).customFirst { it == 1.toByte() }
return "OK"
}
@@ -4294,6 +4294,18 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4294,6 +4294,18 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class IrBlackBoxInlineCodegenWithBytecodeInlinerTestGenerated extends Abs
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class IrBlackBoxInlineCodegenWithIrInlinerTestGenerated extends AbstractI
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated extends Ab
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4342,6 +4342,18 @@ public class JvmIrAgainstOldBoxInlineTestGenerated extends AbstractJvmIrAgainstO
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_MULTI_MODULE_IR_AGAINST_OLD, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {
@@ -4294,6 +4294,18 @@ public class JvmOldAgainstIrBoxInlineTestGenerated extends AbstractJvmOldAgainst
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxInline/signature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_MULTI_MODULE_OLD_AGAINST_IR, true);
}
@Test
@TestMetadata("byteIteratorWithForLoop.kt")
public void testByteIteratorWithForLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithForLoop.kt");
}
@Test
@TestMetadata("byteIteratorWithWhileLoop.kt")
public void testByteIteratorWithWhileLoop() throws Exception {
runTest("compiler/testData/codegen/boxInline/signature/byteIteratorWithWhileLoop.kt");
}
@Test
@TestMetadata("inProjectionSubstitution.kt")
public void testInProjectionSubstitution() throws Exception {