From ec6ec2072863305416c17896dfcbf50820a5f0b2 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Thu, 20 Jan 2022 12:46:46 +0300 Subject: [PATCH] [FE 1.0] Fix subtyping for captured integer literal types ^KT-50877 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++++ ...irOldFrontendDiagnosticsTestGenerated.java | 6 ++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 ++++ .../box/fir/flexibleIntegerLiterals.kt | 2 ++ .../tests/platformTypes/kt50877.fir.kt | 31 +++++++++++++++++++ .../tests/platformTypes/kt50877.kt | 31 +++++++++++++++++++ .../tests/platformTypes/kt50877.txt | 19 ++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++ .../kotlin/types/AbstractTypeChecker.kt | 10 +++++- .../types/checker/ClassicTypeSystemContext.kt | 2 ++ 10 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/platformTypes/kt50877.fir.kt create mode 100644 compiler/testData/diagnostics/tests/platformTypes/kt50877.kt create mode 100644 compiler/testData/diagnostics/tests/platformTypes/kt50877.txt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index de0bb5920fe..71b78789284 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -22956,6 +22956,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/platformTypes/javaEmptyList.kt"); } + @Test + @TestMetadata("kt50877.kt") + public void testKt50877() throws Exception { + runTest("compiler/testData/diagnostics/tests/platformTypes/kt50877.kt"); + } + @Test @TestMetadata("methodTypeParameterDefaultBound.kt") public void testMethodTypeParameterDefaultBound() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 748cac748d3..6b71812da3b 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -22956,6 +22956,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/platformTypes/javaEmptyList.kt"); } + @Test + @TestMetadata("kt50877.kt") + public void testKt50877() throws Exception { + runTest("compiler/testData/diagnostics/tests/platformTypes/kt50877.kt"); + } + @Test @TestMetadata("methodTypeParameterDefaultBound.kt") public void testMethodTypeParameterDefaultBound() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 4855ac249a0..28a46cea25d 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -22956,6 +22956,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/platformTypes/javaEmptyList.kt"); } + @Test + @TestMetadata("kt50877.kt") + public void testKt50877() throws Exception { + runTest("compiler/testData/diagnostics/tests/platformTypes/kt50877.kt"); + } + @Test @TestMetadata("methodTypeParameterDefaultBound.kt") public void testMethodTypeParameterDefaultBound() throws Exception { diff --git a/compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt b/compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt index 245ef9c7c50..d9665547fc7 100644 --- a/compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt +++ b/compiler/testData/codegen/box/fir/flexibleIntegerLiterals.kt @@ -2,6 +2,8 @@ // IGNORE_BACKEND: JVM, JVM_IR, ANDROID, ANDROID_IR // IGNORE_LIGHT_ANALYSIS // WITH_STDLIB +// JVM_TARGET: 1.8 +// IGNORE_BACKEND: ANDROID // FULL_JDK // This test fails on FE 1.0, but works in production compiler (see KT-49191): diff --git a/compiler/testData/diagnostics/tests/platformTypes/kt50877.fir.kt b/compiler/testData/diagnostics/tests/platformTypes/kt50877.fir.kt new file mode 100644 index 00000000000..99d5b007510 --- /dev/null +++ b/compiler/testData/diagnostics/tests/platformTypes/kt50877.fir.kt @@ -0,0 +1,31 @@ +// FULL_JDK +// WITH_STDLIB + +// FILE: Schematic.kt +class Schematic { + var name: String? = null + + var error: String? = null + + override fun toString(): String { + return name!! + } +} + +// FILE: SortedListModel.java +import java.util.Comparator; + +public class SortedListModel { + public SortedListModel(Comparator comparator) { + } +} + + +// FILE: main.kt +val model = SortedListModel(Comparator.comparing { b1: Schematic -> + when { + b1.error != null -> 2 + b1.name!!.contains(":") -> 1 + else -> 0 + } +}.thenComparing { b1: Schematic -> b1.name!! }) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/platformTypes/kt50877.kt b/compiler/testData/diagnostics/tests/platformTypes/kt50877.kt new file mode 100644 index 00000000000..99d5b007510 --- /dev/null +++ b/compiler/testData/diagnostics/tests/platformTypes/kt50877.kt @@ -0,0 +1,31 @@ +// FULL_JDK +// WITH_STDLIB + +// FILE: Schematic.kt +class Schematic { + var name: String? = null + + var error: String? = null + + override fun toString(): String { + return name!! + } +} + +// FILE: SortedListModel.java +import java.util.Comparator; + +public class SortedListModel { + public SortedListModel(Comparator comparator) { + } +} + + +// FILE: main.kt +val model = SortedListModel(Comparator.comparing { b1: Schematic -> + when { + b1.error != null -> 2 + b1.name!!.contains(":") -> 1 + else -> 0 + } +}.thenComparing { b1: Schematic -> b1.name!! }) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/platformTypes/kt50877.txt b/compiler/testData/diagnostics/tests/platformTypes/kt50877.txt new file mode 100644 index 00000000000..df288c23c8a --- /dev/null +++ b/compiler/testData/diagnostics/tests/platformTypes/kt50877.txt @@ -0,0 +1,19 @@ +package + +public val model: SortedListModel + +public final class Schematic { + public constructor Schematic() + public final var error: kotlin.String? + public final var name: kotlin.String? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ fun toString(): kotlin.String +} + +public open class SortedListModel { + public constructor SortedListModel(/*0*/ comparator: java.util.Comparator!) + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index ebeabbe7a76..554ccb86c0f 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -22962,6 +22962,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/platformTypes/javaEmptyList.kt"); } + @Test + @TestMetadata("kt50877.kt") + public void testKt50877() throws Exception { + runTest("compiler/testData/diagnostics/tests/platformTypes/kt50877.kt"); + } + @Test @TestMetadata("methodTypeParameterDefaultBound.kt") public void testMethodTypeParameterDefaultBound() throws Exception { diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt index 170013ec5a6..4960c17caa7 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt @@ -313,8 +313,16 @@ object AbstractTypeChecker { && typeConstructor.supertypes().any { it.asSimpleType()?.isIntegerLiteralType() == true } } + fun isCapturedIntegerLiteralType(type: SimpleTypeMarker): Boolean { + if (type !is CapturedTypeMarker) return false + val projection = type.typeConstructor().projection() + return !projection.isStarProjection() && projection.getType().upperBoundIfFlexible().isIntegerLiteralType() + } + + fun isIntegerLiteralTypeOrCapturedOne(type: SimpleTypeMarker) = type.isIntegerLiteralType() || isCapturedIntegerLiteralType(type) + when { - subType.isIntegerLiteralType() && superType.isIntegerLiteralType() -> { + isIntegerLiteralTypeOrCapturedOne(subType) && isIntegerLiteralTypeOrCapturedOne(superType) -> { return true } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index 2cfcdbba650..fc3f153ad83 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -900,6 +900,8 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy override fun KotlinTypeMarker.isTypeVariableType(): Boolean { return this is UnwrappedType && constructor is NewTypeVariableConstructor } + + class WA // Workaround for KT-52313 } fun TypeVariance.convertVariance(): Variance {