[FIR2IR] Approximate extension receiver type after type argument substitution

Frontend may leak captured types into type arguments, so they should be
  approximated before using them in fir2ir cast inserter to determine
 a specific type from smartcast

^KT-62863 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-11-06 14:34:04 +02:00
committed by Space Team
parent d2ebaf4e6c
commit d08567c2c7
22 changed files with 261 additions and 1 deletions
@@ -51314,6 +51314,12 @@ public class LLFirBlackBoxCodegenBasedTestGenerated extends AbstractLLFirBlackBo
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
@@ -51314,6 +51314,12 @@ public class LLFirReversedBlackBoxCodegenBasedTestGenerated extends AbstractLLFi
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.ir.util.classId
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
class Fir2IrImplicitCastInserter(
private val components: Fir2IrComponents
@@ -313,7 +314,13 @@ class Fir2IrImplicitCastInserter(
receiver === extensionReceiver -> {
val extensionReceiverType = referencedDeclaration?.receiverParameter?.typeRef?.coneType ?: return null
val substitutor = createSubstitutorFromTypeArguments(selector, referencedDeclaration)
substitutor.substituteOrSelf(extensionReceiverType)
val substitutedType = substitutor.substituteOrSelf(extensionReceiverType)
// Frontend may write captured types as type arguments (by design), so we need to approximate receiver type after substitution
val approximatedType = session.typeApproximator.approximateToSuperType(
substitutedType,
TypeApproximatorConfiguration.InternalTypesApproximation
)
approximatedType ?: substitutedType
}
else -> return null
}
@@ -50733,6 +50733,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
@@ -50733,6 +50733,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
@@ -50733,6 +50733,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
@@ -0,0 +1,111 @@
FILE fqName:<root> fileName:/smartcastToStarType.kt
CLASS INTERFACE name:Base modality:ABSTRACT visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Base<T of <root>.Base>
TYPE_PARAMETER name:T index:0 variance:out superTypes:[kotlin.Any?] reified:false
FUN name:foo visibility:public modality:ABSTRACT <> ($this:<root>.Base<T of <root>.Base>) returnType:T of <root>.Base
$this: VALUE_PARAMETER name:<this> type:<root>.Base<T of <root>.Base>
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 kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type: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 kotlin.Any
$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 kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.Base<T of <root>.Derived>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Derived<T of <root>.Derived>
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
CONSTRUCTOR visibility:public <> () returnType:<root>.Derived<T of <root>.Derived> [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.Base<T of <root>.Derived>]'
FUN name:foo visibility:public modality:OPEN <> ($this:<root>.Derived<T of <root>.Derived>) returnType:T of <root>.Derived
overridden:
public abstract fun foo (): T of <root>.Base declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:<root>.Derived<T of <root>.Derived>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public open fun foo (): T of <root>.Derived declared in <root>.Derived'
TYPE_OP type=T of <root>.Derived origin=CAST typeOperand=T of <root>.Derived
CONST String type=kotlin.String value="error"
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>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type: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>.Base
$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>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:bar visibility:public modality:FINAL <T> ($receiver:<root>.Derived<T of <root>.bar>) returnType:<root>.Base<T of <root>.bar>
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
$receiver: VALUE_PARAMETER name:<this> type:<root>.Derived<T of <root>.bar>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun bar <T> (): <root>.Base<T of <root>.bar> declared in <root>'
BLOCK type=<root>.bar.<no name provided><T of <root>.bar> origin=OBJECT_LITERAL
CLASS CLASS name:<no name provided> modality:FINAL visibility:local superTypes:[<root>.Base<T of <root>.bar>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.bar.<no name provided><T of <root>.bar>
CONSTRUCTOR visibility:public <> () returnType:<root>.bar.<no name provided><T of <root>.bar> [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:<no name provided> modality:FINAL visibility:local superTypes:[<root>.Base<T of <root>.bar>]'
FUN name:foo visibility:public modality:OPEN <> ($this:<root>.bar.<no name provided><T of <root>.bar>) returnType:T of <root>.bar
overridden:
public abstract fun foo (): T of <root>.Base declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:<root>.bar.<no name provided><T of <root>.bar>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public open fun foo (): T of <root>.bar declared in <root>.bar.<no name provided>'
TYPE_OP type=T of <root>.bar origin=CAST typeOperand=T of <root>.bar
CONST String type=kotlin.String value="OK"
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>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type: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>.Base
$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>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.bar.<no name provided>' type=<root>.bar.<no name provided><T of <root>.bar> origin=OBJECT_LITERAL
FUN name:test visibility:public modality:FINAL <T> (x:<root>.Base<T of <root>.test>) returnType:T of <root>.test
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
VALUE_PARAMETER name:x index:0 type:<root>.Base<T of <root>.test>
BLOCK_BODY
WHEN type=kotlin.Unit origin=IF
BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=<root>.Derived<*>
GET_VAR 'x: <root>.Base<T of <root>.test> declared in <root>.test' type=<root>.Base<T of <root>.test> origin=null
then: BLOCK type=kotlin.Unit origin=null
VAR name:y type:<root>.Base<kotlin.Any?> [val]
CALL 'public final fun bar <T> (): <root>.Base<T of <root>.bar> declared in <root>' type=<root>.Base<kotlin.Any?> origin=null
<T>: kotlin.Any?
$receiver: TYPE_OP type=<root>.Derived<*> origin=IMPLICIT_CAST typeOperand=<root>.Derived<*>
GET_VAR 'x: <root>.Base<T of <root>.test> declared in <root>.test' type=<root>.Base<T of <root>.test> origin=null
RETURN type=kotlin.Nothing from='public final fun test <T> (x: <root>.Base<T of <root>.test>): T of <root>.test declared in <root>'
TYPE_OP type=T of <root>.test origin=CAST typeOperand=T of <root>.test
CALL 'public abstract fun foo (): T of <root>.Base declared in <root>.Base' type=kotlin.Any? origin=null
$this: GET_VAR 'val y: <root>.Base<kotlin.Any?> declared in <root>.test' type=<root>.Base<kotlin.Any?> origin=null
RETURN type=kotlin.Nothing from='public final fun test <T> (x: <root>.Base<T of <root>.test>): T of <root>.test declared in <root>'
CALL 'public abstract fun foo (): T of <root>.Base declared in <root>.Base' type=T of <root>.test origin=null
$this: GET_VAR 'x: <root>.Base<T of <root>.test> declared in <root>.test' type=<root>.Base<T of <root>.test> origin=null
FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
VAR name:x type:<root>.Derived<kotlin.String> [val]
CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.Derived' type=<root>.Derived<kotlin.String> origin=null
<class: T>: kotlin.String
RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in <root>'
CALL 'public final fun test <T> (x: <root>.Base<T of <root>.test>): T of <root>.test declared in <root>' type=kotlin.String origin=null
<T>: kotlin.String
x: GET_VAR 'val x: <root>.Derived<kotlin.String> declared in <root>.box' type=<root>.Derived<kotlin.String> origin=null
@@ -0,0 +1,29 @@
// ISSUE: KT-62863
// FIR_IDENTICAL
// DUMP_IR
interface Base<out T> {
fun foo(): T
}
class Derived<T> : Base<T> {
override fun foo(): T = "error" as T
}
fun <T> Derived<T>.bar(): Base<T> = object : Base<T> {
override fun foo(): T = "OK" as T
}
fun <T> test(x: Base<T>): T {
if (x is Derived<*>) {
val y: Base<Any?> = x.bar()
return y.foo() as T
}
return x.foo()
}
fun box(): String {
val x = Derived<String>()
return test(x)
}
@@ -47739,6 +47739,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -50733,6 +50733,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
@@ -50733,6 +50733,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
@@ -40816,6 +40816,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnImplicitDispatchReceiver.kt");
}
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@TestMetadata("toStringOnReceiverWIthSmartcast.kt")
public void testToStringOnReceiverWIthSmartcast() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/toStringOnReceiverWIthSmartcast.kt");
@@ -35109,6 +35109,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -35109,6 +35109,12 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -35109,6 +35109,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -35109,6 +35109,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -38789,6 +38789,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -39787,6 +39787,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -38291,6 +38291,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -38790,6 +38790,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -34779,6 +34779,12 @@ public class FirWasmCodegenBoxTestGenerated extends AbstractFirWasmCodegenBoxTes
runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {
@@ -34779,6 +34779,12 @@ public class K1WasmCodegenBoxTestGenerated extends AbstractK1WasmCodegenBoxTest
runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");
}
@Test
@TestMetadata("smartcastToStarType.kt")
public void testSmartcastToStarType() throws Exception {
runTest("compiler/testData/codegen/box/smartCasts/smartcastToStarType.kt");
}
@Test
@TestMetadata("whenSmartCast.kt")
public void testWhenSmartCast() throws Exception {