Decouple TypeCheckerContext and TypeSystemContext
This commit is contained in:
committed by
TeamCityServer
parent
53a7dc1126
commit
3909e3c54c
@@ -393,24 +393,28 @@ public class OverridingUtil {
|
||||
"Should be the same number of type parameters: " + firstParameters + " vs " + secondParameters;
|
||||
|
||||
NewKotlinTypeCheckerImpl typeChecker = new NewKotlinTypeCheckerImpl(kotlinTypeRefiner);
|
||||
OverridingUtilTypeCheckerContext context = createTypeCheckerContext(firstParameters, secondParameters);
|
||||
ClassicTypeCheckerContext context = createTypeCheckerContext(firstParameters, secondParameters);
|
||||
|
||||
return new Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext>(typeChecker, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private OverridingUtilTypeCheckerContext createTypeCheckerContext(
|
||||
private ClassicTypeCheckerContext createTypeCheckerContext(
|
||||
@NotNull List<TypeParameterDescriptor> firstParameters,
|
||||
@NotNull List<TypeParameterDescriptor> secondParameters
|
||||
) {
|
||||
if (firstParameters.isEmpty()) return new OverridingUtilTypeCheckerContext(null);
|
||||
if (firstParameters.isEmpty()) {
|
||||
return (ClassicTypeCheckerContext) new OverridingUtilTypeSystemContext(null, equalityAxioms, kotlinTypeRefiner)
|
||||
.newBaseTypeCheckerContext(true, true);
|
||||
}
|
||||
|
||||
Map<TypeConstructor, TypeConstructor> matchingTypeConstructors = new HashMap<TypeConstructor, TypeConstructor>();
|
||||
for (int i = 0; i < firstParameters.size(); i++) {
|
||||
matchingTypeConstructors.put(firstParameters.get(i).getTypeConstructor(), secondParameters.get(i).getTypeConstructor());
|
||||
}
|
||||
|
||||
return new OverridingUtilTypeCheckerContext(matchingTypeConstructors);
|
||||
return (ClassicTypeCheckerContext) new OverridingUtilTypeSystemContext(matchingTypeConstructors, equalityAxioms, kotlinTypeRefiner)
|
||||
.newBaseTypeCheckerContext(true, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -977,34 +981,6 @@ public class OverridingUtil {
|
||||
return maxVisibility;
|
||||
}
|
||||
|
||||
private class OverridingUtilTypeCheckerContext extends ClassicTypeCheckerContext {
|
||||
private final @Nullable Map<TypeConstructor, TypeConstructor> matchingTypeConstructors;
|
||||
|
||||
public OverridingUtilTypeCheckerContext(@Nullable Map<TypeConstructor, TypeConstructor> matchingTypeConstructors) {
|
||||
super(
|
||||
/* errorTypesEqualsToAnything = */ true,
|
||||
/* stubTypesEqualsToAnything = */ true,
|
||||
/* allowedTypeVariable = */ true,
|
||||
kotlinTypeRefiner
|
||||
);
|
||||
this.matchingTypeConstructors = matchingTypeConstructors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areEqualTypeConstructors(@NotNull TypeConstructor a, @NotNull TypeConstructor b) {
|
||||
return super.areEqualTypeConstructors(a, b) || areEqualTypeConstructorsByAxioms(a, b);
|
||||
}
|
||||
|
||||
private boolean areEqualTypeConstructorsByAxioms(@NotNull TypeConstructor a, @NotNull TypeConstructor b) {
|
||||
if (equalityAxioms.equals(a, b)) return true;
|
||||
if (matchingTypeConstructors == null) return false;
|
||||
|
||||
TypeConstructor img1 = matchingTypeConstructors.get(a);
|
||||
TypeConstructor img2 = matchingTypeConstructors.get(b);
|
||||
return (img1 != null && img1.equals(b)) || (img2 != null && img2.equals(a));
|
||||
}
|
||||
}
|
||||
|
||||
public static class OverrideCompatibilityInfo {
|
||||
public enum Result {
|
||||
OVERRIDABLE,
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve
|
||||
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.TypeConstructor
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
|
||||
class OverridingUtilTypeSystemContext(
|
||||
val matchingTypeConstructors: Map<TypeConstructor, TypeConstructor>?,
|
||||
private val equalityAxioms: KotlinTypeChecker.TypeConstructorEquality,
|
||||
val kotlinTypeRefiner: KotlinTypeRefiner
|
||||
) : ClassicTypeSystemContext {
|
||||
|
||||
override fun areEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker): Boolean {
|
||||
require(c1 is TypeConstructor)
|
||||
require(c2 is TypeConstructor)
|
||||
return super.areEqualTypeConstructors(c1, c2) || areEqualTypeConstructorsByAxioms(c1, c2)
|
||||
}
|
||||
|
||||
override fun newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext {
|
||||
return ClassicTypeCheckerContext(
|
||||
errorTypesEqualToAnything,
|
||||
stubTypesEqualToAnything,
|
||||
allowedTypeVariable = true,
|
||||
kotlinTypeRefiner,
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
private fun areEqualTypeConstructorsByAxioms(a: TypeConstructor, b: TypeConstructor): Boolean {
|
||||
if (equalityAxioms.equals(a, b)) return true
|
||||
if (matchingTypeConstructors == null) return false
|
||||
val img1 = matchingTypeConstructors[a]
|
||||
val img2 = matchingTypeConstructors[b]
|
||||
return img1 != null && img1 == b || img2 != null && img2 == a
|
||||
}
|
||||
}
|
||||
+4
-27
@@ -16,24 +16,18 @@
|
||||
|
||||
package org.jetbrains.kotlin.types.checker
|
||||
|
||||
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
import org.jetbrains.kotlin.types.refinement.TypeRefinement
|
||||
|
||||
open class ClassicTypeCheckerContext(
|
||||
val errorTypeEqualsToAnything: Boolean,
|
||||
val stubTypeEqualsToAnything: Boolean = true,
|
||||
val allowedTypeVariable: Boolean = true,
|
||||
val kotlinTypeRefiner: KotlinTypeRefiner = KotlinTypeRefiner.Default
|
||||
) : ClassicTypeSystemContext, AbstractTypeCheckerContext() {
|
||||
|
||||
override fun prepareType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
require(type is KotlinType, type::errorMessage)
|
||||
return NewKotlinTypeChecker.Default.transformToNewType(type.unwrap())
|
||||
}
|
||||
val kotlinTypeRefiner: KotlinTypeRefiner = KotlinTypeRefiner.Default,
|
||||
override val typeSystemContext: ClassicTypeSystemContext = SimpleClassicTypeSystemContext
|
||||
) : AbstractTypeCheckerContext() {
|
||||
|
||||
@OptIn(TypeRefinement::class)
|
||||
override fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
@@ -47,25 +41,8 @@ open class ClassicTypeCheckerContext(
|
||||
override val isStubTypeEqualsToAnything: Boolean
|
||||
get() = stubTypeEqualsToAnything
|
||||
|
||||
override fun areEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker): Boolean {
|
||||
require(c1 is TypeConstructor, c1::errorMessage)
|
||||
require(c2 is TypeConstructor, c2::errorMessage)
|
||||
return areEqualTypeConstructors(c1, c2)
|
||||
}
|
||||
|
||||
open fun areEqualTypeConstructors(a: TypeConstructor, b: TypeConstructor): Boolean = when {
|
||||
/*
|
||||
* For integer literal types we have special rules for constructor's equality,
|
||||
* so we have to check it manually
|
||||
* For example: Int in ILT.possibleTypes -> ILT == Int
|
||||
*/
|
||||
a is IntegerLiteralTypeConstructor -> a.checkConstructor(b)
|
||||
b is IntegerLiteralTypeConstructor -> b.checkConstructor(a)
|
||||
else -> a == b
|
||||
}
|
||||
|
||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform {
|
||||
return classicSubstitutionSupertypePolicy(type)
|
||||
return typeSystemContext.classicSubstitutionSupertypePolicy(type)
|
||||
}
|
||||
|
||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean get() = this is UnwrappedType && allowedTypeVariable && constructor is NewTypeVariableConstructor
|
||||
|
||||
@@ -341,7 +341,7 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext {
|
||||
return ClassicTypeCheckerContext(errorTypesEqualToAnything, stubTypesEqualToAnything)
|
||||
return ClassicTypeCheckerContext(errorTypesEqualToAnything, stubTypesEqualToAnything, typeSystemContext = this)
|
||||
}
|
||||
|
||||
override fun nullableNothingType(): SimpleTypeMarker {
|
||||
@@ -485,8 +485,8 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
|
||||
}
|
||||
|
||||
override fun prepareType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
require(type is UnwrappedType, type::errorMessage)
|
||||
return NewKotlinTypeChecker.Default.transformToNewType(type)
|
||||
require(type is KotlinType, type::errorMessage)
|
||||
return NewKotlinTypeChecker.Default.transformToNewType(type.unwrap())
|
||||
}
|
||||
|
||||
override fun DefinitelyNotNullTypeMarker.original(): SimpleTypeMarker {
|
||||
|
||||
Reference in New Issue
Block a user