JVM IR: Avoid IMPLICIT_NOTNULL checks on special bridge methods

...with dynamic type checks, except for the `@PlatformDependent`
methods, for which the JVM backend adds null checks.
This commit is contained in:
Steven Schäfer
2020-08-06 17:34:56 +02:00
committed by Alexander Udalov
parent 9026f89ba5
commit 0328fcaf5d
16 changed files with 369 additions and 4 deletions
@@ -3323,6 +3323,56 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt");
} }
@TestMetadata("noNullCheckOnCollectionContains.kt")
public void testNoNullCheckOnCollectionContains() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionContains.kt");
}
@TestMetadata("noNullCheckOnCollectionRemove.kt")
public void testNoNullCheckOnCollectionRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionRemove.kt");
}
@TestMetadata("noNullCheckOnListIndexOf.kt")
public void testNoNullCheckOnListIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListIndexOf.kt");
}
@TestMetadata("noNullCheckOnListLastIndexOf.kt")
public void testNoNullCheckOnListLastIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListLastIndexOf.kt");
}
@TestMetadata("noNullCheckOnMapContainsKey.kt")
public void testNoNullCheckOnMapContainsKey() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsKey.kt");
}
@TestMetadata("noNullCheckOnMapContainsValue.kt")
public void testNoNullCheckOnMapContainsValue() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsValue.kt");
}
@TestMetadata("noNullCheckOnMapGet.kt")
public void testNoNullCheckOnMapGet() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapGet.kt");
}
@TestMetadata("noNullCheckOnMapRemove.kt")
public void testNoNullCheckOnMapRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapRemove.kt");
}
@TestMetadata("nullCheckOnMapGetOrDefault.kt")
public void testNullCheckOnMapGetOrDefault() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapGetOrDefault.kt");
}
@TestMetadata("nullCheckOnMapRemove2.kt")
public void testNullCheckOnMapRemove2() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapRemove2.kt");
}
@TestMetadata("safeCastWithWrongType.kt") @TestMetadata("safeCastWithWrongType.kt")
public void testSafeCastWithWrongType() throws Exception { public void testSafeCastWithWrongType() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt");
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.ir.allOverridden import org.jetbrains.kotlin.backend.common.ir.allOverridden
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrConstKind import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
@@ -107,8 +108,14 @@ class SpecialBridgeMethods(val context: CommonBackendContext) {
val specialMethodNames = (specialMethodsWithDefaults + specialMethods).map { (description) -> description.name }.toHashSet() val specialMethodNames = (specialMethodsWithDefaults + specialMethods).map { (description) -> description.name }.toHashSet()
val specialPropertyNames = specialProperties.map { (description) -> description.name }.toHashSet() val specialPropertyNames = specialProperties.map { (description) -> description.name }.toHashSet()
fun findSpecialWithOverride(irFunction: IrSimpleFunction): Pair<IrSimpleFunction, SpecialMethodWithDefaultInfo>? { fun findSpecialWithOverride(
for (overridden in irFunction.allOverridden()) { irFunction: IrSimpleFunction,
includeSelf: Boolean = false
): Pair<IrSimpleFunction, SpecialMethodWithDefaultInfo>? {
if (irFunction.parent !is IrClass)
return null
for (overridden in irFunction.allOverridden(includeSelf)) {
val description = overridden.toDescription() val description = overridden.toDescription()
specialMethodsWithDefaults[description]?.let { specialMethodsWithDefaults[description]?.let {
return Pair(overridden, it) return Pair(overridden, it)
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower.SpecialBridgeMethods import org.jetbrains.kotlin.backend.common.lower.SpecialBridgeMethods
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.ir.hasPlatformDependent
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrFile
@@ -30,7 +31,8 @@ private enum class AssertionScope {
Enabled, Disabled Enabled, Disabled
} }
private class JvmArgumentNullabilityAssertionsLowering(context: JvmBackendContext) : FileLoweringPass, IrElementTransformer<AssertionScope> { private class JvmArgumentNullabilityAssertionsLowering(context: JvmBackendContext) : FileLoweringPass,
IrElementTransformer<AssertionScope> {
private val isWithUnifiedNullChecks = context.state.unifiedNullChecks private val isWithUnifiedNullChecks = context.state.unifiedNullChecks
private val isCallAssertionsDisabled = context.state.isCallAssertionsDisabled private val isCallAssertionsDisabled = context.state.isCallAssertionsDisabled
@@ -90,7 +92,13 @@ private class JvmArgumentNullabilityAssertionsLowering(context: JvmBackendContex
} }
private fun isCallToMethodWithTypeCheckBarrier(expression: IrMemberAccessExpression<*>): Boolean = private fun isCallToMethodWithTypeCheckBarrier(expression: IrMemberAccessExpression<*>): Boolean =
expression.symbol.owner.safeAs<IrSimpleFunction>()?.let { specialBridgeMethods.findSpecialWithOverride(it) != null } == true expression.symbol.owner.safeAs<IrSimpleFunction>()
?.let {
val bridgeInfo = specialBridgeMethods.findSpecialWithOverride(it, includeSelf = true)
// The JVM BE adds null checks around platform dependent special bridge methods (Map.getOrDefault and the version of
// MutableMap.remove with two arguments).
bridgeInfo != null && !bridgeInfo.first.hasPlatformDependent()
} == true
private val IrStatementOrigin?.isOperatorWithNoNullabilityAssertionsOnExtensionReceiver private val IrStatementOrigin?.isOperatorWithNoNullabilityAssertionsOnExtensionReceiver
get() = this is IrStatementOrigin.COMPONENT_N || this in operatorsWithNoNullabilityAssertionsOnExtensionReceiver get() = this is IrStatementOrigin.COMPONENT_N || this in operatorsWithNoNullabilityAssertionsOnExtensionReceiver
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val m = mapOf<A, String>()
return if (A.nil() in m.keys) "Fail" else "OK"
}
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val m = mutableMapOf<A, String>()
return if (m.remove(A.nil()) == null) "OK" else "Fail"
}
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val l = listOf<A>()
return if (l.indexOf(A.nil()) == -1) "OK" else "Fail"
}
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val l = listOf<A>()
return if (l.lastIndexOf(A.nil()) == -1) "OK" else "Fail"
}
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val m = mapOf<A, String>()
return if (m.containsKey(A.nil())) "Fail" else "OK"
}
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val m = mapOf<String, A>()
return if (m.containsValue(A.nil())) "Fail" else "OK"
}
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val m = mapOf<A, String>()
return m[A.nil()] ?: "OK"
}
@@ -0,0 +1,12 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
val m = mutableMapOf<A, String>()
return if (m.remove(A.nil()) == null) "OK" else "Fail"
}
@@ -0,0 +1,27 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// JVM_TARGET: 1.8
// WITH_RUNTIME
// FULL_JDK
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
// There is a null check on both arguments of Map.getOrDefault, so we expect this code to throw an exception.
// Which exception this is depends on the language version (it's a NullPointerException in Kotlin 1.4).
val m = mapOf<A, A>()
try {
m.getOrDefault(A.nil(), A())
} catch (e: Exception) {
try {
m.getOrDefault(A(), A.nil())
} catch (e: Exception) {
return "OK"
}
return "Fail 2"
}
return "Fail 1"
}
@@ -0,0 +1,27 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// JVM_TARGET: 1.8
// WITH_RUNTIME
// FULL_JDK
// FILE: A.java
public class A {
public static A nil() { return null; }
}
// FILE: test.kt
fun box(): String {
// There is a null check on both arguments of MutableMap.remove, so we expect this code to throw an exception.
// Which exception this is depends on the language version (it's a NullPointerException in Kotlin 1.4).
val m = mutableMapOf<A, A>()
try {
m.remove(A.nil(), A())
} catch (e: Exception) {
try {
m.remove(A(), A.nil())
} catch (e: Exception) {
return "OK"
}
return "Fail 2"
}
return "Fail 1"
}
@@ -3343,6 +3343,56 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt");
} }
@TestMetadata("noNullCheckOnCollectionContains.kt")
public void testNoNullCheckOnCollectionContains() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionContains.kt");
}
@TestMetadata("noNullCheckOnCollectionRemove.kt")
public void testNoNullCheckOnCollectionRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionRemove.kt");
}
@TestMetadata("noNullCheckOnListIndexOf.kt")
public void testNoNullCheckOnListIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListIndexOf.kt");
}
@TestMetadata("noNullCheckOnListLastIndexOf.kt")
public void testNoNullCheckOnListLastIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListLastIndexOf.kt");
}
@TestMetadata("noNullCheckOnMapContainsKey.kt")
public void testNoNullCheckOnMapContainsKey() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsKey.kt");
}
@TestMetadata("noNullCheckOnMapContainsValue.kt")
public void testNoNullCheckOnMapContainsValue() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsValue.kt");
}
@TestMetadata("noNullCheckOnMapGet.kt")
public void testNoNullCheckOnMapGet() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapGet.kt");
}
@TestMetadata("noNullCheckOnMapRemove.kt")
public void testNoNullCheckOnMapRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapRemove.kt");
}
@TestMetadata("nullCheckOnMapGetOrDefault.kt")
public void testNullCheckOnMapGetOrDefault() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapGetOrDefault.kt");
}
@TestMetadata("nullCheckOnMapRemove2.kt")
public void testNullCheckOnMapRemove2() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapRemove2.kt");
}
@TestMetadata("safeCastWithWrongType.kt") @TestMetadata("safeCastWithWrongType.kt")
public void testSafeCastWithWrongType() throws Exception { public void testSafeCastWithWrongType() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt");
@@ -3343,6 +3343,56 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt");
} }
@TestMetadata("noNullCheckOnCollectionContains.kt")
public void testNoNullCheckOnCollectionContains() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionContains.kt");
}
@TestMetadata("noNullCheckOnCollectionRemove.kt")
public void testNoNullCheckOnCollectionRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionRemove.kt");
}
@TestMetadata("noNullCheckOnListIndexOf.kt")
public void testNoNullCheckOnListIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListIndexOf.kt");
}
@TestMetadata("noNullCheckOnListLastIndexOf.kt")
public void testNoNullCheckOnListLastIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListLastIndexOf.kt");
}
@TestMetadata("noNullCheckOnMapContainsKey.kt")
public void testNoNullCheckOnMapContainsKey() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsKey.kt");
}
@TestMetadata("noNullCheckOnMapContainsValue.kt")
public void testNoNullCheckOnMapContainsValue() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsValue.kt");
}
@TestMetadata("noNullCheckOnMapGet.kt")
public void testNoNullCheckOnMapGet() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapGet.kt");
}
@TestMetadata("noNullCheckOnMapRemove.kt")
public void testNoNullCheckOnMapRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapRemove.kt");
}
@TestMetadata("nullCheckOnMapGetOrDefault.kt")
public void testNullCheckOnMapGetOrDefault() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapGetOrDefault.kt");
}
@TestMetadata("nullCheckOnMapRemove2.kt")
public void testNullCheckOnMapRemove2() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapRemove2.kt");
}
@TestMetadata("safeCastWithWrongType.kt") @TestMetadata("safeCastWithWrongType.kt")
public void testSafeCastWithWrongType() throws Exception { public void testSafeCastWithWrongType() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt");
@@ -3323,6 +3323,56 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/instanceOfWithWrongType.kt");
} }
@TestMetadata("noNullCheckOnCollectionContains.kt")
public void testNoNullCheckOnCollectionContains() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionContains.kt");
}
@TestMetadata("noNullCheckOnCollectionRemove.kt")
public void testNoNullCheckOnCollectionRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnCollectionRemove.kt");
}
@TestMetadata("noNullCheckOnListIndexOf.kt")
public void testNoNullCheckOnListIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListIndexOf.kt");
}
@TestMetadata("noNullCheckOnListLastIndexOf.kt")
public void testNoNullCheckOnListLastIndexOf() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnListLastIndexOf.kt");
}
@TestMetadata("noNullCheckOnMapContainsKey.kt")
public void testNoNullCheckOnMapContainsKey() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsKey.kt");
}
@TestMetadata("noNullCheckOnMapContainsValue.kt")
public void testNoNullCheckOnMapContainsValue() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapContainsValue.kt");
}
@TestMetadata("noNullCheckOnMapGet.kt")
public void testNoNullCheckOnMapGet() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapGet.kt");
}
@TestMetadata("noNullCheckOnMapRemove.kt")
public void testNoNullCheckOnMapRemove() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/noNullCheckOnMapRemove.kt");
}
@TestMetadata("nullCheckOnMapGetOrDefault.kt")
public void testNullCheckOnMapGetOrDefault() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapGetOrDefault.kt");
}
@TestMetadata("nullCheckOnMapRemove2.kt")
public void testNullCheckOnMapRemove2() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/nullCheckOnMapRemove2.kt");
}
@TestMetadata("safeCastWithWrongType.kt") @TestMetadata("safeCastWithWrongType.kt")
public void testSafeCastWithWrongType() throws Exception { public void testSafeCastWithWrongType() throws Exception {
runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt"); runTest("compiler/testData/codegen/box/casts/javaInterop/safeCastWithWrongType.kt");