From d40fd440b033a8276b232094348401856bd136ba Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 16 Apr 2015 18:00:31 +0300 Subject: [PATCH] KT-7433 Type inference pushes Unit inside generic function with lower bound constraint #KT-7433 Fixed #KT-7351 Fixed --- .../kotlin/resolve/calls/CallCompleter.kt | 16 +++++++-------- .../kt7351ConstraintFromUnitExpectedType.kt | 19 ++++++++++++++++++ .../kt7351ConstraintFromUnitExpectedType.txt | 20 +++++++++++++++++++ .../tests/inference/constraints/kt7433.kt | 9 +++++++++ .../tests/inference/constraints/kt7433.txt | 4 ++++ .../checkers/JetDiagnosticsTestGenerated.java | 12 +++++++++++ 6 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.kt create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.txt create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/kt7433.kt create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/kt7433.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt index c892cd1aa17..257b0d1978a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt @@ -140,14 +140,6 @@ public class CallCompleter( val returnType = getCandidateDescriptor().getReturnType() if (returnType != null) { getConstraintSystem()!!.addSupertypeConstraint(expectedType, returnType, EXPECTED_TYPE_POSITION.position()) - - if (expectedType === TypeUtils.UNIT_EXPECTED_TYPE) { - updateSystemIfSuccessful { - system -> - system.addSupertypeConstraint(KotlinBuiltIns.getInstance().getUnitType(), returnType, EXPECTED_TYPE_POSITION.position()) - system.getStatus().isSuccessful() - } - } } val constraintSystemCompleter = trace[CONSTRAINT_SYSTEM_COMPLETER, getCall().getCalleeExpression()] @@ -162,6 +154,14 @@ public class CallCompleter( (getConstraintSystem() as ConstraintSystemImpl).processDeclaredBoundConstraints() + if (returnType != null && expectedType === TypeUtils.UNIT_EXPECTED_TYPE) { + updateSystemIfSuccessful { + system -> + system.addSupertypeConstraint(KotlinBuiltIns.getInstance().getUnitType(), returnType, EXPECTED_TYPE_POSITION.position()) + system.getStatus().isSuccessful() + } + } + setResultingSubstitutor(getConstraintSystem()!!.getResultingSubstitutor()) } diff --git a/compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.kt b/compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.kt new file mode 100644 index 00000000000..4ab6d7d1a57 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.kt @@ -0,0 +1,19 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +package kt7351 + +trait Node + +trait Source { + fun f() : T +} +fun > S.woo() : T = this.f() + +fun Node.append(block : Source.() -> Unit) { +} + +fun crashMe(node : Node) { + node.append { + woo() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.txt b/compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.txt new file mode 100644 index 00000000000..7f91377bfa1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.txt @@ -0,0 +1,20 @@ +package + +package kt7351 { + internal fun crashMe(/*0*/ node: kt7351.Node): kotlin.Unit + internal fun kt7351.Node.append(/*0*/ block: kt7351.Source.() -> kotlin.Unit): kotlin.Unit + internal fun > S.woo(): T + + internal trait Node { + 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 + } + + internal trait Source { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + internal abstract fun f(): T + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/testData/diagnostics/tests/inference/constraints/kt7433.kt b/compiler/testData/diagnostics/tests/inference/constraints/kt7433.kt new file mode 100644 index 00000000000..1c9ad2d2e1d --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/kt7433.kt @@ -0,0 +1,9 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE + +public inline fun Iterable.reduce1(operation: (S, T) -> S): S = throw Exception() + +fun test(ints: List) { + val f: () -> Unit = { + ints.reduce1 { a, b -> a + b } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/constraints/kt7433.txt b/compiler/testData/diagnostics/tests/inference/constraints/kt7433.txt new file mode 100644 index 00000000000..b7696ee16dd --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/kt7433.txt @@ -0,0 +1,4 @@ +package + +internal fun test(/*0*/ ints: kotlin.List): kotlin.Unit +kotlin.inline() public fun kotlin.Iterable.reduce1(/*0*/ operation: (S, T) -> S): S diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 1b9b3fff5ce..455a62c0f6b 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -5834,6 +5834,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("kt7351ConstraintFromUnitExpectedType.kt") + public void testKt7351ConstraintFromUnitExpectedType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/constraints/kt7351ConstraintFromUnitExpectedType.kt"); + doTest(fileName); + } + + @TestMetadata("kt7433.kt") + public void testKt7433() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/constraints/kt7433.kt"); + doTest(fileName); + } + @TestMetadata("notNullConstraintOnNullableType.kt") public void testNotNullConstraintOnNullableType() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/constraints/notNullConstraintOnNullableType.kt");