refactoring: fixes after review
rename, removed unnecessary methods, improved test
This commit is contained in:
+7
-24
@@ -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<JetType>, Collection<JetType>> pair =
|
||||
TypeUtils.filterNumberTypes(typeConstraintsWithoutErrorTypes.getLowerBounds());
|
||||
Collection<JetType> lowerBounds = pair.getFirst();
|
||||
Collection<JetType> generalLowerBounds = pair.getFirst();
|
||||
Collection<JetType> 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<JetType> upperBounds = typeConstraintsWithoutErrorTypes.getUpperBounds();
|
||||
for (JetType upperBound : upperBounds) {
|
||||
@@ -69,15 +68,13 @@ public class ConstraintsUtil {
|
||||
//fun <T> foo(t: T, consumer: Consumer<T>): T
|
||||
//foo(1, c: Consumer<Any>) - 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<JetType> 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<JetType> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -637,15 +637,4 @@ public class TypeUtils {
|
||||
}
|
||||
return Pair.create(otherTypes, numberTypes);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JetType commonSupertypeForPossiblyNumberTypes(@NotNull Collection<JetType> types) {
|
||||
Pair<Collection<JetType>, Collection<JetType>> pair = filterNumberTypes(types);
|
||||
Collection<JetType> numberTypes = pair.getSecond();
|
||||
Collection<JetType> otherTypes = pair.getFirst();
|
||||
if (!numberTypes.isEmpty()) {
|
||||
otherTypes.add(commonSupertypeForNumberTypes(numberTypes));
|
||||
}
|
||||
return CommonSupertypes.commonSupertype(otherTypes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,10 @@ fun <T> upperBound(t: T, <!UNUSED_PARAMETER!>l<!>: Contr<T>) = t
|
||||
fun testUpperBound(contrS: Contr<String>, contrB: Contr<Byte>, contrN: Contr<Number>) {
|
||||
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>upperBound<!>(1, contrS)
|
||||
|
||||
upperBound(1, contrN)
|
||||
upperBound(1, contrB)
|
||||
val n = upperBound(1, contrN)
|
||||
n: Number
|
||||
<!TYPE_MISMATCH!>n<!>: Int
|
||||
val b = upperBound(1, contrB)
|
||||
b: Byte
|
||||
<!TYPE_MISMATCH!>b<!>: Int
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user