[FIR] Make result type after safe-call nullable (for nullable receiver)

This commit is contained in:
Simon Ogorodnik
2019-05-15 16:02:47 +03:00
committed by Mikhail Glukhikh
parent f7b6fec70c
commit 672c172c99
5 changed files with 22 additions and 15 deletions
@@ -202,7 +202,15 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
is FirResolvedCallableReference -> {
val symbol = newCallee.coneSymbol
if (symbol is ConeCallableSymbol) {
jump.tryCalculateReturnType(symbol.firUnsafe())
val returnType = jump.tryCalculateReturnType(symbol.firUnsafe())
if (access.safe && access.explicitReceiver!!.resultType.coneTypeUnsafe<ConeKotlinType>().isNullable) {
returnType.withReplacedConeType(
session,
returnType.coneTypeUnsafe<ConeKotlinType>().withNullability(ConeNullability.NULLABLE)
)
} else {
returnType
}
} else if (symbol is ConeClassifierSymbol) {
val firUnsafe = symbol.firUnsafe<FirElement>()
// TODO: unhack
@@ -4,9 +4,8 @@ FILE fqName:<root> fileName:/dotQualified.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun length (s: kotlin.String): kotlin.Int declared in <root>'
ERROR_CALL 'No getter found for R|kotlin/String.length|' type=kotlin.Int
FUN name:lengthN visibility:public modality:FINAL <> (s:kotlin.String?) returnType:kotlin.Int
FUN name:lengthN visibility:public modality:FINAL <> (s:kotlin.String?) returnType:kotlin.Int?
VALUE_PARAMETER name:s index:0 type:kotlin.String?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun lengthN (s: kotlin.String?): kotlin.Int declared in <root>'
ERROR_CALL 'No getter found for R|kotlin/String.length|' type=kotlin.Int
RETURN type=kotlin.Nothing from='public final fun lengthN (s: kotlin.String?): kotlin.Int? declared in <root>'
ERROR_CALL 'No getter found for R|kotlin/String.length|' type=kotlin.Int?
+4 -4
View File
@@ -41,19 +41,19 @@ FILE fqName:<root> fileName:/kt30020.kt
ERROR_CALL 'Unresolved reference: R|<local>/<complex-set>|' type=IrErrorType
VAR name:<complex-set> type:kotlin.collections.MutableList<kotlin.Any> [val]
BLOCK type=kotlin.collections.MutableList<kotlin.Any> origin=EXCLEXCL
VAR name:<bangbang> type:kotlin.collections.MutableList<kotlin.Any> [val]
CALL 'public abstract fun <get-xs> (): kotlin.collections.MutableList<kotlin.Any> declared in <root>.X' type=kotlin.collections.MutableList<kotlin.Any> origin=null
VAR name:<bangbang> type:kotlin.collections.MutableList<kotlin.Any>? [val]
CALL 'public abstract fun <get-xs> (): kotlin.collections.MutableList<kotlin.Any> declared in <root>.X' type=kotlin.collections.MutableList<kotlin.Any>? origin=null
$this: GET_VAR 'nx: <root>.X? declared in <root>.test' type=<root>.X? origin=null
WHEN type=kotlin.collections.MutableList<kotlin.Any> origin=EXCLEXCL
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 <bangbang>: kotlin.collections.MutableList<kotlin.Any> [val] declared in <root>.test' type=kotlin.collections.MutableList<kotlin.Any> origin=null
arg0: GET_VAR 'val <bangbang>: kotlin.collections.MutableList<kotlin.Any>? [val] declared in <root>.test' type=kotlin.collections.MutableList<kotlin.Any>? origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: THROW type=kotlin.Nothing
CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.KotlinNullPointerException' type=kotlin.KotlinNullPointerException origin=null
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: GET_VAR 'val <bangbang>: kotlin.collections.MutableList<kotlin.Any> [val] declared in <root>.test' type=kotlin.collections.MutableList<kotlin.Any> origin=null
then: GET_VAR 'val <bangbang>: kotlin.collections.MutableList<kotlin.Any>? [val] declared in <root>.test' type=kotlin.collections.MutableList<kotlin.Any>? origin=null
ERROR_CALL 'Unresolved reference: R|<local>/<complex-set>|' type=IrErrorType
VAR name:<complex-set> type:kotlin.collections.MutableList<kotlin.Any> [val]
BLOCK type=kotlin.collections.MutableList<kotlin.Any> origin=EXCLEXCL
@@ -45,10 +45,10 @@ FILE fqName:test fileName:/safeCallWithIncrementDecrement.kt
FUN name:testProperty visibility:public modality:FINAL <> (nc:test.C?) returnType:kotlin.Unit
VALUE_PARAMETER name:nc index:0 type:test.C?
BLOCK_BODY
VAR name:<unary> type:kotlin.Int [val]
CALL 'public final fun <get-p> (): kotlin.Int declared in test' type=kotlin.Int origin=null
VAR name:<unary> type:kotlin.Int? [val]
CALL 'public final fun <get-p> (): kotlin.Int declared in test' type=kotlin.Int? origin=null
ERROR_CALL 'Unresolved reference: R|test/p|' type=IrErrorType
GET_VAR 'val <unary>: kotlin.Int [val] declared in test.testProperty' type=kotlin.Int origin=null
GET_VAR 'val <unary>: kotlin.Int? [val] declared in test.testProperty' type=kotlin.Int? origin=null
FUN name:testArrayAccess visibility:public modality:FINAL <> (nc:test.C?) returnType:kotlin.Unit
VALUE_PARAMETER name:nc index:0 type:test.C?
BLOCK_BODY
+3 -3
View File
@@ -58,11 +58,11 @@ FILE fqName:<root> fileName:/safeCalls.kt
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:test1 visibility:public modality:FINAL <> (x:kotlin.String?) returnType:kotlin.Int
FUN name:test1 visibility:public modality:FINAL <> (x:kotlin.String?) returnType:kotlin.Int?
VALUE_PARAMETER name:x index:0 type:kotlin.String?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test1 (x: kotlin.String?): kotlin.Int declared in <root>'
ERROR_CALL 'No getter found for R|kotlin/String.length|' type=kotlin.Int
RETURN type=kotlin.Nothing from='public final fun test1 (x: kotlin.String?): kotlin.Int? declared in <root>'
ERROR_CALL 'No getter found for R|kotlin/String.length|' type=kotlin.Int?
FUN name:test2 visibility:public modality:FINAL <> (x:kotlin.String?) returnType:IrErrorType
VALUE_PARAMETER name:x index:0 type:kotlin.String?
BLOCK_BODY