[FIR] Add equality constraint from expected type for some synthetic function calls

This fixes some cases where we infer some type variable inside one
of the branches to Nothing instead of the expected type because Nothing
appeared in some other branch.

Specifically, we add an equality instead of a subtype constraint during
completion of calls to synthetic functions for if/when, try and !!.
We don't do it when the call contains a (possibly nested) elvis or is
inside the RHS of an assignment.
Otherwise, we would prevent some smart-casts.

#KT-65882 Fixed
This commit is contained in:
Kirill Rakhman
2024-02-21 16:06:47 +01:00
committed by Space Team
parent eaef7122f6
commit 69a7bf7f68
106 changed files with 1441 additions and 578 deletions
@@ -0,0 +1,64 @@
FILE fqName:<root> fileName:/1.kt
CLASS CLASS name:A modality:FINAL visibility:public superTypes:[<root>.Java1]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.A
CONSTRUCTOR visibility:public <> () returnType:<root>.A [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.Java1'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:A modality:FINAL visibility:public superTypes:[<root>.Java1]'
FUN FAKE_OVERRIDE name:bar visibility:public modality:OPEN <> () returnType:@[FlexibleNullability] kotlin.Any? [fake_override]
overridden:
public open fun bar (): @[FlexibleNullability] kotlin.Any? declared in <root>.Java1
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in <root>.Java1
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> (t:@[FlexibleNullability] kotlin.Any?) returnType:kotlin.Unit [fake_override]
overridden:
public open fun foo (t: @[FlexibleNullability] kotlin.Any?): kotlin.Unit declared in <root>.Java1
VALUE_PARAMETER name:t index:0 type:@[FlexibleNullability] kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in <root>.Java1
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in <root>.Java1
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS CLASS name:B modality:FINAL visibility:public superTypes:[<root>.Java1]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.B
CONSTRUCTOR visibility:public <> () returnType:<root>.B [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.Java1'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:B modality:FINAL visibility:public superTypes:[<root>.Java1]'
FUN FAKE_OVERRIDE name:bar visibility:public modality:OPEN <> () returnType:@[FlexibleNullability] kotlin.Any? [fake_override]
overridden:
public open fun bar (): @[FlexibleNullability] kotlin.Any? declared in <root>.Java1
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in <root>.Java1
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> (t:@[FlexibleNullability] kotlin.Any?) returnType:kotlin.Unit [fake_override]
overridden:
public open fun foo (t: @[FlexibleNullability] kotlin.Any?): kotlin.Unit declared in <root>.Java1
VALUE_PARAMETER name:t index:0 type:@[FlexibleNullability] kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in <root>.Java1
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in <root>.Java1
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:bar visibility:public modality:FINAL <> ($this:<root>.B) returnType:kotlin.String
$this: VALUE_PARAMETER name:<this> type:<root>.B
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun bar (): kotlin.String declared in <root>.B'
CALL 'public final fun CHECK_NOT_NULL <T0> (arg0: T0 of kotlin.internal.ir.CHECK_NOT_NULL?): {T0 of kotlin.internal.ir.CHECK_NOT_NULL & Any} declared in kotlin.internal.ir' type=kotlin.String origin=EXCLEXCL
<T0>: kotlin.Nothing
arg0: CONST Null type=kotlin.Nothing? value=null
FUN name:foo visibility:public modality:FINAL <> ($this:<root>.B, t:kotlin.String) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.B
VALUE_PARAMETER name:t index:0 type:kotlin.String
BLOCK_BODY
@@ -0,0 +1,24 @@
class A : Java1 {
constructor() /* primary */ {
super/*Java1*/()
/* <init>() */
}
}
class B : Java1 {
constructor() /* primary */ {
super/*Java1*/()
/* <init>() */
}
fun bar(): String {
return CHECK_NOT_NULL<Nothing>(arg0 = null)
}
fun foo(t: String) {
}
}
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// TARGET_BACKEND: JVM
// FILE: Java1.java