[FIR] Make type of safe call always nullable

^KT-46860 In Progress
This commit is contained in:
Dmitriy Novozhilov
2021-06-21 17:34:21 +03:00
committed by teamcityserver
parent f18e38a49f
commit 09994ee8ea
9 changed files with 18 additions and 14 deletions
@@ -3,4 +3,4 @@
fun test() {
val foo: String? = null
foo?.<!REDUNDANT_CALL_OF_CONVERSION_METHOD!>toString()<!>
}
}
@@ -1,4 +1,4 @@
// WITH_RUNTIME
val s: String? = ""
val empty = s?.<!USELESS_CALL_ON_NOT_NULL!>isNullOrEmpty()<!>
val empty = s?.<!USELESS_CALL_ON_NOT_NULL!>isNullOrEmpty()<!>
@@ -1,4 +1,4 @@
// WITH_RUNTIME
val list1: List<Int>? = listOf(1)
val list = list1?.<!USELESS_CALL_ON_NOT_NULL!>orEmpty()<!>
val list = list1?.<!USELESS_CALL_ON_NOT_NULL!>orEmpty()<!>
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.resolve
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.functions.FunctionClassKind
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.*
@@ -343,8 +344,11 @@ fun FirSafeCallExpression.propagateTypeFromQualifiedAccessAfterNullCheck(
) {
val receiverType = nullableReceiverExpression.typeRef.coneTypeSafe<ConeKotlinType>()
val typeAfterNullCheck = regularQualifiedAccess.expressionTypeOrUnitForAssignment() ?: return
val isReceiverActuallyNullable = receiverType != null && session.typeContext.run { receiverType.isNullableType() }
val isReceiverActuallyNullable = if (session.languageVersionSettings.supportsFeature(LanguageFeature.SafeCallsAreAlwaysNullable)) {
true
} else {
receiverType != null && session.typeContext.run { receiverType.isNullableType() }
}
val resultingType =
if (isReceiverActuallyNullable)
typeAfterNullCheck.withNullability(ConeNullability.NULLABLE, session.typeContext)