Substitution implemented for flexible occurrences of Java type parameters

This commit is contained in:
Andrey Breslav
2014-08-12 21:03:20 +04:00
parent bf53222bd9
commit dd2e95b3bc
63 changed files with 177 additions and 117 deletions
@@ -45,7 +45,7 @@ class LazyJavaTypeResolver(
val canonicalText = javaType.getCanonicalText()
val jetType = JavaToKotlinClassMap.getInstance().mapPrimitiveKotlinClass(canonicalText)
assert(jetType != null, "Primitive type is not found: " + canonicalText)
return jetType!!
jetType!!
}
is JavaClassifierType -> if (PLATFORM_TYPES && attr.allowFlexible && attr.howThisTypeIsUsed != SUPERTYPE)
LazyFlexibleJavaClassifierType(javaType, attr)
@@ -271,7 +271,20 @@ class LazyJavaTypeResolver(
) : DelegatingFlexibleType(
LazyJavaClassifierType(javaType, attr.toFlexible(FLEXIBLE_LOWER_BOUND)),
LazyJavaClassifierType(javaType, attr.toFlexible(FLEXIBLE_UPPER_BOUND))
)
), CustomTypeVariable {
override val isTypeVariable: Boolean = lowerBound.getConstructor() == upperBound.getConstructor()
&& lowerBound.getConstructor().getDeclarationDescriptor() is TypeParameterDescriptor
override val typeParameterDescriptor: TypeParameterDescriptor? = if (isTypeVariable) lowerBound.getConstructor().getDeclarationDescriptor() as TypeParameterDescriptor else null
override fun substitutionResult(replacement: JetType): JetType {
return if (replacement.isFlexible()) replacement
else DelegatingFlexibleType(TypeUtils.makeNotNullable(replacement), TypeUtils.makeNullable(replacement))
}
}
private class JavaTypeVariable()
}
trait JavaTypeAttributes {
@@ -0,0 +1,35 @@
/*
* 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
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor
// To facilitate laziness, any JetType implementation may inherit from this trait,
// even if it turns out that the type an instance represents is not actually a type variable
// (i.e. it is not derived from a type parameter), see isTypeVariable
public trait CustomTypeVariable : JetType {
val isTypeVariable: Boolean
// If typeParameterDescriptor != null <=> isTypeVariable == true, this is not a type variable
val typeParameterDescriptor: TypeParameterDescriptor?
// Throws an exception when isTypeVariable == false
fun substitutionResult(replacement: JetType): JetType
}
fun JetType.isCustomTypeVariable() = (this as? CustomTypeVariable)?.isTypeVariable ?: false
@@ -155,7 +155,7 @@ public class TypeSubstitutor {
// The type is within the substitution range, i.e. T or T?
JetType type = originalProjection.getType();
Variance originalProjectionKind = originalProjection.getProjectionKind();
if (type instanceof FlexibleType) {
if (type instanceof FlexibleType && !TypesPackage.isCustomTypeVariable(type)) {
FlexibleType flexibleType = (FlexibleType) type;
TypeProjection substitutedLower =
unsafeSubstitute(new TypeProjectionImpl(originalProjectionKind, flexibleType.getLowerBound()), recursionDepth + 1);
@@ -185,10 +185,17 @@ public class TypeSubstitutor {
//noinspection ConstantConditions
return TypeUtils.makeStarProjection(typeParameter);
case NO_CONFLICT:
boolean resultingIsNullable = type.isNullable() || replacement.getType().isNullable();
JetType substitutedType = TypeUtils.makeNullableAsSpecified(replacement.getType(), resultingIsNullable);
Variance resultingProjectionKind = combine(originalProjectionKind, replacement.getProjectionKind());
JetType substitutedType;
if (TypesPackage.isCustomTypeVariable(type)) {
CustomTypeVariable typeVariable = (CustomTypeVariable) type;
substitutedType = typeVariable.substitutionResult(replacement.getType());
}
else {
boolean resultingIsNullable = type.isNullable() || replacement.getType().isNullable();
substitutedType = TypeUtils.makeNullableAsSpecified(replacement.getType(), resultingIsNullable);
}
Variance resultingProjectionKind = combine(originalProjectionKind, replacement.getProjectionKind());
return new TypeProjectionImpl(resultingProjectionKind, substitutedType);
default:
throw new IllegalStateException();
@@ -30,6 +30,11 @@ public open class DelegatingFlexibleType(
override val upperBound: JetType
) : DelegatingType(), FlexibleType {
{
assert (!lowerBound.isFlexible()) { "Lower bound of a flexible type can not be flexible: $lowerBound" }
assert (!upperBound.isFlexible()) { "Upper bound of a flexible type can not be flexible: $upperBound" }
}
override fun getDelegate() = lowerBound
override fun toString() = "('$lowerBound'..'$upperBound')"