From 102d5c7d5a5c3024688ee3df58bd16ebd739dbdb Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 19 Feb 2020 16:41:08 +0300 Subject: [PATCH] [NI] Fix detecting expected type variable for postponed atoms #KT-36819 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 5 +++ .../KotlinConstraintSystemCompleter.kt | 6 ++-- .../tests/inference/kt36819.fir.kt | 18 ++++++++++ .../diagnostics/tests/inference/kt36819.kt | 18 ++++++++++ .../diagnostics/tests/inference/kt36819.txt | 33 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 5 +++ .../DiagnosticsUsingJavacTestGenerated.java | 5 +++ 7 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/kt36819.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt36819.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt36819.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 5936be1f315..e62f6a935dc 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -10101,6 +10101,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/inference/kt35702.kt"); } + @TestMetadata("kt36819.kt") + public void testKt36819() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt36819.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/KotlinConstraintSystemCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt index 41b87963266..e5de822c5d6 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt @@ -167,9 +167,11 @@ class KotlinConstraintSystemCompleter( val builtIns = (variable as TypeVariableTypeConstructor).builtIns val csBuilder = (c as NewConstraintSystemImpl).getBuilder() + + val expectedTypeVariable = postponedAtom.expectedType?.constructor?.takeIf { it in c.allTypeVariables } ?: variable val atomToAnalyze = when (postponedAtom) { is PostponedCallableReferenceAtom -> postponedAtom.preparePostponedAtomWithTypeVariableAsExpectedType( - c, csBuilder, variable, + c, csBuilder, expectedTypeVariable, condition = { true }, isSuitable = KotlinType::isBuiltinFunctionalTypeOrSubtype, typeVariableCreator = { TypeVariableForCallableReferenceReturnType(builtIns, "_Q") }, @@ -180,7 +182,7 @@ class KotlinConstraintSystemCompleter( } ) is LambdaWithTypeVariableAsExpectedTypeAtom -> postponedAtom.preparePostponedAtomWithTypeVariableAsExpectedType( - c, csBuilder, variable, + c, csBuilder, expectedTypeVariable, condition = { it.atom.parametersTypes?.all { type -> type != null } != true }, isSuitable = KotlinType::isBuiltinFunctionalType, typeVariableCreator = { TypeVariableForLambdaReturnType(postponedAtom.atom, builtIns, "_R") }, diff --git a/compiler/testData/diagnostics/tests/inference/kt36819.fir.kt b/compiler/testData/diagnostics/tests/inference/kt36819.fir.kt new file mode 100644 index 00000000000..3967aae18fb --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt36819.fir.kt @@ -0,0 +1,18 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE +// ISSUE: KT-36819 + +fun select(vararg x: K) = x[0] +interface A +class B: A +class C: A +fun id1(x: T): T = x +fun id2(x: R): R = x + +class Out(x: R) + +fun main() { + val x1 = select(id1 { B() }, id2 { C() }) + val x2 = select({ B() }, { C() }) // OK, CST = () -> A + val x3 = select(id1(Out(B())), id2(Out(C()))) // OK, CST = Out +} diff --git a/compiler/testData/diagnostics/tests/inference/kt36819.kt b/compiler/testData/diagnostics/tests/inference/kt36819.kt new file mode 100644 index 00000000000..7157864aecb --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt36819.kt @@ -0,0 +1,18 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE +// ISSUE: KT-36819 + +fun select(vararg x: K) = x[0] +interface A +class B: A +class C: A +fun id1(x: T): T = x +fun id2(x: R): R = x + +class Out(x: R) + +fun main() { + val x1 = select(id1 { B() }, id2 { C() }) + val x2 = select({ B() }, { C() }) // OK, CST = () -> A + val x3 = select(id1(Out(B())), id2(Out(C()))) // OK, CST = Out +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/kt36819.txt b/compiler/testData/diagnostics/tests/inference/kt36819.txt new file mode 100644 index 00000000000..b323db87317 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt36819.txt @@ -0,0 +1,33 @@ +package + +public fun id1(/*0*/ x: T): T +public fun id2(/*0*/ x: R): R +public fun main(): kotlin.Unit +public fun select(/*0*/ vararg x: K /*kotlin.Array*/): K + +public interface 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 B : A { + public constructor B() + 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 C : A { + public constructor C() + 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 Out { + public constructor Out(/*0*/ x: R) + 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 674adaa1dce..d7e085da3e5 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -10108,6 +10108,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/inference/kt35702.kt"); } + @TestMetadata("kt36819.kt") + public void testKt36819() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt36819.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 b6f8b7b4e98..38bee4b8325 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -10103,6 +10103,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inference/kt35702.kt"); } + @TestMetadata("kt36819.kt") + public void testKt36819() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt36819.kt"); + } + @TestMetadata("kt6175.kt") public void testKt6175() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/kt6175.kt");