Approximate captured types in contravariant positions properly

^KT-43802 Fixed
This commit is contained in:
Victor Petukhov
2021-02-18 16:29:59 +03:00
parent 387d84f826
commit 4e5647090e
10 changed files with 205 additions and 6 deletions
@@ -85,6 +85,15 @@ abstract class TypeConstructorSubstitution : TypeSubstitution() {
}
}
class SubstitutionWithCapturedTypeApproximation(substitution: TypeSubstitution) : DelegatedTypeSubstitution(substitution) {
override fun approximateCapturedTypes() = true
}
class SubstitutionWithContravariantCapturedTypeApproximation(substitution: TypeSubstitution) : DelegatedTypeSubstitution(substitution) {
override fun approximateCapturedTypes() = true
override fun approximateContravariantCapturedTypes() = true
}
class IndexedParametersSubstitution(
val parameters: Array<TypeParameterDescriptor>,
val arguments: Array<TypeProjection>,
@@ -67,6 +67,29 @@ public class TypeSubstitutor implements TypeSubstitutorMarker {
);
}
@NotNull
public TypeSubstitutor replaceWithContravariantApproximatingSubstitution() {
if (substitution instanceof SubstitutionWithCapturedTypeApproximation) {
return new TypeSubstitutor(
new SubstitutionWithContravariantCapturedTypeApproximation(
((SubstitutionWithCapturedTypeApproximation) substitution).getSubstitution()
)
);
}
if (substitution instanceof IndexedParametersSubstitution && !substitution.approximateContravariantCapturedTypes()) {
return new TypeSubstitutor(
new IndexedParametersSubstitution(
((IndexedParametersSubstitution) substitution).getParameters(),
((IndexedParametersSubstitution) substitution).getArguments(),
true
)
);
}
return this;
}
@NotNull
public static TypeSubstitutor createChainedSubstitutor(@NotNull TypeSubstitution first, @NotNull TypeSubstitution second) {
return create(DisjointKeysUnionTypeSubstitution.create(first, second));