Simplify OverridingUtil

Do not use NewKotlinTypeCheckerImpl
This commit is contained in:
Denis.Zharkov
2021-09-22 16:40:30 +03:00
committed by TeamCityServer
parent b821b26cfe
commit ec97dab6cd
2 changed files with 22 additions and 31 deletions
@@ -202,6 +202,7 @@ object AbstractTypeChecker {
return equalTypes(context.newTypeCheckerState(false, stubTypesEqualToAnything), a, b) return equalTypes(context.newTypeCheckerState(false, stubTypesEqualToAnything), a, b)
} }
@JvmOverloads
fun isSubtypeOf( fun isSubtypeOf(
state: TypeCheckerState, state: TypeCheckerState,
subType: KotlinTypeMarker, subType: KotlinTypeMarker,
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker; import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.types.checker.KotlinTypePreparator; import org.jetbrains.kotlin.types.checker.KotlinTypePreparator;
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner; import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner;
import org.jetbrains.kotlin.types.checker.NewKotlinTypeCheckerImpl;
import org.jetbrains.kotlin.utils.SmartSet; import org.jetbrains.kotlin.utils.SmartSet;
import java.util.*; import java.util.*;
@@ -311,13 +310,14 @@ public class OverridingUtil {
return OverrideCompatibilityInfo.conflict("Type parameter number mismatch"); return OverrideCompatibilityInfo.conflict("Type parameter number mismatch");
} }
Pair<NewKotlinTypeCheckerImpl, TypeCheckerState> typeChecker = createTypeChecker(superTypeParameters, subTypeParameters);
TypeCheckerState typeCheckerState = createTypeCheckerState(superTypeParameters, subTypeParameters);
for (int i = 0; i < superTypeParameters.size(); i++) { for (int i = 0; i < superTypeParameters.size(); i++) {
if (!areTypeParametersEquivalent( if (!areTypeParametersEquivalent(
superTypeParameters.get(i), superTypeParameters.get(i),
subTypeParameters.get(i), subTypeParameters.get(i),
typeChecker typeCheckerState
)) { )) {
return OverrideCompatibilityInfo.incompatible("Type parameter bounds mismatch"); return OverrideCompatibilityInfo.incompatible("Type parameter bounds mismatch");
} }
@@ -327,7 +327,7 @@ public class OverridingUtil {
if (!areTypesEquivalent( if (!areTypesEquivalent(
superValueParameters.get(i), superValueParameters.get(i),
subValueParameters.get(i), subValueParameters.get(i),
typeChecker) typeCheckerState)
) { ) {
return OverrideCompatibilityInfo.incompatible("Value parameter type mismatch"); return OverrideCompatibilityInfo.incompatible("Value parameter type mismatch");
} }
@@ -345,8 +345,8 @@ public class OverridingUtil {
if (superReturnType != null && subReturnType != null) { if (superReturnType != null && subReturnType != null) {
boolean bothErrors = KotlinTypeKt.isError(subReturnType) && KotlinTypeKt.isError(superReturnType); boolean bothErrors = KotlinTypeKt.isError(subReturnType) && KotlinTypeKt.isError(superReturnType);
if (!bothErrors && if (!bothErrors &&
!typeChecker.getFirst().isSubtypeOf( !AbstractTypeChecker.INSTANCE.isSubtypeOf(
typeChecker.getSecond(), typeCheckerState,
subReturnType.unwrap(), subReturnType.unwrap(),
superReturnType.unwrap() superReturnType.unwrap()
) )
@@ -387,24 +387,13 @@ public class OverridingUtil {
} }
@NotNull @NotNull
private Pair<NewKotlinTypeCheckerImpl, TypeCheckerState> createTypeChecker( private TypeCheckerState createTypeCheckerState(
@NotNull List<TypeParameterDescriptor> firstParameters, @NotNull List<TypeParameterDescriptor> firstParameters,
@NotNull List<TypeParameterDescriptor> secondParameters @NotNull List<TypeParameterDescriptor> secondParameters
) { ) {
assert firstParameters.size() == secondParameters.size() : assert firstParameters.size() == secondParameters.size() :
"Should be the same number of type parameters: " + firstParameters + " vs " + secondParameters; "Should be the same number of type parameters: " + firstParameters + " vs " + secondParameters;
NewKotlinTypeCheckerImpl typeChecker = new NewKotlinTypeCheckerImpl(kotlinTypeRefiner, KotlinTypePreparator.Default.INSTANCE);
TypeCheckerState state = createTypeCheckerState(firstParameters, secondParameters);
return new Pair<NewKotlinTypeCheckerImpl, TypeCheckerState>(typeChecker, state);
}
@NotNull
private TypeCheckerState createTypeCheckerState(
@NotNull List<TypeParameterDescriptor> firstParameters,
@NotNull List<TypeParameterDescriptor> secondParameters
) {
if (firstParameters.isEmpty()) { if (firstParameters.isEmpty()) {
return new OverridingUtilTypeSystemContext(null, equalityAxioms, kotlinTypeRefiner) return new OverridingUtilTypeSystemContext(null, equalityAxioms, kotlinTypeRefiner)
.newTypeCheckerState(true, true); .newTypeCheckerState(true, true);
@@ -435,21 +424,21 @@ public class OverridingUtil {
return null; return null;
} }
private boolean areTypesEquivalent( private static boolean areTypesEquivalent(
@NotNull KotlinType typeInSuper, @NotNull KotlinType typeInSuper,
@NotNull KotlinType typeInSub, @NotNull KotlinType typeInSub,
@NotNull Pair<NewKotlinTypeCheckerImpl, TypeCheckerState> typeChecker @NotNull TypeCheckerState typeCheckerState
) { ) {
boolean bothErrors = KotlinTypeKt.isError(typeInSuper) && KotlinTypeKt.isError(typeInSub); boolean bothErrors = KotlinTypeKt.isError(typeInSuper) && KotlinTypeKt.isError(typeInSub);
if (bothErrors) return true; if (bothErrors) return true;
return typeChecker.getFirst().equalTypes(typeChecker.getSecond(), typeInSuper.unwrap(), typeInSub.unwrap()); return AbstractTypeChecker.INSTANCE.equalTypes(typeCheckerState, typeInSuper.unwrap(), typeInSub.unwrap());
} }
// See JLS 8, 8.4.4 Generic Methods // See JLS 8, 8.4.4 Generic Methods
private boolean areTypeParametersEquivalent( private static boolean areTypeParametersEquivalent(
@NotNull TypeParameterDescriptor superTypeParameter, @NotNull TypeParameterDescriptor superTypeParameter,
@NotNull TypeParameterDescriptor subTypeParameter, @NotNull TypeParameterDescriptor subTypeParameter,
@NotNull Pair<NewKotlinTypeCheckerImpl, TypeCheckerState> typeChecker @NotNull TypeCheckerState typeCheckerState
) { ) {
List<KotlinType> superBounds = superTypeParameter.getUpperBounds(); List<KotlinType> superBounds = superTypeParameter.getUpperBounds();
List<KotlinType> subBounds = new ArrayList<KotlinType>(subTypeParameter.getUpperBounds()); List<KotlinType> subBounds = new ArrayList<KotlinType>(subTypeParameter.getUpperBounds());
@@ -460,7 +449,7 @@ public class OverridingUtil {
ListIterator<KotlinType> it = subBounds.listIterator(); ListIterator<KotlinType> it = subBounds.listIterator();
while (it.hasNext()) { while (it.hasNext()) {
KotlinType subBound = it.next(); KotlinType subBound = it.next();
if (areTypesEquivalent(superBound, subBound, typeChecker)) { if (areTypesEquivalent(superBound, subBound, typeCheckerState)) {
it.remove(); it.remove();
continue outer; continue outer;
} }
@@ -587,13 +576,14 @@ public class OverridingUtil {
if (!isVisibilityMoreSpecific(a, b)) return false; if (!isVisibilityMoreSpecific(a, b)) return false;
Pair<NewKotlinTypeCheckerImpl, TypeCheckerState> checker =
DEFAULT.createTypeChecker(a.getTypeParameters(), b.getTypeParameters()); TypeCheckerState checkerState =
DEFAULT.createTypeCheckerState(a.getTypeParameters(), b.getTypeParameters());
if (a instanceof FunctionDescriptor) { if (a instanceof FunctionDescriptor) {
assert b instanceof FunctionDescriptor : "b is " + b.getClass(); assert b instanceof FunctionDescriptor : "b is " + b.getClass();
return isReturnTypeMoreSpecific(a, aReturnType, b, bReturnType, checker); return isReturnTypeMoreSpecific(a, aReturnType, b, bReturnType, checkerState);
} }
if (a instanceof PropertyDescriptor) { if (a instanceof PropertyDescriptor) {
assert b instanceof PropertyDescriptor : "b is " + b.getClass(); assert b instanceof PropertyDescriptor : "b is " + b.getClass();
@@ -605,11 +595,11 @@ public class OverridingUtil {
if (pa.isVar() && pb.isVar()) { if (pa.isVar() && pb.isVar()) {
// TODO(dsavvinov): using DEFAULT here looks suspicious // TODO(dsavvinov): using DEFAULT here looks suspicious
return checker.getFirst().equalTypes(checker.getSecond(), aReturnType.unwrap(), bReturnType.unwrap()); return AbstractTypeChecker.INSTANCE.equalTypes(checkerState, aReturnType.unwrap(), bReturnType.unwrap());
} }
else { else {
// both vals or var vs val: val can't be more specific then var // both vals or var vs val: val can't be more specific then var
return !(!pa.isVar() && pb.isVar()) && isReturnTypeMoreSpecific(a, aReturnType, b, bReturnType, checker); return !(!pa.isVar() && pb.isVar()) && isReturnTypeMoreSpecific(a, aReturnType, b, bReturnType, checkerState);
} }
} }
throw new IllegalArgumentException("Unexpected callable: " + a.getClass()); throw new IllegalArgumentException("Unexpected callable: " + a.getClass());
@@ -644,9 +634,9 @@ public class OverridingUtil {
@NotNull KotlinType aReturnType, @NotNull KotlinType aReturnType,
@NotNull CallableDescriptor b, @NotNull CallableDescriptor b,
@NotNull KotlinType bReturnType, @NotNull KotlinType bReturnType,
@NotNull Pair<NewKotlinTypeCheckerImpl, TypeCheckerState> typeChecker @NotNull TypeCheckerState typeCheckerState
) { ) {
return typeChecker.getFirst().isSubtypeOf(typeChecker.getSecond(), aReturnType.unwrap(), bReturnType.unwrap()); return AbstractTypeChecker.INSTANCE.isSubtypeOf(typeCheckerState, aReturnType.unwrap(), bReturnType.unwrap());
} }
@NotNull @NotNull