From ba9c455ea1d5ffb96e63f0687e2477c17445e86c Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 30 Jan 2013 20:29:30 +0400 Subject: [PATCH] KT-3301 Inference with several supertypes fails #KT-3301 fixed --- .../calls/inference/ConstraintsUtil.java | 21 +++++++++++++++++++ .../tests/inference/regressions/kt3301.kt | 20 ++++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 3 files changed, 46 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt3301.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java index d79960766ef..c209a221f57 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintsUtil.java @@ -33,6 +33,12 @@ public class ConstraintsUtil { public static Set getValues(@Nullable TypeConstraints typeConstraints) { Set values = Sets.newLinkedHashSet(); if (typeConstraints != null && !typeConstraints.isEmpty()) { + if (typeConstraints.getExactBounds().size() == 1) { + if (verifyOneExactBound(typeConstraints)) { + JetType exactBound = typeConstraints.getExactBounds().iterator().next(); + return Collections.singleton(exactBound); + } + } values.addAll(typeConstraints.getExactBounds()); if (!typeConstraints.getLowerBounds().isEmpty()) { JetType superTypeOfLowerBounds = CommonSupertypes.commonSupertype(typeConstraints.getLowerBounds()); @@ -63,6 +69,21 @@ public class ConstraintsUtil { return values; } + private static boolean verifyOneExactBound(@NotNull TypeConstraints typeConstraints) { + JetType exactBound = typeConstraints.getExactBounds().iterator().next(); + for (JetType lowerBound : typeConstraints.getLowerBounds()) { + if (!JetTypeChecker.INSTANCE.isSubtypeOf(lowerBound, exactBound)) { + return false; + } + } + for (JetType upperBound : typeConstraints.getUpperBounds()) { + if (!JetTypeChecker.INSTANCE.isSubtypeOf(exactBound, upperBound)) { + return false; + } + } + return true; + } + @Nullable public static JetType getValue(@Nullable TypeConstraints typeConstraints) { //todo all checks diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt3301.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt3301.kt new file mode 100644 index 00000000000..32fcb64bfb3 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt3301.kt @@ -0,0 +1,20 @@ +//KT-3301 Inference with several supertypes fails + +package arrays + +trait A +trait B + +object CAB : A, B +object DAB : A, B + +fun m(args : Array) { + +} + +fun test122() { + m(array(CAB, DAB)) // Wrong error here: Array is inferred while expected Array is satisfied +} + +//from library +fun array(vararg t: T): Array {} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 508e71fb439..32358d8882b 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -2294,6 +2294,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/inference/regressions/kt3174.kt"); } + @TestMetadata("kt3301.kt") + public void testKt3301() throws Exception { + doTest("compiler/testData/diagnostics/tests/inference/regressions/kt3301.kt"); + } + @TestMetadata("kt702.kt") public void testKt702() throws Exception { doTest("compiler/testData/diagnostics/tests/inference/regressions/kt702.kt");