[IR] Specify explicitly that classes from Java can be interpreted

In early prototypes of interpreter, it was easier to assume that
all classes from Java can be interpreted and fix something if not.
Check for Java declaration was done by checking that the package name is
starting with "java". But this is actually wrong and can lead to errors
when some code is declared in "java" something package, but is not from
Java stdlib.

#KT-60467 Fixed
This commit is contained in:
Ivan Kylchik
2023-07-19 12:40:55 +02:00
committed by Space Team
parent 43c66ee0e5
commit 30c00f7983
15 changed files with 160 additions and 7 deletions
@@ -28515,6 +28515,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -28515,6 +28515,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -20,8 +20,6 @@ import java.lang.invoke.MethodHandle
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.LinkedHashMap
internal class Wrapper(val value: Any, override val irClass: IrClass, environment: IrInterpreterEnvironment) : Complex {
override val fields: Fields = mutableMapOf()
@@ -100,6 +98,13 @@ internal class Wrapper(val value: Any, override val irClass: IrClass, environmen
"kotlin.text.RegexOption", "kotlin.text.Regex", "kotlin.text.Regex.Companion", "kotlin.text.MatchGroup",
)
private val intrinsicJavaClasses = setOf(
"java.lang.StringBuilder", "java.util.ArrayList",
"java.util.LinkedHashMap", "java.util.LinkedHashSet",
"java.lang.Exception", "java.util.NoSuchElementException", "java.lang.NullPointerException",
"java.lang.IllegalArgumentException", "java.lang.ArithmeticException", "java.lang.UnsupportedOperationException",
)
private val intrinsicFunctionToHandler = mapOf(
"Array.kotlin.collections.asList()" to "kotlin.collections.ArraysKt",
"kotlin.collections.mutableListOf(Array)" to "kotlin.collections.CollectionsKt",
@@ -111,8 +116,7 @@ internal class Wrapper(val value: Any, override val irClass: IrClass, environmen
private val ranges = setOf("kotlin.ranges.CharRange", "kotlin.ranges.IntRange", "kotlin.ranges.LongRange")
private fun IrFunction.getSignature(): String {
val fqName = this.fqName
private fun IrFunction.getSignature(fqName: String = this.fqName): String {
val receiver = (dispatchReceiverParameter ?: extensionReceiverParameter)?.type?.getOnlyName()?.let { "$it." } ?: ""
return this.valueParameters.joinToString(prefix = "$receiver$fqName(", postfix = ")") { it.type.getOnlyName() }
}
@@ -122,11 +126,11 @@ internal class Wrapper(val value: Any, override val irClass: IrClass, environmen
}
fun mustBeHandledWithWrapper(declaration: IrDeclarationWithName): Boolean {
if (declaration is IrFunction) return declaration.getSignature() in intrinsicFunctionToHandler
val fqName = declaration.fqName
return when {
fqName in ranges && (declaration as IrClass).primaryConstructor!!.body == null -> true
else -> fqName in intrinsicClasses || fqName.startsWith("java")
declaration is IrFunction -> declaration.getSignature(fqName) in intrinsicFunctionToHandler
fqName in ranges && (declaration as IrClass).primaryConstructor?.body == null -> true
else -> fqName in intrinsicClasses || fqName in intrinsicJavaClasses
}
}
@@ -0,0 +1,19 @@
// Can't be tested in JVM because frontend doesn't allow such code
// TARGET_BACKEND: JS_IR
// TARGET_BACKEND: NATIVE
// WITH_STDLIB
package java.lang
object Math {
const val E: Double = kotlin.math.<!EVALUATED("2.718281828459045")!>E<!>
const val PI: Double = kotlin.math.<!EVALUATED("3.141592653589793")!>PI<!>
const val OK: String = <!EVALUATED("OK")!>"OK"<!>
}
const val usageE = Math.<!EVALUATED("2.718281828459045")!>E<!>
const val usagePI = Math.<!EVALUATED("3.141592653589793")!>PI<!>
fun box(): String {
return Math.<!EVALUATED("OK")!>OK<!>
}
@@ -0,0 +1,17 @@
// TARGET_BACKEND: JVM_IR
// TARGET_BACKEND: JS_IR
// TARGET_BACKEND: NATIVE
package java2d
class A {
fun getConst() = <!EVALUATED("OK")!>OK<!>
companion object {
const val OK = <!EVALUATED("OK")!>"OK"<!>
}
}
fun box(): String {
return A().getConst()
}
@@ -28515,6 +28515,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -28515,6 +28515,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -24054,6 +24054,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/intOperations.kt");
@@ -21093,6 +21093,18 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaLangPackage.kt")
public void testInJavaLangPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaLangPackage.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -21093,6 +21093,18 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaLangPackage.kt")
public void testInJavaLangPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaLangPackage.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -21093,6 +21093,18 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaLangPackage.kt")
public void testInJavaLangPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaLangPackage.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -23900,6 +23900,18 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaLangPackage.kt")
public void testInJavaLangPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaLangPackage.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -24364,6 +24364,18 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaLangPackage.kt")
public void testInJavaLangPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaLangPackage.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -23669,6 +23669,18 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaLangPackage.kt")
public void testInJavaLangPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaLangPackage.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {
@@ -23901,6 +23901,18 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
runTest("compiler/testData/codegen/box/involvesIrInterpreter/floatOperations.kt");
}
@Test
@TestMetadata("inJavaLangPackage.kt")
public void testInJavaLangPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaLangPackage.kt");
}
@Test
@TestMetadata("inJavaPackage.kt")
public void testInJavaPackage() throws Exception {
runTest("compiler/testData/codegen/box/involvesIrInterpreter/inJavaPackage.kt");
}
@Test
@TestMetadata("intOperations.kt")
public void testIntOperations() throws Exception {