diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 20d295d9abc..e47e67baf7c 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -19230,6 +19230,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/integerLiterals"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform"); } + @Test + @TestMetadata("complexMapping.kt") + public void testComplexMapping() throws Exception { + runTest("compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt"); + } + @Test @TestMetadata("constantUnaryOperators.kt") public void testConstantUnaryOperators() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index 2a1d21efac6..77073b86939 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -19230,6 +19230,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/integerLiterals"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform"); } + @Test + @TestMetadata("complexMapping.kt") + public void testComplexMapping() throws Exception { + runTest("compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt"); + } + @Test @TestMetadata("constantUnaryOperators.kt") public void testConstantUnaryOperators() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index 6102c72bc16..76ef3b02281 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -19230,6 +19230,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/integerLiterals"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform"); } + @Test + @TestMetadata("complexMapping.kt") + public void testComplexMapping() throws Exception { + runTest("compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt"); + } + @Test @TestMetadata("constantUnaryOperators.kt") public void testConstantUnaryOperators() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index 094abe5e732..f39f37105fa 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -19236,6 +19236,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/integerLiterals"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform"); } + @Test + @TestMetadata("complexMapping.kt") + public void testComplexMapping() throws Exception { + runTest("compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt"); + } + @Test @TestMetadata("constantUnaryOperators.kt") public void testConstantUnaryOperators() throws Exception { diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt index 48a216ea54e..c35fbcb1a5d 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt @@ -68,17 +68,40 @@ class ResultTypeResolver( variableWithConstraints: VariableWithConstraints, direction: ResolveDirection ): KotlinTypeMarker? { - findResultIfThereIsEqualsConstraint(c, variableWithConstraints)?.let { return it } + val resultTypeFromEqualConstraint = findResultIfThereIsEqualsConstraint(c, variableWithConstraints) + if (resultTypeFromEqualConstraint != null) { + with(c) { + if (!isK2 || !resultTypeFromEqualConstraint.contains { type -> + type.typeConstructor().isIntegerLiteralConstantTypeConstructor() + } + ) { + // In K2, we don't return here ILT-based types immediately + return resultTypeFromEqualConstraint + } + } + } val subType = c.findSubType(variableWithConstraints) // Super type should be the most flexible, sub type should be the least one val superType = c.findSuperType(variableWithConstraints).makeFlexibleIfNecessary(c, variableWithConstraints.constraints) - return if (direction == ResolveDirection.TO_SUBTYPE || direction == ResolveDirection.UNKNOWN) { + val resultTypeFromDirection = if (direction == ResolveDirection.TO_SUBTYPE || direction == ResolveDirection.UNKNOWN) { c.resultType(subType, superType, variableWithConstraints) } else { c.resultType(superType, subType, variableWithConstraints) } + // In the general case, we can have here two types, one from EQUAL constraint which must be ILT-based, + // and the second one from UPPER/LOWER constraints (subType/superType based) + // The logic of choice here is: + // - if one type is null, we return another one + // - we return type from UPPER/LOWER constraints if it's more precise + // - otherwise we return ILT-based type + return when { + resultTypeFromEqualConstraint == null -> resultTypeFromDirection + resultTypeFromDirection == null -> resultTypeFromEqualConstraint + AbstractTypeChecker.isSubtypeOf(c, resultTypeFromDirection, resultTypeFromEqualConstraint) -> resultTypeFromDirection + else -> resultTypeFromEqualConstraint + } } /* diff --git a/compiler/testData/codegen/box/fir/vararg.kt b/compiler/testData/codegen/box/fir/vararg.kt index 70d0a501a8d..6b318543a72 100644 --- a/compiler/testData/codegen/box/fir/vararg.kt +++ b/compiler/testData/codegen/box/fir/vararg.kt @@ -1,8 +1,6 @@ // TARGET_BACKEND: JVM_IR // IGNORE_BACKEND_K1: JVM_IR // (type mismatch) -// IGNORE_BACKEND_K2: JVM_IR -// (CCE in select) // WITH_STDLIB fun > arrayData(vararg values: A): A = values.first() diff --git a/compiler/testData/diagnostics/tests/integerLiterals/complexMapping.fir.kt b/compiler/testData/diagnostics/tests/integerLiterals/complexMapping.fir.kt new file mode 100644 index 00000000000..46e5cb0fc9e --- /dev/null +++ b/compiler/testData/diagnostics/tests/integerLiterals/complexMapping.fir.kt @@ -0,0 +1,35 @@ +// WITH_STDLIB +// FILE: First.java + +public class First implements Comparable { + public static First compose(int something) { + return null; + } +} + +// FILE: Second.java + +public class Second implements Comparable { + public static Second compose(String something) { + return null; + } +} + +// FILE: complexMapping.kt + +private fun > range(vararg ranges: Pair): Ranges = null!! + +private abstract class Ranges> { + abstract fun > map(transform: (C) -> M): Ranges +} + +private val INF: Nothing? = null + +private val foo = listOf( + range(0 to 1) to range(INF to ""), + range(2 to 3) to range(INF to "", "" to INF), + range(4 to 5) to range("" to INF), + range(6 to INF) to range("" to INF) +).map { + it.first.map(First::compose) to it.second.map(Second::compose) +} diff --git a/compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt b/compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt new file mode 100644 index 00000000000..4c73ae0a218 --- /dev/null +++ b/compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt @@ -0,0 +1,35 @@ +// WITH_STDLIB +// FILE: First.java + +public class First implements Comparable { + public static First compose(int something) { + return null; + } +} + +// FILE: Second.java + +public class Second implements Comparable { + public static Second compose(String something) { + return null; + } +} + +// FILE: complexMapping.kt + +private fun > range(vararg ranges: Pair): Ranges = null!! + +private abstract class Ranges> { + abstract fun > map(transform: (C) -> M): Ranges +} + +private val INF: Nothing? = null + +private val foo = listOf( + range(0 to 1) to range(INF to ""), + range(2 to 3) to range(INF to "", "" to INF), + range(4 to 5) to range("" to INF), + range(6 to INF) to range("" to INF) +).map { + it.first.map(First::compose) to it.second.map(Second::compose) +} diff --git a/compiler/testData/diagnostics/tests/integerLiterals/vararg.fir.txt b/compiler/testData/diagnostics/tests/integerLiterals/vararg.fir.txt index c27fc29cce7..83cf583955b 100644 --- a/compiler/testData/diagnostics/tests/integerLiterals/vararg.fir.txt +++ b/compiler/testData/diagnostics/tests/integerLiterals/vararg.fir.txt @@ -6,9 +6,9 @@ FILE: vararg.fir.kt ^arrayDataNoBound Null(null)!! } public final fun test(b: R|kotlin/Byte|): R|kotlin/Unit| { - R|/select|(R|/arrayData|(vararg(Int(1))), R|/b|) - R|/select|(R|/id|(Int(1)), R|/b|) - R|/select|(R|/id|(R|/arrayData|(vararg(Int(1)))), R|/b|) + R|/select|(R|/arrayData|(vararg(Byte(1))), R|/b|) + R|/select|(R|/id|(Byte(1)), R|/b|) + R|/select|(R|/id|(R|/arrayData|(vararg(Byte(1)))), R|/b|) R|/select|(R|/arrayDataNoBound|(vararg(Byte(1))), R|/b|) } public final fun select(a: R|S|, b: R|S|): R|S| { 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 718f004126d..81044d6df0d 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 @@ -19236,6 +19236,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/integerLiterals"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true); } + @Test + @TestMetadata("complexMapping.kt") + public void testComplexMapping() throws Exception { + runTest("compiler/testData/diagnostics/tests/integerLiterals/complexMapping.kt"); + } + @Test @TestMetadata("constantUnaryOperators.kt") public void testConstantUnaryOperators() throws Exception {