From 9d257a1ed809d6d694569c873e5a0774180e31b1 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 20 Aug 2019 16:01:53 +0200 Subject: [PATCH] JVM IR: generate call to checkNotNull in IrCheckNotNull since 1.4 #KT-22275 --- .../backend/jvm/codegen/ExpressionCodegen.kt | 2 +- .../backend/jvm/intrinsics/IrCheckNotNull.kt | 16 +++++++----- .../kotlin/backend/jvm/intrinsics/ThrowNPE.kt | 26 ------------------- .../exclExclThrowsNpe_1_4.kt | 1 - .../multipleExclExcl_1_4.kt | 1 - .../nullCheckAfterExclExcl_1_4.kt | 1 - 6 files changed, 11 insertions(+), 36 deletions(-) delete mode 100644 compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/ThrowNPE.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index a022aa4ec27..7845428ea2d 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -110,7 +110,7 @@ class ExpressionCodegen( val typeMapper = context.typeMapper val methodSignatureMapper = context.methodSignatureMapper - private val state = classCodegen.state + val state = classCodegen.state private val fileEntry = classCodegen.context.psiSourceManager.getFileEntry(irFunction.fileParent) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt index ce6d3a8aaa9..3556c93ca5d 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt @@ -10,9 +10,9 @@ import org.jetbrains.kotlin.backend.jvm.codegen.ExpressionCodegen import org.jetbrains.kotlin.backend.jvm.codegen.PromisedValue import org.jetbrains.kotlin.codegen.AsmUtil import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods +import org.jetbrains.kotlin.config.ApiVersion import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression import org.jetbrains.org.objectweb.asm.Label -import org.jetbrains.org.objectweb.asm.Type object IrCheckNotNull : IntrinsicMethod() { override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? { @@ -23,11 +23,15 @@ object IrCheckNotNull : IntrinsicMethod() { override fun materialize() { arg0.materialize() mv.dup() - val ifNonNullLabel = Label() - mv.ifnonnull(ifNonNullLabel) - mv.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwNpe", Type.getMethodDescriptor(Type.VOID_TYPE), false) - mv.mark(ifNonNullLabel) + if (codegen.state.languageVersionSettings.apiVersion >= ApiVersion.KOTLIN_1_4) { + mv.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "checkNotNull", "(Ljava/lang/Object;)V", false) + } else { + val ifNonNullLabel = Label() + mv.ifnonnull(ifNonNullLabel) + mv.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwNpe", "()V", false) + mv.mark(ifNonNullLabel) + } } } } -} \ No newline at end of file +} diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/ThrowNPE.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/ThrowNPE.kt deleted file mode 100644 index a3f39b99e06..00000000000 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/ThrowNPE.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.backend.jvm.intrinsics - -import org.jetbrains.kotlin.backend.jvm.JvmBackendContext -import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods -import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression -import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression -import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature -import org.jetbrains.org.objectweb.asm.Type - -object ThrowNPE : IntrinsicMethod() { - override fun toCallable( - expression: IrFunctionAccessExpression, - signature: JvmMethodSignature, - context: JvmBackendContext - ): IrIntrinsicFunction { - return IrIntrinsicFunction.createWithResult(expression, signature, context) { - it.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwNpe", "()V", false) - Type.VOID_TYPE - } - } -} \ No newline at end of file diff --git a/compiler/testData/codegen/box/nullCheckOptimization/exclExclThrowsNpe_1_4.kt b/compiler/testData/codegen/box/nullCheckOptimization/exclExclThrowsNpe_1_4.kt index b66c5368a9c..331309157d0 100644 --- a/compiler/testData/codegen/box/nullCheckOptimization/exclExclThrowsNpe_1_4.kt +++ b/compiler/testData/codegen/box/nullCheckOptimization/exclExclThrowsNpe_1_4.kt @@ -1,6 +1,5 @@ // !API_VERSION: LATEST // TARGET_BACKEND: JVM -// IGNORE_BACKEND: JVM_IR fun box(): String { val s: String? = null diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/multipleExclExcl_1_4.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/multipleExclExcl_1_4.kt index 88cdf076176..59b76a2cb5a 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/multipleExclExcl_1_4.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/multipleExclExcl_1_4.kt @@ -1,5 +1,4 @@ // !API_VERSION: LATEST -// IGNORE_BACKEND: JVM_IR fun test(s: String?): Int { s!! diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/nullCheckAfterExclExcl_1_4.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/nullCheckAfterExclExcl_1_4.kt index 276a372e2d5..80c1c10fc7c 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/nullCheckAfterExclExcl_1_4.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/nullCheckAfterExclExcl_1_4.kt @@ -1,5 +1,4 @@ // !API_VERSION: LATEST -// IGNORE_BACKEND: JVM_IR fun test(s: String?): Int { s!!