From cb25e1d55aaa9763286b76137444d62b313a553b Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 26 Sep 2013 14:41:48 +0400 Subject: [PATCH] if some type parameter has only weak constraints (from bounds), then we consider it as unknown --- .../upperBounds/doNotInferFromBoundsOnly.kt | 69 +++++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 5 ++ .../calls/inference/TypeConstraintsImpl.java | 9 +++ 3 files changed, 83 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/inference/upperBounds/doNotInferFromBoundsOnly.kt diff --git a/compiler/testData/diagnostics/tests/inference/upperBounds/doNotInferFromBoundsOnly.kt b/compiler/testData/diagnostics/tests/inference/upperBounds/doNotInferFromBoundsOnly.kt new file mode 100644 index 00000000000..885438f26c9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/upperBounds/doNotInferFromBoundsOnly.kt @@ -0,0 +1,69 @@ +package a + +trait A + +fun emptyList(): List = throw Exception() + +fun test1() { + emptyList() +} + +//-------------- + +fun emptyListOfA(): List = throw Exception() + +fun test2() { + emptyListOfA() +} + +//-------------- + +fun emptyStrangeMap(): Map = throw Exception() + +fun test3() { + emptyStrangeMap() +} + +//-------------- + +fun emptyStrangeMap1(t: T): Map = throw Exception("$t") + +fun test4() { + //todo we may infer 'Int' for 'R' here + emptyStrangeMap1(1) +} + +//-------------- + +fun emptyStrangeMap2(t: T): Map where R: A = throw Exception("$t") + +fun test5(a: A) { + //todo we may infer 'A' for 'R' here + emptyStrangeMap2(a) +} + +//-------------- + +fun emptyStrangeMap3(r: R): Map = throw Exception("$r") + +fun test6(a: A) { + emptyStrangeMap3(a) +} + +//-------------- + +fun emptyStrangeMap4(l: MutableList): Map = throw Exception("$l") + +fun test7(list: MutableList) { + emptyStrangeMap4(list) +} + +//-------------- + +fun test7() : Map = emptyStrangeMap() + +//-------------- + +fun foo(): U = throw Exception() + +fun test8(): Int = foo() diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 747a6dbf332..9d1ee1736f5 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -3537,6 +3537,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/inference/upperBounds"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("doNotInferFromBoundsOnly.kt") + public void testDoNotInferFromBoundsOnly() throws Exception { + doTest("compiler/testData/diagnostics/tests/inference/upperBounds/doNotInferFromBoundsOnly.kt"); + } + @TestMetadata("kt2856.kt") public void testKt2856() throws Exception { doTest("compiler/testData/diagnostics/tests/inference/upperBounds/kt2856.kt"); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/TypeConstraintsImpl.java b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/TypeConstraintsImpl.java index 1c7c4421095..a32fce90126 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/TypeConstraintsImpl.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/TypeConstraintsImpl.java @@ -155,6 +155,15 @@ public class TypeConstraintsImpl implements TypeConstraints { if (constraints.isEmpty()) { return Collections.emptyList(); } + boolean hasStrongConstraint = ContainerUtil.exists(constraints, new Condition() { + @Override + public boolean value(Constraint constraint) { + return constraint.constraintPosition.isStrong(); + } + }); + if (!hasStrongConstraint) { + return Collections.emptyList(); + } Set exactBounds = filterBounds(constraints, BoundKind.EXACT_BOUND, values); if (exactBounds.size() == 1) {