From 2f9b7495fcfac58711d09fd719d901057306b86f Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 7 Oct 2020 13:13:17 +0300 Subject: [PATCH] [FIR2IR] Make safe call result always nullable --- .../src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt | 2 +- compiler/testData/codegen/box/classes/kt1018.kt | 1 - .../bytecodeText/nullCheckOptimization/redundantSafeCall.kt | 1 - .../nullCheckOptimization/redundantSafeCall_1_4.kt | 1 - compiler/testData/ir/irText/expressions/safeCalls.fir.txt | 4 ++-- 5 files changed, 3 insertions(+), 6 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt index b183db96278..0e2a147a82e 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt @@ -389,7 +389,7 @@ fun Fir2IrComponents.createSafeCallConstruction( val startOffset = expressionOnNotNull.startOffset val endOffset = expressionOnNotNull.endOffset - val resultType = expressionOnNotNull.type.let { if (isReceiverNullable) it.makeNullable() else it } + val resultType = expressionOnNotNull.type.makeNullable() return IrBlockImpl(startOffset, endOffset, resultType, IrStatementOrigin.SAFE_CALL).apply { statements += receiverVariable statements += IrWhenImpl(startOffset, endOffset, resultType).apply { diff --git a/compiler/testData/codegen/box/classes/kt1018.kt b/compiler/testData/codegen/box/classes/kt1018.kt index 74cc65d8331..4749e7a755e 100644 --- a/compiler/testData/codegen/box/classes/kt1018.kt +++ b/compiler/testData/codegen/box/classes/kt1018.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // The IR prduced by FIR implicitly casts `null` of type `Nothing?` to type `Int` instead of `Int?` in `getColumnCount`. public class StockMarketTableModel() { diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt index 3e7757ac288..5a1fe4989db 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun test(s: String) = s?.length // 0 IFNULL diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall_1_4.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall_1_4.kt index 39c7d170a74..06686adedc9 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall_1_4.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/redundantSafeCall_1_4.kt @@ -1,5 +1,4 @@ // !API_VERSION: LATEST -// IGNORE_BACKEND_FIR: JVM_IR fun test(s: String) = s?.length diff --git a/compiler/testData/ir/irText/expressions/safeCalls.fir.txt b/compiler/testData/ir/irText/expressions/safeCalls.fir.txt index 2ee8971aa04..89b6504723b 100644 --- a/compiler/testData/ir/irText/expressions/safeCalls.fir.txt +++ b/compiler/testData/ir/irText/expressions/safeCalls.fir.txt @@ -158,10 +158,10 @@ FILE fqName: fileName:/safeCalls.kt FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit - BLOCK type=kotlin.Int origin=SAFE_CALL + BLOCK type=kotlin.Int? origin=SAFE_CALL VAR IR_TEMPORARY_VARIABLE name:tmp_5 type:kotlin.Int [val] CONST Int type=kotlin.Int value=42 - WHEN type=kotlin.Int origin=null + WHEN type=kotlin.Int? origin=null BRANCH if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ arg0: GET_VAR 'val tmp_5: kotlin.Int [val] declared in .box' type=kotlin.Int origin=null