From a493125a759b0c95d990de42af786e088db1474f Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 28 Aug 2013 14:01:31 +0400 Subject: [PATCH] refactoring: fixes after review rename, removed unnecessary methods, improved test --- .../calls/inference/ConstraintsUtil.java | 31 +++++-------------- .../jetbrains/jet/lang/types/TypeUtils.java | 11 ------- .../numbers/numbersInSimpleConstraints.kt | 8 +++-- 3 files changed, 13 insertions(+), 37 deletions(-) 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 3e8a3b4a99c..d4404efb976 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 @@ -20,13 +20,12 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.intellij.openapi.util.Pair; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; -import org.jetbrains.jet.lang.resolve.constants.NumberValueTypeConstructor; import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; -import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import java.util.*; @@ -50,14 +49,14 @@ public class ConstraintsUtil { Pair, Collection> pair = TypeUtils.filterNumberTypes(typeConstraintsWithoutErrorTypes.getLowerBounds()); - Collection lowerBounds = pair.getFirst(); + Collection generalLowerBounds = pair.getFirst(); Collection numberLowerBounds = pair.getSecond(); - JetType superTypeOfLowerBounds = commonSupertype(lowerBounds); + JetType superTypeOfLowerBounds = commonSupertype(generalLowerBounds); if (trySuggestion(superTypeOfLowerBounds, typeConstraints)) { return Collections.singleton(superTypeOfLowerBounds); } - addToValuesIfDifferent(superTypeOfLowerBounds, values); + ContainerUtil.addIfNotNull(superTypeOfLowerBounds, values); Collection upperBounds = typeConstraintsWithoutErrorTypes.getUpperBounds(); for (JetType upperBound : upperBounds) { @@ -69,15 +68,13 @@ public class ConstraintsUtil { //fun foo(t: T, consumer: Consumer): T //foo(1, c: Consumer) - infer Int, not Any here - for (JetType upperBound : typeConstraintsWithoutErrorTypes.getUpperBounds()) { - addToValuesIfDifferent(upperBound, values); - } + values.addAll(typeConstraintsWithoutErrorTypes.getUpperBounds()); JetType superTypeOfNumberLowerBounds = commonSupertypeForNumberTypes(numberLowerBounds); if (trySuggestion(superTypeOfNumberLowerBounds, typeConstraints)) { return Collections.singleton(superTypeOfNumberLowerBounds); } - addToValuesIfDifferent(superTypeOfNumberLowerBounds, values); + ContainerUtil.addIfNotNull(superTypeOfNumberLowerBounds, values); if (superTypeOfLowerBounds != null && superTypeOfNumberLowerBounds != null) { JetType superTypeOfAllLowerBounds = commonSupertype(Lists.newArrayList(superTypeOfLowerBounds, superTypeOfNumberLowerBounds)); @@ -88,20 +85,6 @@ public class ConstraintsUtil { return values; } - private static void addToValuesIfDifferent(@Nullable JetType type, @NotNull Set values) { - if (type == null) return; - if (values.isEmpty()) { - values.add(type); - return; - } - for (JetType value : values) { - if (!JetTypeChecker.INSTANCE.equalTypes(type, value)) { - values.add(type); - return; - } - } - } - @Nullable private static JetType commonSupertype(@NotNull Collection lowerBounds) { if (lowerBounds.isEmpty()) return null; @@ -159,7 +142,7 @@ public class ConstraintsUtil { if (ErrorUtils.containsErrorType(type)) { values.add(type); } - else { + else if (type != null) { typeConstraintsWithoutErrorType.addBound(boundKind, type); } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java index f19d2cabc64..c239a747ded 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java @@ -637,15 +637,4 @@ public class TypeUtils { } return Pair.create(otherTypes, numberTypes); } - - @NotNull - public static JetType commonSupertypeForPossiblyNumberTypes(@NotNull Collection types) { - Pair, Collection> pair = filterNumberTypes(types); - Collection numberTypes = pair.getSecond(); - Collection otherTypes = pair.getFirst(); - if (!numberTypes.isEmpty()) { - otherTypes.add(commonSupertypeForNumberTypes(numberTypes)); - } - return CommonSupertypes.commonSupertype(otherTypes); - } } diff --git a/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt b/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt index 8f472164351..f85cb294f51 100644 --- a/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt +++ b/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt @@ -61,6 +61,10 @@ fun upperBound(t: T, l: Contr) = t fun testUpperBound(contrS: Contr, contrB: Contr, contrN: Contr) { upperBound(1, contrS) - upperBound(1, contrN) - upperBound(1, contrB) + val n = upperBound(1, contrN) + n: Number + n: Int + val b = upperBound(1, contrB) + b: Byte + b: Int }