EA-37910 - UOE: DescriptorUtils.getSubstitution
The corresponding method reworked to be less of a hack Tests added
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.jet.lang.resolve;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
@@ -29,7 +28,9 @@ import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||
import org.jetbrains.jet.lang.types.*;
|
||||
import org.jetbrains.jet.lang.types.DescriptorSubstitutor;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.TypeUtils;
|
||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||
@@ -44,58 +45,16 @@ import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor
|
||||
public class DescriptorUtils {
|
||||
|
||||
@NotNull
|
||||
public static <D extends CallableDescriptor> D substituteBounds(@NotNull D functionDescriptor) {
|
||||
public static <D extends CallableDescriptor> D substituteBounds(@NotNull final D functionDescriptor) {
|
||||
final List<TypeParameterDescriptor> typeParameters = functionDescriptor.getTypeParameters();
|
||||
if (typeParameters.isEmpty()) return functionDescriptor;
|
||||
final Map<TypeConstructor, TypeParameterDescriptor> typeConstructors = Maps.newHashMap();
|
||||
for (TypeParameterDescriptor typeParameter : typeParameters) {
|
||||
typeConstructors.put(typeParameter.getTypeConstructor(), typeParameter);
|
||||
}
|
||||
//noinspection unchecked
|
||||
return (D) functionDescriptor.substitute(new TypeSubstitutor(TypeSubstitution.EMPTY) {
|
||||
@Override
|
||||
public boolean inRange(@NotNull TypeConstructor typeConstructor) {
|
||||
return typeConstructors.containsKey(typeConstructor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return typeParameters.isEmpty();
|
||||
}
|
||||
// TODO: this does not handle any recursion in the bounds
|
||||
@SuppressWarnings("unchecked")
|
||||
D substitutedFunction = (D) functionDescriptor.substitute(DescriptorSubstitutor.createUpperBoundsSubstitutor(typeParameters));
|
||||
assert substitutedFunction != null : "Substituting upper bounds should always be legal";
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public TypeSubstitution getSubstitution() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JetType safeSubstitute(@NotNull JetType type, @NotNull Variance howThisTypeIsUsed) {
|
||||
JetType substituted = substitute(type, howThisTypeIsUsed);
|
||||
if (substituted == null) {
|
||||
return ErrorUtils.createErrorType("Substitution failed");
|
||||
}
|
||||
return substituted;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public JetType substitute(@NotNull JetType type, @NotNull Variance howThisTypeIsUsed) {
|
||||
TypeParameterDescriptor typeParameterDescriptor = typeConstructors.get(type.getConstructor());
|
||||
if (typeParameterDescriptor != null) {
|
||||
switch (howThisTypeIsUsed) {
|
||||
case INVARIANT:
|
||||
return type;
|
||||
case IN_VARIANCE:
|
||||
throw new UnsupportedOperationException(); // TODO : lower bounds
|
||||
case OUT_VARIANCE:
|
||||
return typeParameterDescriptor.getDefaultType();
|
||||
}
|
||||
}
|
||||
return super.substitute(type, howThisTypeIsUsed);
|
||||
}
|
||||
});
|
||||
return substitutedFunction;
|
||||
}
|
||||
|
||||
public static Modality convertModality(Modality modality, boolean makeNonAbstract) {
|
||||
|
||||
@@ -16,12 +16,17 @@
|
||||
|
||||
package org.jetbrains.jet.lang.types;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptorImpl;
|
||||
import org.jetbrains.jet.utils.DFS;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -30,6 +35,13 @@ import java.util.Map;
|
||||
*/
|
||||
public class DescriptorSubstitutor {
|
||||
|
||||
private static final Function<TypeProjection,JetType> PROJECTIONS_TO_TYPES = new Function<TypeProjection, JetType>() {
|
||||
@Override
|
||||
public JetType apply(TypeProjection projection) {
|
||||
return projection.getType();
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
public static TypeSubstitutor substituteTypeParameters(
|
||||
@NotNull List<TypeParameterDescriptor> typeParameters,
|
||||
@@ -80,4 +92,69 @@ public class DescriptorSubstitutor {
|
||||
return substitutor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@NotNull
|
||||
public static TypeSubstitutor createUpperBoundsSubstitutor(
|
||||
@NotNull final List<TypeParameterDescriptor> typeParameters
|
||||
) {
|
||||
Map<TypeConstructor, TypeProjection> mutableSubstitution = Maps.newHashMap();
|
||||
TypeSubstitutor substitutor = TypeSubstitutor.create(mutableSubstitution);
|
||||
|
||||
// todo assert: no loops
|
||||
for (TypeParameterDescriptor descriptor : topologicallySortTypeParameters(typeParameters)) {
|
||||
JetType upperBoundsAsType = descriptor.getUpperBoundsAsType();
|
||||
JetType substitutedUpperBoundsAsType = substitutor.substitute(upperBoundsAsType, Variance.INVARIANT);
|
||||
mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjection(substitutedUpperBoundsAsType));
|
||||
}
|
||||
|
||||
return substitutor;
|
||||
}
|
||||
|
||||
private static List<TypeParameterDescriptor> topologicallySortTypeParameters(final List<TypeParameterDescriptor> typeParameters) {
|
||||
// In the end, we want every parameter to have no references to those after it in the list
|
||||
// This gives us the reversed order: the one that refers to everybody else comes first
|
||||
List<TypeParameterDescriptor> topOrder = DFS.topologicalOrder(
|
||||
typeParameters,
|
||||
new DFS.Neighbors<TypeParameterDescriptor>() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterable<TypeParameterDescriptor> getNeighbors(TypeParameterDescriptor current) {
|
||||
return getTypeParametersFromUpperBounds(current, typeParameters);
|
||||
}
|
||||
});
|
||||
|
||||
assert topOrder.size() == typeParameters.size() : "All type parameters must be visited, but only " + topOrder + " were";
|
||||
|
||||
// Now, the one that refers to everybody else stands in the last position
|
||||
Collections.reverse(topOrder);
|
||||
return topOrder;
|
||||
}
|
||||
|
||||
private static List<TypeParameterDescriptor> getTypeParametersFromUpperBounds(
|
||||
TypeParameterDescriptor current,
|
||||
final List<TypeParameterDescriptor> typeParameters
|
||||
) {
|
||||
return DFS.dfs(
|
||||
current.getUpperBounds(),
|
||||
new DFS.Neighbors<JetType>() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterable<JetType> getNeighbors(JetType current) {
|
||||
return Collections2.transform(current.getArguments(), PROJECTIONS_TO_TYPES);
|
||||
}
|
||||
},
|
||||
new DFS.NodeHandlerWithListResult<JetType, TypeParameterDescriptor>() {
|
||||
@Override
|
||||
public void beforeChildren(JetType current) {
|
||||
ClassifierDescriptor declarationDescriptor = current.getConstructor().getDeclarationDescriptor();
|
||||
// typeParameters in a list, but it contains very few elements, so it's fine to call contains() on it
|
||||
//noinspection SuspiciousMethodCalls
|
||||
if (typeParameters.contains(declarationDescriptor)) {
|
||||
result.add((TypeParameterDescriptor) declarationDescriptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user