Make upper bounds of type parameter a list instead of a set

The same for lower bounds, also refactor type parameter implementations
This commit is contained in:
Alexander Udalov
2015-10-22 19:20:22 +03:00
parent 1ce5f8458c
commit d29757d927
10 changed files with 102 additions and 106 deletions
@@ -39,10 +39,7 @@ import org.jetbrains.kotlin.types.Variance;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import static org.jetbrains.kotlin.load.java.components.TypeUsage.MEMBER_SIGNATURE_CONTRAVARIANT;
import static org.jetbrains.kotlin.load.java.components.TypeUsage.UPPER_BOUND;
@@ -253,7 +250,7 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD
TypeParameterDescriptorImpl altParamDescriptor = originalToAltTypeParameters.get(originalTypeParamDescriptor);
KtTypeParameter altTypeParameter = altFunDeclaration.getTypeParameters().get(i);
Set<KotlinType> originalUpperBounds = originalTypeParamDescriptor.getUpperBounds();
List<KotlinType> originalUpperBounds = originalTypeParamDescriptor.getUpperBounds();
List<KtTypeReference> altUpperBounds = getUpperBounds(altFunDeclaration, altTypeParameter);
if (altUpperBounds.size() != originalUpperBounds.size()) {
if (altUpperBounds.isEmpty()
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve.lazy.descriptors;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor;
import org.jetbrains.kotlin.lexer.KtTokens;
@@ -28,44 +27,44 @@ import org.jetbrains.kotlin.resolve.lazy.LazyEntity;
import org.jetbrains.kotlin.resolve.source.KotlinSourceElementKt;
import org.jetbrains.kotlin.types.KotlinType;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
public class LazyTypeParameterDescriptor extends AbstractLazyTypeParameterDescriptor implements LazyEntity {
private final LazyClassContext c;
private final KtTypeParameter jetTypeParameter;
private final KtTypeParameter typeParameter;
public LazyTypeParameterDescriptor(
@NotNull LazyClassContext c,
@NotNull LazyClassDescriptor containingDeclaration,
@NotNull KtTypeParameter jetTypeParameter,
int index) {
@NotNull KtTypeParameter typeParameter,
int index
) {
super(
c.getStorageManager(),
containingDeclaration,
jetTypeParameter.getNameAsSafeName(),
jetTypeParameter.getVariance(),
jetTypeParameter.hasModifier(KtTokens.REIFIED_KEYWORD),
typeParameter.getNameAsSafeName(),
typeParameter.getVariance(),
typeParameter.hasModifier(KtTokens.REIFIED_KEYWORD),
index,
KotlinSourceElementKt.toSourceElement(jetTypeParameter)
KotlinSourceElementKt.toSourceElement(typeParameter)
);
this.c = c;
this.jetTypeParameter = jetTypeParameter;
this.typeParameter = typeParameter;
this.c.getTrace().record(BindingContext.TYPE_PARAMETER, jetTypeParameter, this);
this.c.getTrace().record(BindingContext.TYPE_PARAMETER, typeParameter, this);
}
@NotNull
@Override
protected Set<KotlinType> resolveUpperBounds() {
Set<KotlinType> upperBounds = Sets.newLinkedHashSet();
protected List<KotlinType> resolveUpperBounds() {
List<KotlinType> upperBounds = new ArrayList<KotlinType>(1);
KtTypeParameter jetTypeParameter = this.jetTypeParameter;
KtTypeReference extendsBound = jetTypeParameter.getExtendsBound();
KtTypeReference extendsBound = typeParameter.getExtendsBound();
if (extendsBound != null) {
KotlinType boundType = c.getDescriptorResolver().resolveTypeParameterExtendsBound(
this, extendsBound, getContainingDeclaration().getScopeForClassHeaderResolution(), c.getTrace());
this, extendsBound, getContainingDeclaration().getScopeForClassHeaderResolution(), c.getTrace()
);
upperBounds.add(boundType);
}
@@ -78,32 +77,30 @@ public class LazyTypeParameterDescriptor extends AbstractLazyTypeParameterDescri
return upperBounds;
}
private void resolveUpperBoundsFromWhereClause(Set<KotlinType> upperBounds) {
KtClassOrObject classOrObject = KtStubbedPsiUtil.getPsiOrStubParent(jetTypeParameter, KtClassOrObject.class, true);
private void resolveUpperBoundsFromWhereClause(@NotNull List<KotlinType> upperBounds) {
KtClassOrObject classOrObject = KtStubbedPsiUtil.getPsiOrStubParent(typeParameter, KtClassOrObject.class, true);
if (classOrObject instanceof KtClass) {
KtClass ktClass = (KtClass) classOrObject;
for (KtTypeConstraint jetTypeConstraint : ktClass.getTypeConstraints()) {
KtSimpleNameExpression constrainedParameterName = jetTypeConstraint.getSubjectTypeParameterName();
for (KtTypeConstraint typeConstraint : classOrObject.getTypeConstraints()) {
KtSimpleNameExpression constrainedParameterName = typeConstraint.getSubjectTypeParameterName();
if (constrainedParameterName != null) {
if (getName().equals(constrainedParameterName.getReferencedNameAsName())) {
c.getTrace().record(BindingContext.REFERENCE_TARGET, constrainedParameterName, this);
KtTypeReference boundTypeReference = jetTypeConstraint.getBoundTypeReference();
KtTypeReference boundTypeReference = typeConstraint.getBoundTypeReference();
if (boundTypeReference != null) {
KotlinType boundType = resolveBoundType(boundTypeReference);
upperBounds.add(boundType);
upperBounds.add(resolveBoundType(boundTypeReference));
}
}
}
}
}
}
@NotNull
private KotlinType resolveBoundType(@NotNull KtTypeReference boundTypeReference) {
return c.getTypeResolver()
.resolveType(getContainingDeclaration().getScopeForClassHeaderResolution(), boundTypeReference,
c.getTrace(), false);
return c.getTypeResolver().resolveType(
getContainingDeclaration().getScopeForClassHeaderResolution(), boundTypeReference, c.getTrace(), false
);
}
@NotNull
@@ -34,7 +34,10 @@ import org.jetbrains.kotlin.utils.Interner;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry;
@@ -414,7 +417,7 @@ public class DescriptorSerializer {
}
extension.serializeTypeParameter(typeParameter, builder);
Set<KotlinType> upperBounds = typeParameter.getUpperBounds();
List<KotlinType> upperBounds = typeParameter.getUpperBounds();
if (upperBounds.size() == 1 && KotlinBuiltIns.isDefaultBound(CollectionsKt.single(upperBounds))) return builder;
for (KotlinType upperBound : upperBounds) {
@@ -16,19 +16,16 @@
package org.jetbrains.kotlin.load.java.lazy.descriptors
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor
import org.jetbrains.kotlin.load.java.components.TypeUsage
import org.jetbrains.kotlin.load.java.lazy.LazyJavaResolverContext
import org.jetbrains.kotlin.load.java.lazy.types.toAttributes
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver
import org.jetbrains.kotlin.load.java.lazy.types.toFlexible
import org.jetbrains.kotlin.load.java.lazy.types.toAttributes
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
class LazyJavaTypeParameterDescriptor(
private val c: LazyJavaResolverContext,
@@ -38,26 +35,23 @@ class LazyJavaTypeParameterDescriptor(
) : AbstractLazyTypeParameterDescriptor(
c.storageManager,
containingDeclaration,
javaTypeParameter.getName(),
javaTypeParameter.name,
Variance.INVARIANT,
/* isReified = */ false,
index,
SourceElement.NO_SOURCE
) {
override fun resolveUpperBounds(): Set<KotlinType> {
val bounds = javaTypeParameter.getUpperBounds()
override fun resolveUpperBounds(): List<KotlinType> {
val bounds = javaTypeParameter.upperBounds
if (bounds.isEmpty()) {
return setOf(LazyJavaTypeResolver.FlexibleJavaClassifierTypeCapabilities.create(
c.module.builtIns.getAnyType(),
c.module.builtIns.getNullableAnyType()
return listOf(LazyJavaTypeResolver.FlexibleJavaClassifierTypeCapabilities.create(
c.module.builtIns.anyType,
c.module.builtIns.nullableAnyType
))
}
else {
return bounds.map {
javaType -> c.typeResolver.transformJavaType(javaType, TypeUsage.UPPER_BOUND.toAttributes(upperBoundForTypeParameter = this))
}.toSet()
return bounds.map {
c.typeResolver.transformJavaType(it, TypeUsage.UPPER_BOUND.toAttributes(upperBoundForTypeParameter = this))
}
}
}
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.types.TypeConstructor;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import org.jetbrains.kotlin.types.Variance;
import java.util.Set;
import java.util.List;
public interface TypeParameterDescriptor extends ClassifierDescriptor {
boolean isReified();
@@ -31,13 +31,13 @@ public interface TypeParameterDescriptor extends ClassifierDescriptor {
Variance getVariance();
@NotNull
Set<KotlinType> getUpperBounds();
List<KotlinType> getUpperBounds();
@NotNull
KotlinType getUpperBoundsAsType();
@NotNull
Set<KotlinType> getLowerBounds();
List<KotlinType> getLowerBounds();
@NotNull
@Override
@@ -33,18 +33,21 @@ import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import java.util.Collections;
import java.util.Set;
import java.util.List;
import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
public abstract class AbstractTypeParameterDescriptor extends DeclarationDescriptorNonRootImpl implements TypeParameterDescriptor {
public static final List<KotlinType> FALLBACK_UPPER_BOUNDS_ON_RECURSION =
Collections.singletonList(ErrorUtils.createErrorType("Recursion while calculating upper bounds"));
private final Variance variance;
private final boolean reified;
private final int index;
private final NotNullLazyValue<TypeConstructor> typeConstructor;
private final NotNullLazyValue<KotlinType> defaultType;
private final NotNullLazyValue<Set<KotlinType>> upperBounds;
private final NotNullLazyValue<List<KotlinType>> upperBounds;
private final NotNullLazyValue<KotlinType> upperBoundsAsType;
protected AbstractTypeParameterDescriptor(
@@ -71,23 +74,26 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
this.defaultType = storageManager.createLazyValue(new Function0<KotlinType>() {
@Override
public KotlinType invoke() {
return KotlinTypeImpl.create(Annotations.Companion.getEMPTY(), getTypeConstructor(), false, Collections.<TypeProjection>emptyList(),
new LazyScopeAdapter(storageManager.createLazyValue(
new Function0<KtScope>() {
@Override
public KtScope invoke() {
return getUpperBoundsAsType().getMemberScope();
}
}
)));
return KotlinTypeImpl.create(
Annotations.Companion.getEMPTY(),
getTypeConstructor(), false, Collections.<TypeProjection>emptyList(),
new LazyScopeAdapter(storageManager.createLazyValue(
new Function0<KtScope>() {
@Override
public KtScope invoke() {
return getUpperBoundsAsType().getMemberScope();
}
}
))
);
}
});
this.upperBounds = storageManager.createRecursionTolerantLazyValue(new Function0<Set<KotlinType>>() {
this.upperBounds = storageManager.createRecursionTolerantLazyValue(new Function0<List<KotlinType>>() {
@Override
public Set<KotlinType> invoke() {
public List<KotlinType> invoke() {
return resolveUpperBounds();
}
}, Collections.singleton(ErrorUtils.createErrorType("Recursion while calculating upper bounds")));
}, FALLBACK_UPPER_BOUNDS_ON_RECURSION);
this.upperBoundsAsType = storageManager.createLazyValue(new Function0<KotlinType>() {
@Override
public KotlinType invoke() {
@@ -97,8 +103,7 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
}
@NotNull
@ReadOnly
protected abstract Set<KotlinType> resolveUpperBounds();
protected abstract List<KotlinType> resolveUpperBounds();
@NotNull
protected abstract TypeConstructor createTypeConstructor();
@@ -121,7 +126,7 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
@NotNull
@Override
public Set<KotlinType> getUpperBounds() {
public List<KotlinType> getUpperBounds() {
return upperBounds.invoke();
}
@@ -133,7 +138,7 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
@NotNull
private KotlinType computeUpperBoundsAsType() {
Set<KotlinType> upperBounds = getUpperBounds();
List<KotlinType> upperBounds = getUpperBounds();
assert !upperBounds.isEmpty() : "Upper bound list is empty in " + getName();
KotlinType upperBoundsAsType = TypeIntersector.intersectTypes(KotlinTypeChecker.DEFAULT, upperBounds);
return upperBoundsAsType != null ? upperBoundsAsType : getBuiltIns(this).getNothingType();
@@ -153,8 +158,8 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
@NotNull
@Override
public Set<KotlinType> getLowerBounds() {
return Collections.singleton(getBuiltIns(this).getNothingType());
public List<KotlinType> getLowerBounds() {
return Collections.singletonList(getBuiltIns(this).getNothingType());
}
@NotNull
@@ -28,10 +28,10 @@ import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeConstructor;
import org.jetbrains.kotlin.types.TypeConstructorImpl;
import org.jetbrains.kotlin.types.Variance;
import org.jetbrains.kotlin.utils.SmartSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.List;
import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
@@ -63,7 +63,7 @@ public class TypeParameterDescriptorImpl extends AbstractTypeParameterDescriptor
return new TypeParameterDescriptorImpl(containingDeclaration, annotations, reified, variance, name, index, source);
}
private final Set<KotlinType> upperBounds = SmartSet.create();
private final List<KotlinType> upperBounds = new ArrayList<KotlinType>(1);
private boolean initialized = false;
private TypeParameterDescriptorImpl(
@@ -132,7 +132,7 @@ public class TypeParameterDescriptorImpl extends AbstractTypeParameterDescriptor
@NotNull
@Override
protected Set<KotlinType> resolveUpperBounds() {
protected List<KotlinType> resolveUpperBounds() {
checkInitialized();
return upperBounds;
}
@@ -53,7 +53,6 @@ public class DescriptorSubstitutor {
index++,
SourceElement.NO_SOURCE
);
substituted.setInitialized();
mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substituted.getDefaultType()));
@@ -68,8 +67,11 @@ public class DescriptorSubstitutor {
for (TypeParameterDescriptor descriptor : typeParameters) {
TypeParameterDescriptorImpl substituted = substitutedMap.get(descriptor);
for (KotlinType upperBound : descriptor.getUpperBounds()) {
substituted.getUpperBounds().add(substitutor.substitute(upperBound, Variance.INVARIANT));
KotlinType substitutedBound = substitutor.substitute(upperBound, Variance.INVARIANT);
assert substitutedBound != null : "Upper bound failed to substitute: " + descriptor;
substituted.addUpperBound(substitutedBound);
}
substituted.setInitialized();
}
return substitutor;
@@ -42,11 +42,8 @@ public class TypeIntersector {
}
@Nullable
public static KotlinType intersectTypes(
@NotNull KotlinTypeChecker typeChecker,
@NotNull Set<KotlinType> types
) {
assert (!types.isEmpty()) : "Attempting to intersect empty set of types, this case should be dealt with on the call site.";
public static KotlinType intersectTypes(@NotNull KotlinTypeChecker typeChecker, @NotNull Collection<KotlinType> types) {
assert !types.isEmpty() : "Attempting to intersect empty collection of types, this case should be dealt with on the call site.";
if (types.size() == 1) {
return types.iterator().next();
@@ -73,7 +70,7 @@ public class TypeIntersector {
if (nullabilityStripped.isEmpty()) {
// All types were errors
return ErrorUtils.createErrorType("Intersection of errors types: " + types);
return ErrorUtils.createErrorType("Intersection of error types: " + types);
}
// Now we remove types that have subtypes in the list
@@ -95,7 +92,6 @@ public class TypeIntersector {
if (!type.equals(other) && typeChecker.isSubtypeOf(other, type)) {
continue outer;
}
}
}
@@ -22,15 +22,19 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.*
import org.jetbrains.kotlin.serialization.deserialization.Deserialization
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
import org.jetbrains.kotlin.serialization.deserialization.upperBounds
import org.jetbrains.kotlin.types.KotlinType
import java.util.*
class DeserializedTypeParameterDescriptor(
c: DeserializationContext, private val proto: ProtoBuf.TypeParameter, index: Int) : AbstractLazyTypeParameterDescriptor(c.storageManager, c.containingDeclaration, c.nameResolver.getName(proto.name), Deserialization.variance(proto.variance), proto.reified, index, SourceElement.NO_SOURCE) {
private val typeDeserializer: TypeDeserializer = c.typeDeserializer
private val typeTable: TypeTable = c.typeTable
private val c: DeserializationContext,
private val proto: ProtoBuf.TypeParameter,
index: Int
) : AbstractLazyTypeParameterDescriptor(
c.storageManager, c.containingDeclaration, c.nameResolver.getName(proto.name),
Deserialization.variance(proto.variance), proto.reified, index, SourceElement.NO_SOURCE
) {
private val annotations = DeserializedAnnotationsWithPossibleTargets(c.storageManager) {
c.components.annotationAndConstantLoader
.loadTypeParameterAnnotations(proto, c.nameResolver)
@@ -39,15 +43,13 @@ class DeserializedTypeParameterDescriptor(
override fun getAnnotations(): Annotations = annotations
override fun resolveUpperBounds(): Set<KotlinType> {
val upperBounds = proto.upperBounds(typeTable)
override fun resolveUpperBounds(): List<KotlinType> {
val upperBounds = proto.upperBounds(c.typeTable)
if (upperBounds.isEmpty()) {
return setOf(this.builtIns.getDefaultBound())
return listOf(this.builtIns.defaultBound)
}
val result = LinkedHashSet<KotlinType>(upperBounds.size())
for (upperBound in upperBounds) {
result.add(typeDeserializer.type(upperBound, Annotations.EMPTY))
return upperBounds.map {
c.typeDeserializer.type(it, Annotations.EMPTY)
}
return result
}
}