From 51424ec59842ec826523d2c8e1db4b5b8586bf91 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Mon, 23 Dec 2019 17:10:20 +0300 Subject: [PATCH] [NI] Add constraint `K <: T` from constraint `K <: CapturedType(in T)` #KT-33263 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 5 ++++ ...ctTypeCheckerContextForConstraintSystem.kt | 18 +++++++++--- .../tests/inference/kt33263.fir.kt | 23 +++++++++++++++ .../diagnostics/tests/inference/kt33263.kt | 23 +++++++++++++++ .../diagnostics/tests/inference/kt33263.txt | 29 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 5 ++++ .../DiagnosticsUsingJavacTestGenerated.java | 5 ++++ 7 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/kt33263.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt33263.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt33263.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index 56c5f53998f..d1c0b5880c9 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -9918,6 +9918,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/inference/kt32434.kt"); } + @TestMetadata("kt33263.kt") + public void testKt33263() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt33263.kt"); + } + @TestMetadata("kt6175.kt") public void testKt6175() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/kt6175.kt"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt index afa3b16e197..0b7894c0b6a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt @@ -28,10 +28,20 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck abstract fun addLowerConstraint(typeVariable: TypeConstructorMarker, subType: KotlinTypeMarker) - override fun getLowerCapturedTypePolicy(subType: SimpleTypeMarker, superType: CapturedTypeMarker): LowerCapturedTypePolicy = when { - isMyTypeVariable(subType) -> LowerCapturedTypePolicy.SKIP_LOWER - subType.contains { it.anyBound(this::isMyTypeVariable) } -> LowerCapturedTypePolicy.CHECK_ONLY_LOWER - else -> LowerCapturedTypePolicy.CHECK_SUBTYPE_AND_LOWER + override fun getLowerCapturedTypePolicy(subType: SimpleTypeMarker, superType: CapturedTypeMarker): LowerCapturedTypePolicy { + return when { + isMyTypeVariable(subType) -> { + val projection = superType.typeConstructorProjection() + val type = projection.getType().asSimpleType() + if (projection.getVariance() == TypeVariance.IN && type != null && isMyTypeVariable(type)) { + LowerCapturedTypePolicy.CHECK_ONLY_LOWER + } else { + LowerCapturedTypePolicy.SKIP_LOWER + } + } + subType.contains { it.anyBound(this::isMyTypeVariable) } -> LowerCapturedTypePolicy.CHECK_ONLY_LOWER + else -> LowerCapturedTypePolicy.CHECK_SUBTYPE_AND_LOWER + } } /** diff --git a/compiler/testData/diagnostics/tests/inference/kt33263.fir.kt b/compiler/testData/diagnostics/tests/inference/kt33263.fir.kt new file mode 100644 index 00000000000..e68ffdd3d02 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt33263.fir.kt @@ -0,0 +1,23 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// ISSUE: KT-33263 + +class A +class Foo +class Bar + +fun Foo.create(): Bar = null!!//Bar() +//fun Foo.create(): Bar = null!!//Bar() +fun convert(bar: Bar): Bar = null!!//Bar() + +fun test(x: Any) {} +fun take(x: Bar) {} + +fun test_1(foo: Foo) { +// convert(foo.create()) + convert(foo.create()) +} + +fun test_2(foo: Foo) { + test(convert(foo.create())) +} diff --git a/compiler/testData/diagnostics/tests/inference/kt33263.kt b/compiler/testData/diagnostics/tests/inference/kt33263.kt new file mode 100644 index 00000000000..29843559173 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt33263.kt @@ -0,0 +1,23 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// ISSUE: KT-33263 + +class A +class Foo +class Bar + +fun Foo.create(): Bar = null!!//Bar() +//fun Foo.create(): Bar = null!!//Bar() +fun convert(bar: Bar): Bar = null!!//Bar() + +fun test(x: Any) {} +fun take(x: Bar) {} + +fun test_1(foo: Foo) { +// convert(foo.create()) + ")!>convert(foo.create()) +} + +fun test_2(foo: Foo) { + test(convert(foo.create())) +} diff --git a/compiler/testData/diagnostics/tests/inference/kt33263.txt b/compiler/testData/diagnostics/tests/inference/kt33263.txt new file mode 100644 index 00000000000..3258f2f018b --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt33263.txt @@ -0,0 +1,29 @@ +package + +public fun convert(/*0*/ bar: Bar): Bar +public fun take(/*0*/ x: Bar): kotlin.Unit +public fun test(/*0*/ x: kotlin.Any): kotlin.Unit +public fun test_1(/*0*/ foo: Foo): kotlin.Unit +public fun test_2(/*0*/ foo: Foo): kotlin.Unit +public fun Foo.create(): Bar + +public final class A { + public constructor A() + 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 +} + +public final class Bar { + public constructor Bar() + 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 +} + +public final class Foo { + public constructor Foo() + 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/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 8d403195310..35524c58668 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -9925,6 +9925,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/inference/kt32434.kt"); } + @TestMetadata("kt33263.kt") + public void testKt33263() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt33263.kt"); + } + @TestMetadata("kt6175.kt") public void testKt6175() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/kt6175.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 0f3d404fccd..053d687294a 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -9920,6 +9920,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inference/kt32434.kt"); } + @TestMetadata("kt33263.kt") + public void testKt33263() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt33263.kt"); + } + @TestMetadata("kt6175.kt") public void testKt6175() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/kt6175.kt");