From cf64687b0290179c2c0c057b8f5f082c4cd4587f Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Sat, 27 Jun 2015 14:26:03 +0300 Subject: [PATCH] More accurate error reporting with type inference error for delegated properties Add the constraints from completer if they don't lead to errors except errors from upper bounds to improve diagnostics --- .../kotlin/resolve/calls/CallCompleter.kt | 13 +++++++------ .../inference/extensionProperty.kt | 4 ++-- .../substitutions/delegationAndInference.kt | 12 ++++++++++++ .../substitutions/delegationAndInference.txt | 14 ++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++++ 5 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.kt create mode 100644 compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.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 1a0700d8119..c61593f9d4f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt @@ -32,8 +32,7 @@ import org.jetbrains.kotlin.resolve.calls.context.CheckValueArgumentsMode import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystem import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemImpl import org.jetbrains.kotlin.resolve.calls.inference.InferenceErrorData -import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.EXPECTED_TYPE_POSITION -import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.FROM_COMPLETER +import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.* import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResultsImpl import org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus @@ -132,8 +131,8 @@ public class CallCompleter( expectedType: JetType, trace: BindingTrace ) { - fun updateSystemIfSuccessful(update: (ConstraintSystem) -> Boolean) { - val copy = (getConstraintSystem() as ConstraintSystemImpl).copy() + fun updateSystemIfSuccessful(update: (ConstraintSystemImpl) -> Boolean) { + val copy = (getConstraintSystem() as ConstraintSystemImpl).copy() as ConstraintSystemImpl if (update(copy)) { setConstraintSystem(copy) } @@ -146,11 +145,13 @@ public class CallCompleter( val constraintSystemCompleter = trace[CONSTRAINT_SYSTEM_COMPLETER, getCall().getCalleeExpression()] if (constraintSystemCompleter != null) { - //todo improve error reporting with errors in constraints from completer + // todo improve error reporting with errors in constraints from completer + // todo add constraints from completer unconditionally; improve constraints from completer for generic methods + // add the constraints only if they don't lead to errors (except errors from upper bounds to improve diagnostics) updateSystemIfSuccessful { system -> constraintSystemCompleter.completeConstraintSystem(system, this) - !system.getStatus().hasOnlyErrorsFromPosition(FROM_COMPLETER.position()) + !system.filterConstraintsOut(TYPE_BOUND_POSITION).getStatus().hasOnlyErrorsDerivedFrom(FROM_COMPLETER) } } diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/extensionProperty.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/extensionProperty.kt index 6e418323021..ad3d058475b 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/extensionProperty.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/extensionProperty.kt @@ -1,10 +1,10 @@ package foo open class A { - val B.w: Int by MyProperty() + val B.w: Int by MyProperty() } -val B.r: Int by MyProperty() +val B.r: Int by MyProperty() val A.e: Int by MyProperty() diff --git a/compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.kt b/compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.kt new file mode 100644 index 00000000000..d0cad0fb02a --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.kt @@ -0,0 +1,12 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +class A(val map: MutableMap) { + + var a: String by map.withDefault1 { "foo" } +} + +fun MutableMap.get(thisRef: Any?, property: PropertyMetadata): G = throw Exception() + +fun MutableMap.set(thisRef: Any?, property: PropertyMetadata, value: S) {} + +fun MutableMap.withDefault1(default: (key: K) -> V): MutableMap = this \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.txt b/compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.txt new file mode 100644 index 00000000000..2faa56775f4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.txt @@ -0,0 +1,14 @@ +package + +internal fun kotlin.MutableMap.get(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.PropertyMetadata): G +internal fun kotlin.MutableMap.set(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.PropertyMetadata, /*2*/ value: S): kotlin.Unit +internal fun kotlin.MutableMap.withDefault1(/*0*/ default: (K) -> V): kotlin.MutableMap + +internal final class A { + public constructor A(/*0*/ map: kotlin.MutableMap) + internal final var a: kotlin.String + internal final val map: kotlin.MutableMap + 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/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index fc9cbd12a0a..23be88f7365 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -7142,6 +7142,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/inference/substitutions"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("delegationAndInference.kt") + public void testDelegationAndInference() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/substitutions/delegationAndInference.kt"); + doTest(fileName); + } + @TestMetadata("kt6081SubstituteIntoClassCorrectly.kt") public void testKt6081SubstituteIntoClassCorrectly() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/substitutions/kt6081SubstituteIntoClassCorrectly.kt");