From 8da5f6b95563dc3a0cd6ed5e6eae42bb907c57ff Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 23 Apr 2019 12:03:13 +0300 Subject: [PATCH] IR: fixes after rebase --- .../kotlin/fir/Fir2IrTextTestGenerated.java | 15 ++ .../kotlin/ir/types/IrTypeCheckerContext.kt | 27 +++ .../kotlin/ir/types/IrTypeSystemContext.kt | 15 +- .../org/jetbrains/kotlin/ir/util/IrUtils.kt | 1 + .../ir/irText/types/asOnPlatformType.kt | 24 +++ .../ir/irText/types/asOnPlatformType.txt | 37 +++++ .../ir/irText/types/intersectionType1_OI.txt | 8 +- .../ir/irText/types/intersectionType2_OI.txt | 4 +- .../localVariableOfIntersectionType_NI.txt | 27 +-- .../smartCastOnFieldReceiverOfGenericType.kt | 16 ++ .../smartCastOnFieldReceiverOfGenericType.txt | 27 +++ .../types/smartCastOnReceiverOfGenericType.kt | 33 ++++ .../smartCastOnReceiverOfGenericType.txt | 156 ++++++++++++++++++ .../kotlin/ir/IrTextTestCaseGenerated.java | 15 ++ 14 files changed, 381 insertions(+), 24 deletions(-) create mode 100644 compiler/testData/ir/irText/types/asOnPlatformType.kt create mode 100644 compiler/testData/ir/irText/types/asOnPlatformType.txt create mode 100644 compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.kt create mode 100644 compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.txt create mode 100644 compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt create mode 100644 compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.txt diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java index d9be7d8ea50..975447d93c5 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java @@ -1606,6 +1606,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irText/types"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("asOnPlatformType.kt") + public void testAsOnPlatformType() throws Exception { + runTest("compiler/testData/ir/irText/types/asOnPlatformType.kt"); + } + @TestMetadata("explicitEqualsAndCompareToCallsOnPlatformTypeReceiver.kt") public void testExplicitEqualsAndCompareToCallsOnPlatformTypeReceiver() throws Exception { runTest("compiler/testData/ir/irText/types/explicitEqualsAndCompareToCallsOnPlatformTypeReceiver.kt"); @@ -1650,5 +1655,15 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { public void testNullabilityAssertionOnExtensionReceiver() throws Exception { runTest("compiler/testData/ir/irText/types/nullabilityAssertionOnExtensionReceiver.kt"); } + + @TestMetadata("smartCastOnFieldReceiverOfGenericType.kt") + public void testSmartCastOnFieldReceiverOfGenericType() throws Exception { + runTest("compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.kt"); + } + + @TestMetadata("smartCastOnReceiverOfGenericType.kt") + public void testSmartCastOnReceiverOfGenericType() throws Exception { + runTest("compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt"); + } } } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeCheckerContext.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeCheckerContext.kt index 5f060083e08..476ec34d99b 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeCheckerContext.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeCheckerContext.kt @@ -12,9 +12,14 @@ import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection import org.jetbrains.kotlin.types.AbstractTypeCheckerContext import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.SimpleTypeMarker +import org.jetbrains.kotlin.types.model.TypeArgumentMarker import org.jetbrains.kotlin.types.model.TypeConstructorMarker class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSystemContext, AbstractTypeCheckerContext() { + + override fun anyType(): SimpleTypeMarker = + irBuiltIns.anyType as IrSimpleType + override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform { require(type is IrSimpleType) val parameters = extractTypeParameters((type.classifier as IrClassSymbol).owner).map { it.symbol } @@ -60,4 +65,26 @@ class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSystemCo // TODO remove 'Exact' annotation only return removeAnnotations() } + + override fun KotlinTypeMarker.isUninferredParameter(): Boolean = false + + override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? = + error("Captured type is unsupported in IR") + + override fun SimpleTypeMarker.isPrimitiveType(): Boolean { + // TODO this is currently used in overload resolution only + return false + } + + override fun KotlinTypeMarker.argumentsCount(): Int = + when (this) { + is IrSimpleType -> arguments.size + else -> 0 + } + + override fun KotlinTypeMarker.getArgument(index: Int): TypeArgumentMarker = + when (this) { + is IrSimpleType -> arguments[index] + else -> error("Type $this has no arguments") + } } \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt index 9e43f363b9a..256b9f39d13 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt @@ -65,13 +65,6 @@ interface IrTypeSystemContext : TypeSystemInferenceExtensionContext { override fun SimpleTypeMarker.typeConstructor() = (this as IrSimpleType).classifier - override fun SimpleTypeMarker.argumentsCount(): Int = (this as IrSimpleType).arguments.size - - override fun SimpleTypeMarker.getArgument(index: Int): TypeArgumentMarker { - val simpleType = this as IrSimpleType - return simpleType.arguments[index] - } - override fun KotlinTypeMarker.asTypeArgument() = this as IrTypeArgument override fun CapturedTypeMarker.lowerType(): KotlinTypeMarker? = error("Captured Type is not valid for IrTypes") @@ -328,6 +321,14 @@ interface IrTypeSystemContext : TypeSystemInferenceExtensionContext { @Suppress("UNCHECKED_CAST") override fun intersectTypes(types: List): KotlinTypeMarker = makeTypeIntersection(types as List) + + override fun TypeConstructorMarker.isCapturedTypeConstructor(): Boolean = false + + override fun createErrorTypeWithCustomConstructor(debugName: String, constructor: TypeConstructorMarker): KotlinTypeMarker = + TODO("IrTypeSystemContext doesn't support constraint system resolution") + + override fun CapturedTypeMarker.captureStatus(): CaptureStatus = + error("Captured type is unsupported in IR") } fun extractTypeParameters(klass: IrDeclarationParent): List { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index da04ce3b1c9..6e377ba4750 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe diff --git a/compiler/testData/ir/irText/types/asOnPlatformType.kt b/compiler/testData/ir/irText/types/asOnPlatformType.kt new file mode 100644 index 00000000000..ec2c514501d --- /dev/null +++ b/compiler/testData/ir/irText/types/asOnPlatformType.kt @@ -0,0 +1,24 @@ +// FILE: asOnPlatformType.kt +fun test() { + val nullStr = JavaClass.nullString() + val nonnullStr = JavaClass.nonnullString() + + nullStr.foo() + nonnullStr.foo() + nullStr.fooN() + nonnullStr.fooN() +} + +inline fun T.foo(): T = this as T +inline fun T.fooN(): T? = this as T? + +// FILE: JavaClass.java +public class JavaClass { + public static String nullString() { + return null; + } + + public static String nonnullString() { + return "OK"; + } +} diff --git a/compiler/testData/ir/irText/types/asOnPlatformType.txt b/compiler/testData/ir/irText/types/asOnPlatformType.txt new file mode 100644 index 00000000000..97aeb566a43 --- /dev/null +++ b/compiler/testData/ir/irText/types/asOnPlatformType.txt @@ -0,0 +1,37 @@ +FILE fqName: fileName:/asOnPlatformType.kt + FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit + BLOCK_BODY + VAR name:nullStr type:kotlin.String? [val] + CALL 'public open fun nullString (): kotlin.String? declared in .JavaClass' type=kotlin.String? origin=null + VAR name:nonnullStr type:kotlin.String? [val] + CALL 'public open fun nonnullString (): kotlin.String? declared in .JavaClass' type=kotlin.String? origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public final fun foo (): T of .foo [inline] declared in ' type=kotlin.String? origin=null + : kotlin.String? + $receiver: GET_VAR 'val nullStr: kotlin.String? [val] declared in .test' type=kotlin.String? origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public final fun foo (): T of .foo [inline] declared in ' type=kotlin.String? origin=null + : kotlin.String? + $receiver: GET_VAR 'val nonnullStr: kotlin.String? [val] declared in .test' type=kotlin.String? origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public final fun fooN (): T of .fooN? [inline] declared in ' type=kotlin.String? origin=null + : kotlin.String? + $receiver: GET_VAR 'val nullStr: kotlin.String? [val] declared in .test' type=kotlin.String? origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public final fun fooN (): T of .fooN? [inline] declared in ' type=kotlin.String? origin=null + : kotlin.String? + $receiver: GET_VAR 'val nonnullStr: kotlin.String? [val] declared in .test' type=kotlin.String? origin=null + FUN name:foo visibility:public modality:FINAL ($receiver:T of .foo) returnType:T of .foo [inline] + TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] + $receiver: VALUE_PARAMETER name: type:T of .foo + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun foo (): T of .foo [inline] declared in ' + TYPE_OP type=T of .foo origin=CAST typeOperand=T of .foo + GET_VAR ': T of .foo declared in .foo' type=T of .foo origin=null + FUN name:fooN visibility:public modality:FINAL ($receiver:T of .fooN) returnType:T of .fooN? [inline] + TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] + $receiver: VALUE_PARAMETER name: type:T of .fooN + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun fooN (): T of .fooN? [inline] declared in ' + TYPE_OP type=T of .fooN? origin=CAST typeOperand=T of .fooN? + GET_VAR ': T of .fooN declared in .fooN' type=T of .fooN origin=null diff --git a/compiler/testData/ir/irText/types/intersectionType1_OI.txt b/compiler/testData/ir/irText/types/intersectionType1_OI.txt index 09495d38c42..bc693b29242 100644 --- a/compiler/testData/ir/irText/types/intersectionType1_OI.txt +++ b/compiler/testData/ir/irText/types/intersectionType1_OI.txt @@ -57,14 +57,14 @@ FILE fqName: fileName:/intersectionType1_OI.kt CALL 'public final fun arrayOf (vararg elements: T of kotlin.arrayOf): kotlin.Array [inline] declared in kotlin' type=kotlin.Array<.In> origin=null : .In elements: VARARG type=kotlin.Array.In> varargElementType=.In - CALL 'public constructor () [primary] declared in .In' type=.In origin=null - : kotlin.Int + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .In' type=.In origin=null + : kotlin.Int VAR name:a2 type:kotlin.Array<.In> [val] CALL 'public final fun arrayOf (vararg elements: T of kotlin.arrayOf): kotlin.Array [inline] declared in kotlin' type=kotlin.Array<.In> origin=null : .In elements: VARARG type=kotlin.Array.In> varargElementType=.In - CALL 'public constructor () [primary] declared in .In' type=.In origin=null - : kotlin.String + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .In' type=.In origin=null + : kotlin.String TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit CALL 'public final fun foo (a: kotlin.Array<.In.foo>>, b: kotlin.Array<.In>): kotlin.Boolean declared in ' type=kotlin.Boolean origin=null : kotlin.Int diff --git a/compiler/testData/ir/irText/types/intersectionType2_OI.txt b/compiler/testData/ir/irText/types/intersectionType2_OI.txt index adf82196ff3..af12abf7316 100644 --- a/compiler/testData/ir/irText/types/intersectionType2_OI.txt +++ b/compiler/testData/ir/irText/types/intersectionType2_OI.txt @@ -90,9 +90,9 @@ FILE fqName: fileName:/intersectionType2_OI.kt FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:kotlin.Any BLOCK_BODY VAR name:mm type:.B [val] - CALL 'public constructor () [primary] declared in .B' type=.B origin=null + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .B' type=.B origin=null VAR name:nn type:.C [val] - CALL 'public constructor () [primary] declared in .C' type=.C origin=null + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .C' type=.C origin=null VAR name:c type:kotlin.Any [val] WHEN type=kotlin.Any origin=IF BRANCH diff --git a/compiler/testData/ir/irText/types/localVariableOfIntersectionType_NI.txt b/compiler/testData/ir/irText/types/localVariableOfIntersectionType_NI.txt index 7377f55eaf3..b80763b22ac 100644 --- a/compiler/testData/ir/irText/types/localVariableOfIntersectionType_NI.txt +++ b/compiler/testData/ir/irText/types/localVariableOfIntersectionType_NI.txt @@ -100,24 +100,29 @@ FILE fqName: fileName:/localVariableOfIntersectionType_NI.kt $this: CALL 'public abstract fun create (x: .In.Z.create>, y: .In.Z.create>): .Inv.Z.create> declared in .Z' type=.Inv origin=null : kotlin.Any $this: GET_VAR 'z: .Z declared in .test' type=.Z origin=null - x: GET_VAR 'a: .In<.IA> declared in .test' type=.In<.IA> origin=null - y: GET_VAR 'b: .In<.IB> declared in .test' type=.In<.IB> origin=null + x: TYPE_OP type=.In origin=IMPLICIT_CAST typeOperand=.In + GET_VAR 'a: .In<.IA> declared in .test' type=.In<.IA> origin=null + y: TYPE_OP type=.In origin=IMPLICIT_CAST typeOperand=.In + GET_VAR 'b: .In<.IB> declared in .test' type=.In<.IB> origin=null CALL 'public abstract fun bar (): kotlin.Unit declared in .IB' type=kotlin.Unit origin=null $this: TYPE_OP type=.IB origin=IMPLICIT_CAST typeOperand=.IB CALL 'public abstract fun (): T of .Inv declared in .Inv' type=kotlin.Any origin=GET_PROPERTY $this: CALL 'public abstract fun create (x: .In.Z.create>, y: .In.Z.create>): .Inv.Z.create> declared in .Z' type=.Inv origin=null : kotlin.Any $this: GET_VAR 'z: .Z declared in .test' type=.Z origin=null - x: GET_VAR 'a: .In<.IA> declared in .test' type=.In<.IA> origin=null - y: GET_VAR 'b: .In<.IB> declared in .test' type=.In<.IB> origin=null + x: TYPE_OP type=.In origin=IMPLICIT_CAST typeOperand=.In + GET_VAR 'a: .In<.IA> declared in .test' type=.In<.IA> origin=null + y: TYPE_OP type=.In origin=IMPLICIT_CAST typeOperand=.In + GET_VAR 'b: .In<.IB> declared in .test' type=.In<.IB> origin=null VAR name:t type:kotlin.Any [val] - TYPE_OP type=kotlin.Any origin=IMPLICIT_CAST typeOperand=kotlin.Any - CALL 'public abstract fun (): T of .Inv declared in .Inv' type=kotlin.Any origin=GET_PROPERTY - $this: CALL 'public abstract fun create (x: .In.Z.create>, y: .In.Z.create>): .Inv.Z.create> declared in .Z' type=.Inv origin=null - : kotlin.Any - $this: GET_VAR 'z: .Z declared in .test' type=.Z origin=null - x: GET_VAR 'a: .In<.IA> declared in .test' type=.In<.IA> origin=null - y: GET_VAR 'b: .In<.IB> declared in .test' type=.In<.IB> origin=null + CALL 'public abstract fun (): T of .Inv declared in .Inv' type=kotlin.Any origin=GET_PROPERTY + $this: CALL 'public abstract fun create (x: .In.Z.create>, y: .In.Z.create>): .Inv.Z.create> declared in .Z' type=.Inv origin=null + : kotlin.Any + $this: GET_VAR 'z: .Z declared in .test' type=.Z origin=null + x: TYPE_OP type=.In origin=IMPLICIT_CAST typeOperand=.In + GET_VAR 'a: .In<.IA> declared in .test' type=.In<.IA> origin=null + y: TYPE_OP type=.In origin=IMPLICIT_CAST typeOperand=.In + GET_VAR 'b: .In<.IB> declared in .test' type=.In<.IB> origin=null CALL 'public abstract fun foo (): kotlin.Unit declared in .IA' type=kotlin.Unit origin=null $this: TYPE_OP type=.IA origin=IMPLICIT_CAST typeOperand=.IA GET_VAR 'val t: kotlin.Any [val] declared in .test' type=kotlin.Any origin=null diff --git a/compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.kt b/compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.kt new file mode 100644 index 00000000000..6469e3cdfa0 --- /dev/null +++ b/compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.kt @@ -0,0 +1,16 @@ +// FILE: smartCastOnFieldReceiverOfGenericType.kt +fun testSetField(a: Any, b: Any) { + a as JCell + b as String + a.value = b +} + +fun testGetField(a: Any): String { + a as JCell + return a.value +} + +// FILE: JCell.java +public class JCell { + public T value; +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.txt b/compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.txt new file mode 100644 index 00000000000..d0e74226598 --- /dev/null +++ b/compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.txt @@ -0,0 +1,27 @@ +FILE fqName: fileName:/smartCastOnFieldReceiverOfGenericType.kt + FUN name:testSetField visibility:public modality:FINAL <> (a:kotlin.Any, b:kotlin.Any) returnType:kotlin.Unit + VALUE_PARAMETER name:a index:0 type:kotlin.Any + VALUE_PARAMETER name:b index:1 type:kotlin.Any + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=.JCell origin=CAST typeOperand=.JCell + GET_VAR 'a: kotlin.Any declared in .testSetField' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.String origin=CAST typeOperand=kotlin.String + GET_VAR 'b: kotlin.Any declared in .testSetField' type=kotlin.Any origin=null + SET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:value type:T of .JCell? visibility:public ' type=kotlin.Unit origin=EQ + receiver: TYPE_OP type=.JCell origin=IMPLICIT_CAST typeOperand=.JCell + GET_VAR 'a: kotlin.Any declared in .testSetField' type=kotlin.Any origin=null + value: TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String + GET_VAR 'b: kotlin.Any declared in .testSetField' type=kotlin.Any origin=null + FUN name:testGetField visibility:public modality:FINAL <> (a:kotlin.Any) returnType:kotlin.String + VALUE_PARAMETER name:a index:0 type:kotlin.Any + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=.JCell origin=CAST typeOperand=.JCell + GET_VAR 'a: kotlin.Any declared in .testGetField' type=kotlin.Any origin=null + RETURN type=kotlin.Nothing from='public final fun testGetField (a: kotlin.Any): kotlin.String declared in ' + TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String + GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:value type:T of .JCell? visibility:public ' type=kotlin.String? origin=GET_PROPERTY + receiver: TYPE_OP type=.JCell origin=IMPLICIT_CAST typeOperand=.JCell + GET_VAR 'a: kotlin.Any declared in .testGetField' type=kotlin.Any origin=null diff --git a/compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt b/compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt new file mode 100644 index 00000000000..e0f25b39d5a --- /dev/null +++ b/compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt @@ -0,0 +1,33 @@ +fun testFunction(a: Any, b: Any) { + a as MutableList + b as String + a.add(b) +} + +fun testProperty(a: Any, b: Any) { + a as Cell + b as String + a.value = b +} + +fun testInnerClass(a: Any, b: Any, c: Any) { + a as Outer.Inner + b as Int + c as String + a.use(b, c) +} + +fun testNonSubstitutedTypeParameter(a: Any, b: Any) { + a as MutableList> + b as List + a.add(b) +} + +class Cell(var value: T) + +class Outer { + inner class Inner { + fun use(x1: T1, x2: T2) {} + } +} + diff --git a/compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.txt b/compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.txt new file mode 100644 index 00000000000..abf2d2a80bf --- /dev/null +++ b/compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.txt @@ -0,0 +1,156 @@ +FILE fqName: fileName:/smartCastOnReceiverOfGenericType.kt + FUN name:testFunction visibility:public modality:FINAL <> (a:kotlin.Any, b:kotlin.Any) returnType:kotlin.Unit + VALUE_PARAMETER name:a index:0 type:kotlin.Any + VALUE_PARAMETER name:b index:1 type:kotlin.Any + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.collections.MutableList origin=CAST typeOperand=kotlin.collections.MutableList + GET_VAR 'a: kotlin.Any declared in .testFunction' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.String origin=CAST typeOperand=kotlin.String + GET_VAR 'b: kotlin.Any declared in .testFunction' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public abstract fun add (element: E of kotlin.collections.MutableList): kotlin.Boolean declared in kotlin.collections.MutableList' type=kotlin.Boolean origin=null + $this: TYPE_OP type=kotlin.collections.MutableList origin=IMPLICIT_CAST typeOperand=kotlin.collections.MutableList + GET_VAR 'a: kotlin.Any declared in .testFunction' type=kotlin.Any origin=null + element: TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String + GET_VAR 'b: kotlin.Any declared in .testFunction' type=kotlin.Any origin=null + FUN name:testProperty visibility:public modality:FINAL <> (a:kotlin.Any, b:kotlin.Any) returnType:kotlin.Unit + VALUE_PARAMETER name:a index:0 type:kotlin.Any + VALUE_PARAMETER name:b index:1 type:kotlin.Any + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=.Cell origin=CAST typeOperand=.Cell + GET_VAR 'a: kotlin.Any declared in .testProperty' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.String origin=CAST typeOperand=kotlin.String + GET_VAR 'b: kotlin.Any declared in .testProperty' type=kotlin.Any origin=null + CALL 'public final fun (: T of .Cell): kotlin.Unit declared in .Cell' type=kotlin.Unit origin=EQ + $this: TYPE_OP type=.Cell origin=IMPLICIT_CAST typeOperand=.Cell + GET_VAR 'a: kotlin.Any declared in .testProperty' type=kotlin.Any origin=null + : TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String + GET_VAR 'b: kotlin.Any declared in .testProperty' type=kotlin.Any origin=null + FUN name:testInnerClass visibility:public modality:FINAL <> (a:kotlin.Any, b:kotlin.Any, c:kotlin.Any) returnType:kotlin.Unit + VALUE_PARAMETER name:a index:0 type:kotlin.Any + VALUE_PARAMETER name:b index:1 type:kotlin.Any + VALUE_PARAMETER name:c index:2 type:kotlin.Any + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=.Outer.Inner origin=CAST typeOperand=.Outer.Inner + GET_VAR 'a: kotlin.Any declared in .testInnerClass' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.Int origin=CAST typeOperand=kotlin.Int + GET_VAR 'b: kotlin.Any declared in .testInnerClass' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.String origin=CAST typeOperand=kotlin.String + GET_VAR 'c: kotlin.Any declared in .testInnerClass' type=kotlin.Any origin=null + CALL 'public final fun use (x1: T1 of .Outer, x2: T2 of .Outer.Inner): kotlin.Unit declared in .Outer.Inner' type=kotlin.Unit origin=null + $this: TYPE_OP type=.Outer.Inner origin=IMPLICIT_CAST typeOperand=.Outer.Inner + GET_VAR 'a: kotlin.Any declared in .testInnerClass' type=kotlin.Any origin=null + x1: TYPE_OP type=kotlin.Int origin=IMPLICIT_CAST typeOperand=kotlin.Int + GET_VAR 'b: kotlin.Any declared in .testInnerClass' type=kotlin.Any origin=null + x2: TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String + GET_VAR 'c: kotlin.Any declared in .testInnerClass' type=kotlin.Any origin=null + FUN name:testNonSubstitutedTypeParameter visibility:public modality:FINAL (a:kotlin.Any, b:kotlin.Any) returnType:kotlin.Unit + TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] + VALUE_PARAMETER name:a index:0 type:kotlin.Any + VALUE_PARAMETER name:b index:1 type:kotlin.Any + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.collections.MutableList.testNonSubstitutedTypeParameter>> origin=CAST typeOperand=kotlin.collections.MutableList.testNonSubstitutedTypeParameter>> + GET_VAR 'a: kotlin.Any declared in .testNonSubstitutedTypeParameter' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + TYPE_OP type=kotlin.collections.List.testNonSubstitutedTypeParameter> origin=CAST typeOperand=kotlin.collections.List.testNonSubstitutedTypeParameter> + GET_VAR 'b: kotlin.Any declared in .testNonSubstitutedTypeParameter' type=kotlin.Any origin=null + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CALL 'public abstract fun add (element: E of kotlin.collections.MutableList): kotlin.Boolean declared in kotlin.collections.MutableList' type=kotlin.Boolean origin=null + $this: TYPE_OP type=kotlin.collections.MutableList.testNonSubstitutedTypeParameter>> origin=IMPLICIT_CAST typeOperand=kotlin.collections.MutableList.testNonSubstitutedTypeParameter>> + GET_VAR 'a: kotlin.Any declared in .testNonSubstitutedTypeParameter' type=kotlin.Any origin=null + element: TYPE_OP type=kotlin.collections.List.testNonSubstitutedTypeParameter> origin=IMPLICIT_CAST typeOperand=kotlin.collections.List.testNonSubstitutedTypeParameter> + GET_VAR 'b: kotlin.Any declared in .testNonSubstitutedTypeParameter' type=kotlin.Any origin=null + CLASS CLASS name:Cell modality:FINAL visibility:public superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Cell.Cell> + TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] + CONSTRUCTOR visibility:public <> (value:T of .Cell) returnType:.Cell.Cell> [primary] + VALUE_PARAMETER name:value index:0 type:T of .Cell + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Cell modality:FINAL visibility:public superTypes:[kotlin.Any]' + PROPERTY name:value visibility:public modality:FINAL [var] + FIELD PROPERTY_BACKING_FIELD name:value type:T of .Cell visibility:public + EXPRESSION_BODY + GET_VAR 'value: T of .Cell declared in .Cell.' type=T of .Cell origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Cell.Cell>) returnType:T of .Cell + correspondingProperty: PROPERTY name:value visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.Cell.Cell> + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): T of .Cell declared in .Cell' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:value type:T of .Cell visibility:public ' type=T of .Cell origin=null + receiver: GET_VAR ': .Cell.Cell> declared in .Cell.' type=.Cell.Cell> origin=null + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Cell.Cell>, :T of .Cell) returnType:kotlin.Unit + correspondingProperty: PROPERTY name:value visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.Cell.Cell> + VALUE_PARAMETER name: index:0 type:T of .Cell + BLOCK_BODY + SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:value type:T of .Cell visibility:public ' type=kotlin.Unit origin=null + receiver: GET_VAR ': .Cell.Cell> declared in .Cell.' type=.Cell.Cell> origin=null + value: GET_VAR ': T of .Cell declared in .Cell.' type=T of .Cell 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 + $this: VALUE_PARAMETER name: 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 + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + CLASS CLASS name:Outer modality:FINAL visibility:public superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Outer.Outer> + TYPE_PARAMETER name:T1 index:0 variance: superTypes:[kotlin.Any?] + CONSTRUCTOR visibility:public <> () returnType:.Outer.Outer> [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Outer modality:FINAL visibility:public superTypes:[kotlin.Any]' + CLASS CLASS name:Inner modality:FINAL visibility:public [inner] superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Outer.Inner.Outer.Inner, T1 of .Outer> + TYPE_PARAMETER name:T2 index:0 variance: superTypes:[kotlin.Any?] + CONSTRUCTOR visibility:public <> ($this:.Outer.Outer>) returnType:.Outer.Inner.Outer.Inner, T1 of .Outer> [primary] + $outer: VALUE_PARAMETER name: type:.Outer.Outer> + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Inner modality:FINAL visibility:public [inner] superTypes:[kotlin.Any]' + FUN name:use visibility:public modality:FINAL <> ($this:.Outer.Inner.Outer.Inner, T1 of .Outer>, x1:T1 of .Outer, x2:T2 of .Outer.Inner) returnType:kotlin.Unit + $this: VALUE_PARAMETER name: type:.Outer.Inner.Outer.Inner, T1 of .Outer> + VALUE_PARAMETER name:x1 index:0 type:T1 of .Outer + VALUE_PARAMETER name:x2 index:1 type:T2 of .Outer.Inner + BLOCK_BODY + 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 + $this: VALUE_PARAMETER name: 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 + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + 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 + $this: VALUE_PARAMETER name: 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 + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index 18b09557331..3d8cf884cc6 100644 --- a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -1606,6 +1606,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irText/types"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("asOnPlatformType.kt") + public void testAsOnPlatformType() throws Exception { + runTest("compiler/testData/ir/irText/types/asOnPlatformType.kt"); + } + @TestMetadata("explicitEqualsAndCompareToCallsOnPlatformTypeReceiver.kt") public void testExplicitEqualsAndCompareToCallsOnPlatformTypeReceiver() throws Exception { runTest("compiler/testData/ir/irText/types/explicitEqualsAndCompareToCallsOnPlatformTypeReceiver.kt"); @@ -1650,5 +1655,15 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { public void testNullabilityAssertionOnExtensionReceiver() throws Exception { runTest("compiler/testData/ir/irText/types/nullabilityAssertionOnExtensionReceiver.kt"); } + + @TestMetadata("smartCastOnFieldReceiverOfGenericType.kt") + public void testSmartCastOnFieldReceiverOfGenericType() throws Exception { + runTest("compiler/testData/ir/irText/types/smartCastOnFieldReceiverOfGenericType.kt"); + } + + @TestMetadata("smartCastOnReceiverOfGenericType.kt") + public void testSmartCastOnReceiverOfGenericType() throws Exception { + runTest("compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt"); + } } }