JVM IR: fix detection of enum entries in the same module

Use the new `IrClass.hasEnumEntries` flag added in the previous commit.

 #KT-61208 Fixed
This commit is contained in:
Alexander Udalov
2023-08-15 22:49:57 +02:00
committed by Space Team
parent c33c918bd4
commit 6219f7fc0d
21 changed files with 133 additions and 3 deletions
@@ -2254,6 +2254,12 @@ public class FirLightTreeBytecodeTextTestGenerated extends AbstractFirLightTreeB
runTest("compiler/testData/codegen/bytecodeText/enum/enumEntriesMultiMapping.kt");
}
@Test
@TestMetadata("enumEntriesNoMapping.kt")
public void testEnumEntriesNoMapping() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/enumEntriesNoMapping.kt");
}
@Test
@TestMetadata("kt18731.kt")
public void testKt18731() throws Exception {
@@ -2254,6 +2254,12 @@ public class FirPsiBytecodeTextTestGenerated extends AbstractFirPsiBytecodeTextT
runTest("compiler/testData/codegen/bytecodeText/enum/enumEntriesMultiMapping.kt");
}
@Test
@TestMetadata("enumEntriesNoMapping.kt")
public void testEnumEntriesNoMapping() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/enumEntriesNoMapping.kt");
}
@Test
@TestMetadata("kt18731.kt")
public void testKt18731() throws Exception {
@@ -207,6 +207,11 @@ public class IncrementalFirICLightTreeJvmCompilerRunnerTestGenerated extends Abs
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -207,6 +207,11 @@ public class IncrementalFirLightTreeJvmCompilerRunnerTestGenerated extends Abstr
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -216,6 +216,11 @@ public class IncrementalK1JsKlibCompilerRunnerTestGenerated extends AbstractIncr
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -216,6 +216,11 @@ public class IncrementalK1JsKlibCompilerWithScopeExpansionRunnerTestGenerated ex
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -217,6 +217,11 @@ public class IncrementalK1JvmCompilerRunnerTestGenerated extends AbstractIncreme
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -216,6 +216,11 @@ public class IncrementalK2JsKlibCompilerWithScopeExpansionRunnerTestGenerated ex
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithInlineFunctionRemoved")
public void testFileWithInlineFunctionRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithInlineFunctionRemoved/");
@@ -207,6 +207,11 @@ public class IncrementalK2JvmCompilerRunnerTestGenerated extends AbstractIncreme
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -529,11 +529,12 @@ fun IrClass.isEnumClassWhichRequiresExternalEntries(): Boolean =
isEnumClass && (isFromJava() || !hasEnumEntriesFunction())
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())
// Enums from other modules have `entries` if and only if the flag `hasEnumEntries` is true.
return if (isInCurrentModule())
functions.any { it.isGetEntries() } || properties.any { it.getter?.isGetEntries() == true }
else hasEnumEntries
}
private fun IrSimpleFunction.isGetEntries(): Boolean =
@@ -0,0 +1,25 @@
// TARGET_BACKEND: JVM_IR
// FULL_JDK
// WITH_STDLIB
// MODULE: lib
// !LANGUAGE: +EnumEntries
// FILE: MyEnums.kt
enum class MyEnum {
N, O
}
enum class MyEnum2 {
O, K
}
// MODULE: caller(lib)
// !LANGUAGE: +EnumEntries
// FILE: Box.kt
@OptIn(ExperimentalStdlibApi::class)
fun box(): String {
return MyEnum.entries[1].toString() + MyEnum2.entries[1].toString()
}
// 0 class [a-zA-Z]+\$EntriesMappings
@@ -2254,6 +2254,12 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/enum/enumEntriesMultiMapping.kt");
}
@Test
@TestMetadata("enumEntriesNoMapping.kt")
public void testEnumEntriesNoMapping() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/enum/enumEntriesNoMapping.kt");
}
@Test
@TestMetadata("kt18731.kt")
public void testKt18731() throws Exception {
@@ -886,6 +886,11 @@ public class IncrementalK1JvmJpsTestGenerated extends AbstractIncrementalK1JvmJp
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -212,6 +212,11 @@ public class IncrementalK2FirICLightTreeJvmJpsTestGenerated extends AbstractIncr
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -212,6 +212,11 @@ public class IncrementalK2JvmJpsTestGenerated extends AbstractIncrementalK2JvmJp
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -212,6 +212,11 @@ public class IncrementalK2LightTreeJvmJpsTestGenerated extends AbstractIncrement
runTest("jps/jps-plugin/testData/incremental/pureKotlin/dependencyClassReferenced/");
}
@TestMetadata("entriesMappings")
public void testEntriesMappings() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/entriesMappings/");
}
@TestMetadata("fileWithConstantRemoved")
public void testFileWithConstantRemoved() throws Exception {
runTest("jps/jps-plugin/testData/incremental/pureKotlin/fileWithConstantRemoved/");
@@ -0,0 +1 @@
enum class EnumClass { A, B, C }
@@ -0,0 +1,11 @@
================ Step #1 =================
Cleaning output files:
out/production/module/EntriesKt.class
out/production/module/META-INF/module.kotlin_module
End of files
Compiling files:
src/entries.kt
End of files
Exit code: OK
------------------------------------------
@@ -0,0 +1,5 @@
fun test() {
EnumClass.entries.forEach {
println(it)
}
}
@@ -0,0 +1,7 @@
fun test() {
EnumClass.entries.forEach {
println(it)
}
}
// empty line
@@ -0,0 +1,7 @@
================ Step #1 =================
Compiling files:
src/entries.kt
End of files
Exit code: OK
------------------------------------------