[FIR] Allow nullable receiver type in case of safe-calls

This commit is contained in:
Simon Ogorodnik
2019-05-15 15:53:07 +03:00
committed by Mikhail Glukhikh
parent b28207deca
commit 27a1ec3817
12 changed files with 92 additions and 37 deletions
@@ -32,6 +32,7 @@ fun resolveArgumentExpression(
expectedTypeRef: FirTypeRef,
sink: CheckerSink,
isReceiver: Boolean,
isSafeCall: Boolean,
acceptLambdaAtoms: (PostponedResolvedAtomMarker) -> Unit,
typeProvider: (FirExpression) -> FirTypeRef?
) {
@@ -42,6 +43,7 @@ fun resolveArgumentExpression(
expectedType,
sink,
isReceiver,
isSafeCall,
typeProvider
)
// TODO:!
@@ -57,6 +59,7 @@ fun resolveArgumentExpression(
expectedTypeRef,
sink,
isReceiver,
isSafeCall,
acceptLambdaAtoms,
typeProvider
)
@@ -67,10 +70,11 @@ fun resolveArgumentExpression(
expectedTypeRef,
sink,
isReceiver,
isSafeCall,
acceptLambdaAtoms,
typeProvider
)
else -> resolvePlainExpressionArgument(csBuilder, argument, expectedType, sink, isReceiver, typeProvider)
else -> resolvePlainExpressionArgument(csBuilder, argument, expectedType, sink, isReceiver, isSafeCall, typeProvider)
}
}
@@ -80,11 +84,12 @@ fun resolvePlainExpressionArgument(
expectedType: ConeKotlinType?,
sink: CheckerSink,
isReceiver: Boolean,
isSafeCall: Boolean,
typeProvider: (FirExpression) -> FirTypeRef?
) {
if (expectedType == null) return
val argumentType = typeProvider(argument)?.coneTypeSafe<ConeKotlinType>() ?: return
resolvePlainArgumentType(csBuilder, argumentType, expectedType, sink, isReceiver)
resolvePlainArgumentType(csBuilder, argumentType, expectedType, sink, isReceiver, isSafeCall)
}
fun resolvePlainArgumentType(
@@ -92,12 +97,20 @@ fun resolvePlainArgumentType(
argumentType: ConeKotlinType,
expectedType: ConeKotlinType,
sink: CheckerSink,
isReceiver: Boolean
isReceiver: Boolean,
isSafeCall: Boolean
) {
val position = SimpleConstraintSystemConstraintPosition //TODO
val nullableExpectedType = expectedType.withNullability(ConeNullability.NULLABLE)
if (isReceiver && isSafeCall) {
if (!csBuilder.addSubtypeConstraintIfCompatible(argumentType, nullableExpectedType, position)) {
sink.reportApplicability(CandidateApplicability.WRONG_RECEIVER) // TODO
}
return
}
if (!csBuilder.addSubtypeConstraintIfCompatible(argumentType, expectedType, position)) {
val nullableExpectedType = expectedType.withNullability(ConeNullability.NULLABLE)
if (!isReceiver) {
if (!csBuilder.addSubtypeConstraintIfCompatible(argumentType, nullableExpectedType, position)) {
csBuilder.addSubtypeConstraint(argumentType, expectedType, position)
@@ -119,6 +132,7 @@ internal fun Candidate.resolveArgument(
argument: FirExpression,
parameter: FirValueParameter,
isReceiver: Boolean,
isSafeCall: Boolean,
typeProvider: (FirExpression) -> FirTypeRef?,
sink: CheckerSink
) {
@@ -131,6 +145,7 @@ internal fun Candidate.resolveArgument(
parameter.returnTypeRef,
sink,
isReceiver,
isSafeCall,
{ this.postponedAtoms += it },
typeProvider
)
@@ -36,6 +36,7 @@ class CallInfo(
val explicitReceiver: FirExpression?,
val arguments: List<FirExpression>,
val isSafeCall: Boolean,
val typeArguments: List<FirTypeProjection>,
val session: FirSession,
@@ -109,6 +109,7 @@ class PostponedArgumentsAnalyzer(
lambda.atom.returnTypeRef, // TODO: proper ref
checkerSink,
false,
false,
{ atom = it },
typeProvider
)
@@ -103,10 +103,15 @@ internal sealed class CheckReceivers : ResolutionStage() {
if (receiverParameterValue != null) {
if (explicitReceiverExpression != null && explicitReceiverKind.shouldBeResolvedAsExplicit()) {
resolveArgumentExpression(
candidate.csBuilder, explicitReceiverExpression,
candidate.csBuilder,
explicitReceiverExpression,
candidate.substitutor.substituteOrSelf(receiverParameterValue.type),
explicitReceiverExpression.typeRef,
sink, isReceiver = true, typeProvider = callInfo.typeProvider, acceptLambdaAtoms = { candidate.postponedAtoms += it }
sink,
isReceiver = true,
isSafeCall = callInfo.isSafeCall,
typeProvider = callInfo.typeProvider,
acceptLambdaAtoms = { candidate.postponedAtoms += it }
)
}
}
@@ -132,7 +137,14 @@ internal object CheckArguments : CheckerStage() {
val argumentMapping =
candidate.argumentMapping ?: throw IllegalStateException("Argument should be already mapped while checking arguments!")
for ((argument, parameter) in argumentMapping) {
candidate.resolveArgument(argument, parameter, isReceiver = false, typeProvider = callInfo.typeProvider, sink = sink)
candidate.resolveArgument(
argument,
parameter,
isReceiver = false,
isSafeCall = false,
typeProvider = callInfo.typeProvider,
sink = sink
)
}
if (candidate.system.hasContradiction) {
@@ -248,7 +248,16 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
val receiver = qualifiedAccess.explicitReceiver?.transformSingle(this, noExpectedType)
val info = CallInfo(CallKind.VariableAccess, receiver, emptyList(), emptyList(), session, file, container!!) { it.resultType }
val info = CallInfo(
CallKind.VariableAccess,
receiver,
emptyList(),
qualifiedAccess.safe,
emptyList(),
session,
file,
container!!
) { it.resultType }
val resolver = CallResolver(jump, inferenceComponents)
resolver.callInfo = info
resolver.scopes = (scopes + localScopes).asReversed()
@@ -358,7 +367,16 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
val arguments = functionCall.arguments
val typeArguments = functionCall.typeArguments
val info = CallInfo(CallKind.Function, explicitReceiver, arguments, typeArguments, session, file, container!!) { it.resultType }
val info = CallInfo(
CallKind.Function,
explicitReceiver,
arguments,
functionCall.safe,
typeArguments,
session,
file,
container!!
) { it.resultType }
val resolver = CallResolver(jump, inferenceComponents)
resolver.callInfo = info
resolver.scopes = (scopes + localScopes).asReversed()
+9 -9
View File
@@ -16,21 +16,21 @@ FILE fqName:<root> fileName:/bangbang.kt
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: GET_VAR 'val <bangbang>: kotlin.Any? [val] declared in <root>.test1' type=kotlin.Any? origin=null
FUN name:test2 visibility:public modality:FINAL <> (a:kotlin.Any?) returnType:IrErrorType
FUN name:test2 visibility:public modality:FINAL <> (a:kotlin.Any?) returnType:kotlin.Int
VALUE_PARAMETER name:a index:0 type:kotlin.Any?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test2 (a: kotlin.Any?): IrErrorType declared in <root>'
BLOCK type=IrErrorType origin=EXCLEXCL
VAR name:<bangbang> type:IrErrorType [val]
ERROR_CALL 'Unresolved reference: <Inapplicable(WRONG_RECEIVER): [kotlin/Any.hashCode]>#' type=IrErrorType
WHEN type=IrErrorType origin=EXCLEXCL
RETURN type=kotlin.Nothing from='public final fun test2 (a: kotlin.Any?): kotlin.Int declared in <root>'
BLOCK type=kotlin.Int origin=EXCLEXCL
VAR name:<bangbang> type:kotlin.Int [val]
CALL 'public open fun hashCode (): kotlin.Int declared in kotlin.Any' type=kotlin.Int origin=null
$this: GET_VAR 'a: kotlin.Any? declared in <root>.test2' type=kotlin.Any? origin=null
WHEN type=kotlin.Int 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>: IrErrorType [val] declared in <root>.test2' type=IrErrorType origin=null
arg0: GET_VAR 'val <bangbang>: kotlin.Int [val] declared in <root>.test2' type=kotlin.Int origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: THROW type=kotlin.Nothing
ERROR_CALL 'Unresolved reference: <Unresolved name: KotlinNullPointerException>#' type=IrErrorType
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: GET_VAR 'val <bangbang>: IrErrorType [val] declared in <root>.test2' type=IrErrorType origin=null
then: GET_VAR 'val <bangbang>: kotlin.Int [val] declared in <root>.test2' type=kotlin.Int origin=null
@@ -28,9 +28,12 @@ FILE fqName:<root> fileName:/chainOfSafeCalls.kt
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:test visibility:public modality:FINAL <> (nc:<root>.C?) returnType:IrErrorType
FUN name:test visibility:public modality:FINAL <> (nc:<root>.C?) returnType:<root>.C
VALUE_PARAMETER name:nc index:0 type:<root>.C?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test (nc: <root>.C?): IrErrorType declared in <root>'
ERROR_CALL 'Unresolved reference: <Unresolved name: foo>#' type=IrErrorType
RETURN type=kotlin.Nothing from='public final fun test (nc: <root>.C?): <root>.C declared in <root>'
CALL 'public final fun foo (): <root>.C declared in <root>.C' type=<root>.C origin=null
$this: CALL 'public final fun foo (): <root>.C declared in <root>.C' type=<root>.C origin=null
$this: CALL 'public final fun bar (): <root>.C? declared in <root>.C' type=<root>.C? origin=null
$this: CALL 'public final fun foo (): <root>.C declared in <root>.C' type=<root>.C origin=null
$this: GET_VAR 'nc: <root>.C? declared in <root>.test' type=<root>.C? origin=null
+8 -7
View File
@@ -55,20 +55,21 @@ FILE fqName:<root> fileName:/kt30020.kt
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
ERROR_CALL 'Unresolved reference: R|<local>/<complex-set>|' type=IrErrorType
VAR name:<complex-set> type:IrErrorType [val]
BLOCK type=IrErrorType origin=EXCLEXCL
VAR name:<bangbang> type:IrErrorType [val]
ERROR_CALL 'Unresolved reference: <Inapplicable(WRONG_RECEIVER): [/X.f]>#' type=IrErrorType
WHEN type=IrErrorType origin=EXCLEXCL
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 f (): 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>: IrErrorType [val] declared in <root>.test' type=IrErrorType 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>: IrErrorType [val] declared in <root>.test' type=IrErrorType 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
FUN name:testExtensionReceiver visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
@@ -31,7 +31,8 @@ FILE fqName:test fileName:/safeCallWithIncrementDecrement.kt
FUN name:inc visibility:public modality:FINAL <> () returnType:kotlin.Int?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun inc (): kotlin.Int? declared in test'
CALL 'public final fun inc (): kotlin.Int? declared in test' type=kotlin.Int? origin=null
CALL 'public final fun inc (): kotlin.Int declared in kotlin.Int' type=kotlin.Int origin=null
$this: ERROR_CALL 'Unresolved reference: this#' type=kotlin.Int?
FUN name:get visibility:public modality:FINAL <> (index:kotlin.Int) returnType:kotlin.Int
VALUE_PARAMETER name:index index:0 type:kotlin.Int
BLOCK_BODY
+6 -5
View File
@@ -67,24 +67,25 @@ FILE fqName:<root> fileName:/safeCalls.kt
VALUE_PARAMETER name:x index:0 type:kotlin.String?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test2 (x: kotlin.String?): IrErrorType declared in <root>'
ERROR_CALL 'Unresolved reference: <Inapplicable(WRONG_RECEIVER): [kotlin/Any.hashCode, kotlin/Any.hashCode]>#' type=IrErrorType
ERROR_CALL 'Unresolved reference: <Ambiguity: hashCode, [kotlin/Any.hashCode, kotlin/Any.hashCode]>#' type=IrErrorType
FUN name:test3 visibility:public modality:FINAL <> (x:kotlin.String?, y:kotlin.Any?) returnType:IrErrorType
VALUE_PARAMETER name:x index:0 type:kotlin.String?
VALUE_PARAMETER name:y index:1 type:kotlin.Any?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test3 (x: kotlin.String?, y: kotlin.Any?): IrErrorType declared in <root>'
ERROR_CALL 'Unresolved reference: <Inapplicable(WRONG_RECEIVER): [kotlin/Any.equals, kotlin/Any.equals]>#' type=IrErrorType
ERROR_CALL 'Unresolved reference: <Ambiguity: equals, [kotlin/Any.equals, kotlin/Any.equals]>#' type=IrErrorType
GET_VAR 'y: kotlin.Any? declared in <root>.test3' type=kotlin.Any? origin=null
FUN name:test4 visibility:public modality:FINAL <> (x:<root>.Ref?) returnType:kotlin.Unit
VALUE_PARAMETER name:x index:0 type:<root>.Ref?
BLOCK_BODY
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Int visibility:public ' type=kotlin.Int origin=null
value: CONST Int type=kotlin.Int value=0
FUN name:test5 visibility:public modality:FINAL <> (s:kotlin.String?) returnType:IrErrorType
FUN name:test5 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 test5 (s: kotlin.String?): IrErrorType declared in <root>'
ERROR_CALL 'Unresolved reference: <Inapplicable(WRONG_RECEIVER): [/IHost.extLength]>#' type=IrErrorType
RETURN type=kotlin.Nothing from='public final fun test5 (s: kotlin.String?): kotlin.Int declared in <root>'
CALL 'public open fun extLength (): kotlin.Int declared in <root>.IHost' type=kotlin.Int origin=null
$this: GET_VAR 's: kotlin.String? declared in <root>.test5' type=kotlin.String? origin=null
FUN name:foo visibility:public modality:FINAL <> () returnType:kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.Int declared in <root>'
@@ -31,7 +31,8 @@ FILE fqName:<root> fileName:/temporaryInEnumEntryInitializer.kt
CONSTRUCTOR visibility:public <> () returnType:<root>.En.ENTRY [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'private constructor <init> (x: kotlin.String?) [primary] declared in <root>.En'
x: CALL 'public final fun toString (): kotlin.String declared in kotlin' type=kotlin.String origin=null
x: CALL 'public open fun toString (): kotlin.String declared in kotlin.Any' type=kotlin.String origin=null
$this: CALL 'public final fun <get-n> (): kotlin.Any? declared in <root>' type=kotlin.Any? origin=null
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_ENTRY name:ENTRY modality:FINAL visibility:public superTypes:[<root>.En]'
FUN FAKE_OVERRIDE name:clone visibility:protected modality:FINAL <> ($this:kotlin.Enum) returnType:kotlin.Any
overridden:
@@ -18,7 +18,8 @@ FILE fqName:<root> fileName:/temporaryInInitBlock.kt
ANONYMOUS_INITIALIZER isStatic=false
BLOCK_BODY
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:s type:kotlin.String? visibility:public [final] ' type=kotlin.String? origin=null
value: CALL 'public final fun toString (): kotlin.String declared in kotlin' type=kotlin.String origin=null
value: CALL 'public open fun toString (): kotlin.String declared in kotlin.Any' type=kotlin.String origin=null
$this: GET_VAR 'x: kotlin.Any? declared in <root>.C.<init>' type=kotlin.Any? origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.Any