From a084bcbbb5e6e1f606cadb88e31f365c1389ffcf Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 25 Jan 2023 15:45:29 +0200 Subject: [PATCH] [FIR] Expand expected types before ILT approximation ^KT-56176 Fixed --- .../runners/ir/Fir2IrTextTestGenerated.java | 6 ++++++ .../ir/LightTreeFir2IrTextTestGenerated.java | 6 ++++++ ...rCallCompletionResultsWriterTransformer.kt | 2 +- ...eralAndOperatorApproximationTransformer.kt | 5 +++-- ...iteralWithExpectedTypealiasType.fir.ir.txt | 20 +++++++++++++++++++ ...iteralWithExpectedTypealiasType.fir.kt.txt | 11 ++++++++++ ...gerLiteralWithExpectedTypealiasType.ir.txt | 20 +++++++++++++++++++ ...integerLiteralWithExpectedTypealiasType.kt | 9 +++++++++ ...gerLiteralWithExpectedTypealiasType.kt.txt | 11 ++++++++++ .../test/runners/ir/IrTextTestGenerated.java | 6 ++++++ .../klib/KlibTextTestCaseGenerated.java | 5 +++++ 11 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.ir.txt create mode 100644 compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.kt.txt create mode 100644 compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.ir.txt create mode 100644 compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt create mode 100644 compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt.txt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java index a7a018823aa..4b9b710e596 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java @@ -2648,6 +2648,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { runTest("compiler/testData/ir/irText/firProblems/InnerClassInAnonymous.kt"); } + @Test + @TestMetadata("integerLiteralWithExpectedTypealiasType.kt") + public void testIntegerLiteralWithExpectedTypealiasType() throws Exception { + runTest("compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt"); + } + @Test @TestMetadata("internalPotentialFakeOverride.kt") public void testInternalPotentialFakeOverride() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java index ebb35a19a98..7286e4db1b8 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java @@ -2648,6 +2648,12 @@ public class LightTreeFir2IrTextTestGenerated extends AbstractLightTreeFir2IrTex runTest("compiler/testData/ir/irText/firProblems/InnerClassInAnonymous.kt"); } + @Test + @TestMetadata("integerLiteralWithExpectedTypealiasType.kt") + public void testIntegerLiteralWithExpectedTypealiasType() throws Exception { + runTest("compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt"); + } + @Test @TestMetadata("internalPotentialFakeOverride.kt") public void testInternalPotentialFakeOverride() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt index ab71ab8788e..67fd9745e82 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt @@ -695,7 +695,7 @@ class FirCallCompletionResultsWriterTransformer( val finalType = finalSubstitutor.substituteOrNull(initialType) var resultType = block.resultType.withReplacedConeType(finalType) resultType.coneTypeSafe()?.let { - resultType = resultType.resolvedTypeFromPrototype(it.getApproximatedType(data?.getExpectedType(block))) + resultType = resultType.resolvedTypeFromPrototype(it.getApproximatedType(data?.getExpectedType(block)?.fullyExpandedType(session))) } block.replaceTypeRef(resultType) session.lookupTracker?.recordTypeResolveAsLookup(resultType, block.source, context.file.source) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt index 28b42554a0e..19a4b158d96 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralAndOperatorApproximationTransformer.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference import org.jetbrains.kotlin.fir.references.builder.buildResolvedErrorReference import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference import org.jetbrains.kotlin.fir.resolve.ScopeSession +import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.scope import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype @@ -64,7 +65,7 @@ class IntegerLiteralAndOperatorApproximationTransformer( data: ConeKotlinType? ): FirStatement { val type = constExpression.resultType.coneTypeSafe() ?: return constExpression - val approximatedType = type.getApproximatedType(data) + val approximatedType = type.getApproximatedType(data?.fullyExpandedType(session)) constExpression.resultType = constExpression.resultType.resolvedTypeFromPrototype(approximatedType) @Suppress("UNCHECKED_CAST") val kind = approximatedType.toConstKind() as ConstantValueKind @@ -79,7 +80,7 @@ class IntegerLiteralAndOperatorApproximationTransformer( @Suppress("UnnecessaryVariable") val call = integerLiteralOperatorCall val operatorType = call.resultType.coneTypeSafe() ?: return call - val approximatedType = operatorType.getApproximatedType(data) + val approximatedType = operatorType.getApproximatedType(data?.fullyExpandedType(session)) call.transformDispatchReceiver(this, null) call.transformExtensionReceiver(this, null) call.argumentList.transformArguments(this, null) diff --git a/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.ir.txt b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.ir.txt new file mode 100644 index 00000000000..57af48119e7 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.ir.txt @@ -0,0 +1,20 @@ +FILE fqName: fileName:/integerLiteralWithExpectedTypealiasType.kt + TYPEALIAS name:MyLong visibility:public expandedType:kotlin.Long + FUN name:foo visibility:public modality:FINAL <> (l:kotlin.Long) returnType:kotlin.String + VALUE_PARAMETER name:l index:0 type:kotlin.Long + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun foo (l: kotlin.Long): kotlin.String declared in ' + WHEN type=kotlin.String origin=IF + 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 'l: kotlin.Long declared in .foo' type=kotlin.Long origin=null + arg1: CONST Long type=kotlin.Long value=0 + then: CONST String type=kotlin.String value="OK" + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST String type=kotlin.String value="fail" + FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CALL 'public final fun foo (l: kotlin.Long): kotlin.String declared in ' type=kotlin.String origin=null + l: CONST Long type=kotlin.Long value=0 diff --git a/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.kt.txt b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.kt.txt new file mode 100644 index 00000000000..cba78f4a034 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.fir.kt.txt @@ -0,0 +1,11 @@ +typealias MyLong = Long +fun foo(l: Long): String { + return when { + EQEQ(arg0 = l, arg1 = 0L) -> "OK" + else -> "fail" + } +} + +fun box(): String { + return foo(l = 0L) +} diff --git a/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.ir.txt b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.ir.txt new file mode 100644 index 00000000000..12d8a7a90c8 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.ir.txt @@ -0,0 +1,20 @@ +FILE fqName: fileName:/integerLiteralWithExpectedTypealiasType.kt + TYPEALIAS name:MyLong visibility:public expandedType:kotlin.Long + FUN name:foo visibility:public modality:FINAL <> (l:kotlin.Long{ .MyLong }) returnType:kotlin.String + VALUE_PARAMETER name:l index:0 type:kotlin.Long{ .MyLong } + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun foo (l: kotlin.Long{ .MyLong }): kotlin.String declared in ' + WHEN type=kotlin.String origin=IF + 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 'l: kotlin.Long{ .MyLong } declared in .foo' type=kotlin.Long{ .MyLong } origin=null + arg1: CONST Long type=kotlin.Long value=0 + then: CONST String type=kotlin.String value="OK" + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST String type=kotlin.String value="fail" + FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CALL 'public final fun foo (l: kotlin.Long{ .MyLong }): kotlin.String declared in ' type=kotlin.String origin=null + l: CONST Long type=kotlin.Long value=0 diff --git a/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt new file mode 100644 index 00000000000..98c230c7b71 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt @@ -0,0 +1,9 @@ +// ISSUE: KT-56176 + +typealias MyLong = Long + +fun foo(l: MyLong): String { + return if (l == 0L) "OK" else "fail" +} + +fun box(): String = foo(0) diff --git a/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt.txt b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt.txt new file mode 100644 index 00000000000..cba78f4a034 --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt.txt @@ -0,0 +1,11 @@ +typealias MyLong = Long +fun foo(l: Long): String { + return when { + EQEQ(arg0 = l, arg1 = 0L) -> "OK" + else -> "fail" + } +} + +fun box(): String { + return foo(l = 0L) +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java index 2cc0545e526..f52bda5d828 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java @@ -2648,6 +2648,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest { runTest("compiler/testData/ir/irText/firProblems/InnerClassInAnonymous.kt"); } + @Test + @TestMetadata("integerLiteralWithExpectedTypealiasType.kt") + public void testIntegerLiteralWithExpectedTypealiasType() throws Exception { + runTest("compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt"); + } + @Test @TestMetadata("internalPotentialFakeOverride.kt") public void testInternalPotentialFakeOverride() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/klib/KlibTextTestCaseGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/klib/KlibTextTestCaseGenerated.java index 125832d5d03..a403752361e 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/klib/KlibTextTestCaseGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/klib/KlibTextTestCaseGenerated.java @@ -1950,6 +1950,11 @@ public class KlibTextTestCaseGenerated extends AbstractKlibTextTestCase { runTest("compiler/testData/ir/irText/firProblems/InnerClassInAnonymous.kt"); } + @TestMetadata("integerLiteralWithExpectedTypealiasType.kt") + public void testIntegerLiteralWithExpectedTypealiasType() throws Exception { + runTest("compiler/testData/ir/irText/firProblems/integerLiteralWithExpectedTypealiasType.kt"); + } + @TestMetadata("kt43342.kt") public void testKt43342() throws Exception { runTest("compiler/testData/ir/irText/firProblems/kt43342.kt");