From 10c5d987d74cceb53265487c58d807d739e81566 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Fri, 8 Oct 2021 19:31:14 +0300 Subject: [PATCH] FIR: Fix exception on remaining flexible ILT in lambda ^KT-49191 Related --- .../FirBlackBoxCodegenTestGenerated.java | 6 +++++ .../fir/types/ConeIntegerLiteralTypeImpl.kt | 24 +++++++++++++++---- .../box/fir/flexibleIntegerLiterals.kt | 18 ++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 3f3c4a0813d..823d0552b82 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -16336,6 +16336,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/fir/Fir2IrClassifierStorage.kt"); } + @Test + @TestMetadata("flexibleIntegerLiterals.kt") + public void testFlexibleIntegerLiterals() throws Exception { + runTest("compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt"); + } + @Test @TestMetadata("implicitNothingInDelegate.kt") public void testImplicitNothingInDelegate() throws Exception { diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt index a1ecfed658a..336c11f266c 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt @@ -145,11 +145,27 @@ class ConeIntegerLiteralTypeImpl : ConeIntegerLiteralType { } } -fun ConeKotlinType.approximateIntegerLiteralType(expectedType: ConeKotlinType? = null): ConeKotlinType = - (this as? ConeIntegerLiteralType)?.getApproximatedType(expectedType) ?: this +fun ConeKotlinType.approximateIntegerLiteralType(expectedType: ConeKotlinType? = null): ConeKotlinType { + return when (this) { + is ConeIntegerLiteralType -> getApproximatedType(expectedType) + is ConeFlexibleType -> approximateIntegerLiteralBounds(expectedType) + else -> this + } +} -fun ConeKotlinType.approximateIntegerLiteralTypeOrNull(expectedType: ConeKotlinType? = null): ConeKotlinType? = - (this as? ConeIntegerLiteralType)?.getApproximatedType(expectedType) +private fun ConeFlexibleType.approximateIntegerLiteralBounds(expectedType: ConeKotlinType? = null): ConeFlexibleType { + val newLowerBound = lowerBound.approximateIntegerLiteralType(expectedType) + val newUpperBound = upperBound.approximateIntegerLiteralType(expectedType) + + if (newLowerBound !== lowerBound || newUpperBound !== upperBound) { + return ConeFlexibleType( + newLowerBound.lowerBoundIfFlexible(), + newUpperBound.upperBoundIfFlexible() + ) + } + + return this +} private fun ConeClassLikeType.withNullability(nullability: ConeNullability): ConeClassLikeType { if (nullability == this.nullability) return this diff --git a/compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt b/compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt new file mode 100644 index 00000000000..dc04fb006f9 --- /dev/null +++ b/compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt @@ -0,0 +1,18 @@ +// TARGET_BACKEND: JVM +// IGNORE_BACKEND: JVM, JVM_IR, ANDROID, ANDROID_IR +// IGNORE_LIGHT_ANALYSIS +// WITH_RUNTIME +// FULL_JDK + +// This test fails on FE 1.0, but works in production compiler (see KT-49191): +// Caused by: java.lang.AssertionError: Lower bound Function of a flexible type must be a subtype of the upper bound Function? +// at org.jetbrains.kotlin.types.FlexibleTypeImpl.runAssertions(flexibleTypes.kt:105) +fun box(): String { + val x = Comparator.comparing { x: String -> + 1 + } + + if (x.compare("O", "K") != 0) return "fail" + + return "OK" +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index ff43aebfa88..ec8c5e3eb86 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -16246,6 +16246,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/fir/Fir2IrClassifierStorage.kt"); } + @Test + @TestMetadata("flexibleIntegerLiterals.kt") + public void testFlexibleIntegerLiterals() throws Exception { + runTest("compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt"); + } + @Test @TestMetadata("incorrectBytecodeWithEnhancedNullability.kt") public void testIncorrectBytecodeWithEnhancedNullability() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 20d5aa2854c..076dbc5b452 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -16336,6 +16336,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fir/Fir2IrClassifierStorage.kt"); } + @Test + @TestMetadata("flexibleIntegerLiterals.kt") + public void testFlexibleIntegerLiterals() throws Exception { + runTest("compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt"); + } + @Test @TestMetadata("implicitNothingInDelegate.kt") public void testImplicitNothingInDelegate() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index fe45341986d..a92a26d058b 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -13349,6 +13349,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class Fir extends AbstractLightAnalysisModeTest { + @TestMetadata("flexibleIntegerLiterals.kt") + public void ignoreFlexibleIntegerLiterals() throws Exception { + runTest("compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt"); + } + @TestMetadata("SuspendExtension.kt") public void ignoreSuspendExtension() throws Exception { runTest("compiler/testData/codegen/box/fir/SuspendExtension.kt");