JVM IR: fix behavior of Enum.entries for unlowered enums

#KT-57671 Fixed
This commit is contained in:
Alexander Udalov
2023-04-20 19:19:06 +02:00
committed by Space Team
parent 449c866c7a
commit f04d01cf21
6 changed files with 75 additions and 5 deletions
@@ -2253,6 +2253,18 @@ public class FirLightTreeBytecodeTextTestGenerated extends AbstractFirLightTreeB
public void testKt18731_2() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt18731_2.kt");
}
@Test
@TestMetadata("kt57671_1.kt")
public void testKt57671_1() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt57671_1.kt");
}
@Test
@TestMetadata("kt57671_2.kt")
public void testKt57671_2() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt57671_2.kt");
}
}
@Nested
@@ -2253,6 +2253,18 @@ public class FirPsiBytecodeTextTestGenerated extends AbstractFirPsiBytecodeTextT
public void testKt18731_2() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt18731_2.kt");
}
@Test
@TestMetadata("kt57671_1.kt")
public void testKt57671_1() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt57671_1.kt");
}
@Test
@TestMetadata("kt57671_2.kt")
public void testKt57671_2() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt57671_2.kt");
}
}
@Nested
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.ir.isInCurrentModule
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.builders.declarations.addField
@@ -111,13 +112,20 @@ class EnumExternalEntriesLowering(private val context: JvmBackendContext) : File
return IrGetFieldImpl(expression.startOffset, expression.endOffset, field.symbol, field.type)
}
private fun IrClass.hasEnumEntriesFunction() = functions.any {
it.name.toString() == "<get-entries>"
&& it.dispatchReceiverParameter == null
&& it.extensionReceiverParameter == null
&& it.valueParameters.isEmpty()
private fun IrClass.hasEnumEntriesFunction(): Boolean {
// Enums from other modules are always loaded with a property `entries` which has a getter `<get-entries>`.
// Enums from the current module will have a property `entries` if they are unlowered yet (i.e. enum is declared in another file
// which will be lowered after the file with the call site), or a function `<get-entries>` if they are already lowered.
return functions.any { it.isGetEntries() }
|| (properties.any { it.getter?.isGetEntries() == true } && isInCurrentModule())
}
private fun IrSimpleFunction.isGetEntries(): Boolean =
name.toString() == "<get-entries>"
&& dispatchReceiverParameter == null
&& extensionReceiverParameter == null
&& valueParameters.isEmpty()
override fun visitClassNew(declaration: IrClass): IrStatement {
val oldState = state
val mappingState = EntriesMappingState()
@@ -0,0 +1,13 @@
// TARGET_BACKEND: JVM_IR
// FILE: 1.kt
enum class E
// FILE: 2.kt
@OptIn(ExperimentalStdlibApi::class)
fun test() {
E.entries
}
// 0 EntriesMappings
@@ -0,0 +1,13 @@
// TARGET_BACKEND: JVM_IR
// FILE: 1.kt
@OptIn(ExperimentalStdlibApi::class)
fun test() {
E.entries
}
// FILE: 2.kt
enum class E
// 0 EntriesMappings
@@ -2253,6 +2253,18 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
public void testKt18731_2() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt18731_2.kt");
}
@Test
@TestMetadata("kt57671_1.kt")
public void testKt57671_1() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt57671_1.kt");
}
@Test
@TestMetadata("kt57671_2.kt")
public void testKt57671_2() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/kt57671_2.kt");
}
}
@Nested