Support equalityAxioms in isSubtypeOf()

This commit is contained in:
Andrey Breslav
2014-05-30 23:24:50 +04:00
parent 9cfbfd2806
commit 7f60ccf663
7 changed files with 149 additions and 28 deletions
@@ -321,7 +321,7 @@ public class ConstraintSystemImpl implements ConstraintSystem {
public boolean assertEqualTypeConstructors(
@NotNull TypeConstructor a, @NotNull TypeConstructor b
) {
throw new IllegalStateException("'assertEqualTypeConstructors' shouldn't be invoked inside 'isSubtypeOf'");
return a.equals(b);
}
@Override
@@ -57,27 +57,4 @@ public class JetTypeChecker {
}
private static final TypeCheckingProcedure TYPE_CHECKER = new TypeCheckingProcedure(new TypeCheckerTypingConstraints());
private static class TypeCheckerTypingConstraints implements TypingConstraints {
@Override
public boolean assertEqualTypes(@NotNull JetType a, @NotNull JetType b, @NotNull TypeCheckingProcedure typeCheckingProcedure) {
return typeCheckingProcedure.equalTypes(a, b);
// return TypeUtils.equalTypes(a, b);
}
@Override
public boolean assertEqualTypeConstructors(@NotNull TypeConstructor a, @NotNull TypeConstructor b) {
return a.equals(b);
}
@Override
public boolean assertSubtype(@NotNull JetType subtype, @NotNull JetType supertype, @NotNull TypeCheckingProcedure typeCheckingProcedure) {
return typeCheckingProcedure.isSubtypeOf(subtype, supertype);
}
@Override
public boolean noCorrespondingSupertype(@NotNull JetType subtype, @NotNull JetType supertype) {
return false; // type checking fails
}
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.types.checker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeConstructor;
class TypeCheckerTypingConstraints implements TypingConstraints {
@Override
public boolean assertEqualTypes(@NotNull JetType a, @NotNull JetType b, @NotNull TypeCheckingProcedure typeCheckingProcedure) {
return typeCheckingProcedure.equalTypes(a, b);
}
@Override
public boolean assertEqualTypeConstructors(@NotNull TypeConstructor a, @NotNull TypeConstructor b) {
return a.equals(b);
}
@Override
public boolean assertSubtype(@NotNull JetType subtype, @NotNull JetType supertype, @NotNull TypeCheckingProcedure typeCheckingProcedure) {
return typeCheckingProcedure.isSubtypeOf(subtype, supertype);
}
@Override
public boolean noCorrespondingSupertype(@NotNull JetType subtype, @NotNull JetType supertype) {
return false; // type checking fails
}
}
@@ -32,12 +32,19 @@ public class TypeCheckingProcedure {
// as the second parameter, applying the substitution of type arguments to it
@Nullable
public static JetType findCorrespondingSupertype(@NotNull JetType subtype, @NotNull JetType supertype) {
return findCorrespondingSupertype(subtype, supertype, new TypeCheckerTypingConstraints());
}
// This method returns the supertype of the first parameter that has the same constructor
// as the second parameter, applying the substitution of type arguments to it
@Nullable
public static JetType findCorrespondingSupertype(@NotNull JetType subtype, @NotNull JetType supertype, @NotNull TypingConstraints typingConstraints) {
TypeConstructor constructor = subtype.getConstructor();
if (constructor.equals(supertype.getConstructor())) {
if (typingConstraints.assertEqualTypeConstructors(constructor, supertype.getConstructor())) {
return subtype;
}
for (JetType immediateSupertype : constructor.getSupertypes()) {
JetType correspondingSupertype = findCorrespondingSupertype(immediateSupertype, supertype);
JetType correspondingSupertype = findCorrespondingSupertype(immediateSupertype, supertype, typingConstraints);
if (correspondingSupertype != null) {
return TypeSubstitutor.create(subtype).safeSubstitute(correspondingSupertype, Variance.INVARIANT);
}
@@ -167,7 +174,7 @@ public class TypeCheckingProcedure {
if (KotlinBuiltIns.getInstance().isNothingOrNullableNothing(subtype)) {
return true;
}
@Nullable JetType closestSupertype = findCorrespondingSupertype(subtype, supertype);
@Nullable JetType closestSupertype = findCorrespondingSupertype(subtype, supertype, constraints);
if (closestSupertype == null) {
return constraints.noCorrespondingSupertype(subtype, supertype); // if this returns true, there still isn't any supertype to continue with
}
@@ -177,7 +184,7 @@ public class TypeCheckingProcedure {
private boolean checkSubtypeForTheSameConstructor(@NotNull JetType subtype, @NotNull JetType supertype) {
TypeConstructor constructor = subtype.getConstructor();
assert constructor.equals(supertype.getConstructor()) : constructor + " is not " + supertype.getConstructor();
assert constraints.assertEqualTypeConstructors(constructor, supertype.getConstructor()) : constructor + " is not " + supertype.getConstructor();
List<TypeProjection> subArguments = subtype.getArguments();
List<TypeProjection> superArguments = supertype.getArguments();