diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt index c5dd16c6455..d1360c40b05 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.Variance.IN_VARIANCE import org.jetbrains.kotlin.types.Variance.OUT_VARIANCE +import org.jetbrains.kotlin.types.typeUtil.builtIns public class CapturedTypeConstructor( public val typeProjection: TypeProjection @@ -38,7 +39,7 @@ public class CapturedTypeConstructor( val superType = if (typeProjection.getProjectionKind() == Variance.OUT_VARIANCE) typeProjection.getType() else - KotlinBuiltIns.getInstance().getNullableAnyType() + builtIns.nullableAnyType return listOf(superType) } @@ -74,10 +75,10 @@ public class CapturedType( } override val subTypeRepresentative: JetType - get() = representative(OUT_VARIANCE, KotlinBuiltIns.getInstance().getNullableAnyType()) + get() = representative(OUT_VARIANCE, builtIns.nullableAnyType) override val superTypeRepresentative: JetType - get() = representative(IN_VARIANCE, KotlinBuiltIns.getInstance().getNothingType()) + get() = representative(IN_VARIANCE, builtIns.nothingType) private fun representative(variance: Variance, default: JetType) = if (typeProjection.getProjectionKind() == variance) typeProjection.getType() else default diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt index a1a6d0e5a7d..bdb4cca00e3 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemImpl.kt @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.types.TypeUtils.DONT_CARE import org.jetbrains.kotlin.types.checker.JetTypeChecker import org.jetbrains.kotlin.types.checker.TypeCheckingProcedure import org.jetbrains.kotlin.types.checker.TypeCheckingProcedureCallbacks +import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.types.typeUtil.getNestedArguments import org.jetbrains.kotlin.types.typeUtil.isDefaultBound import java.util.* @@ -520,7 +521,7 @@ fun createTypeForFunctionPlaceholder( functionPlaceholderTypeConstructor.getArgumentTypes() } val receiverType = if (isExtension) DONT_CARE else null - return KotlinBuiltIns.getInstance().getFunctionType(Annotations.EMPTY, receiverType, newArgumentTypes, DONT_CARE) + return functionPlaceholder.builtIns.getFunctionType(Annotations.EMPTY, receiverType, newArgumentTypes, DONT_CARE) } private fun TypeSubstitutor.setApproximateCapturedTypes(): TypeSubstitutor { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt b/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt index f24f778f36f..99d85acdb81 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt @@ -23,7 +23,8 @@ import org.jetbrains.kotlin.resolve.calls.inference.isCaptured import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.JetTypeChecker -import java.util.ArrayList +import org.jetbrains.kotlin.types.typeUtil.builtIns +import java.util.* public data class ApproximationBounds( public val lower: T, @@ -39,10 +40,6 @@ private class TypeArgument( get() = JetTypeChecker.DEFAULT.isSubtypeOf(inProjection, outProjection) } -private val NULLABLE_ANY = KotlinBuiltIns.getInstance().getNullableAnyType() - -private val NOTHING = KotlinBuiltIns.getInstance().getNothingType() - private fun TypeArgument.toTypeProjection(): TypeProjection { assert(isConsistent) { "Only consistent enhanced type propection can be converted to type projection" } fun removeProjectionIfRedundant(variance: Variance) = if (variance == typeParameter.getVariance()) Variance.INVARIANT else variance @@ -58,8 +55,8 @@ private fun TypeArgument.toTypeProjection(): TypeProjection { private fun TypeProjection.toTypeArgument(typeParameter: TypeParameterDescriptor) = when (TypeSubstitutor.combine(typeParameter.getVariance(), getProjectionKind()) : Variance) { Variance.INVARIANT -> TypeArgument(typeParameter, getType(), getType()) - Variance.IN_VARIANCE -> TypeArgument(typeParameter, getType(), typeParameter.builtIns.getNullableAnyType()) - Variance.OUT_VARIANCE -> TypeArgument(typeParameter, typeParameter.builtIns.getNothingType(), getType()) + Variance.IN_VARIANCE -> TypeArgument(typeParameter, getType(), typeParameter.builtIns.nullableAnyType) + Variance.OUT_VARIANCE -> TypeArgument(typeParameter, typeParameter.builtIns.nothingType, type) } public fun approximateCapturedTypesIfNecessary(typeProjection: TypeProjection?): TypeProjection? { @@ -97,8 +94,8 @@ public fun approximateCapturedTypes(type: JetType): ApproximationBounds val bound = typeProjection.getType().makeNullableIfNeeded() return when (typeProjection.getProjectionKind()) { - Variance.IN_VARIANCE -> ApproximationBounds(bound, NULLABLE_ANY) - Variance.OUT_VARIANCE -> ApproximationBounds(NOTHING.makeNullableIfNeeded(), bound) + Variance.IN_VARIANCE -> ApproximationBounds(bound, type.builtIns.nullableAnyType) + Variance.OUT_VARIANCE -> ApproximationBounds(type.builtIns.nothingType.makeNullableIfNeeded(), bound) else -> throw AssertionError("Only nontrivial projections should have been captured, not: $typeProjection") } } @@ -114,7 +111,7 @@ public fun approximateCapturedTypes(type: JetType): ApproximationBounds } val lowerBoundIsTrivial = lowerBoundArguments.any { !it.isConsistent } return ApproximationBounds( - if (lowerBoundIsTrivial) NOTHING else type.replaceTypeArguments(lowerBoundArguments), + if (lowerBoundIsTrivial) type.builtIns.nothingType else type.replaceTypeArguments(lowerBoundArguments), type.replaceTypeArguments(upperBoundArguments)) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/CommonSupertypes.java b/core/descriptors/src/org/jetbrains/kotlin/types/CommonSupertypes.java index 49600c89e79..7d878c0c1ab 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/CommonSupertypes.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/CommonSupertypes.java @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.ClassifierDescriptor; import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor; import org.jetbrains.kotlin.descriptors.annotations.Annotations; +import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt; import org.jetbrains.kotlin.resolve.scopes.JetScope; import org.jetbrains.kotlin.types.checker.JetTypeChecker; import org.jetbrains.kotlin.types.typeUtil.TypeUtilPackage; @@ -67,13 +68,13 @@ public class CommonSupertypes { return max; } - private static int depth(@NotNull JetType type) { + private static int depth(@NotNull final JetType type) { return 1 + maxDepth(KotlinPackage.map(type.getArguments(), new Function1() { @Override public JetType invoke(TypeProjection projection) { if (projection.isStarProjection()) { // any type is good enough for depth here - return KotlinBuiltIns.getInstance().getAnyType(); + return type.getConstructor().getBuiltIns().getAnyType(); } return projection.getType(); } @@ -136,7 +137,8 @@ public class CommonSupertypes { // Everything deleted => it's Nothing or Nothing? if (typeSet.isEmpty()) { // TODO : attributes - return nullable ? KotlinBuiltIns.getInstance().getNullableNothingType() : KotlinBuiltIns.getInstance().getNothingType(); + KotlinBuiltIns builtIns = types.iterator().next().getConstructor().getBuiltIns(); + return nullable ? builtIns.getNullableNothingType() : builtIns.getNothingType(); } if (typeSet.size() == 1) { @@ -256,7 +258,7 @@ public class CommonSupertypes { if (recursionDepth >= maxDepth) { // If recursion is too deep, we cut it by taking as an ultimate supertype argument // Example: class A : Base; class B : Base, commonSuperType(A, B) = Base - return new TypeProjectionImpl(OUT_VARIANCE, KotlinBuiltIns.getInstance().getNullableAnyType()); + return new TypeProjectionImpl(OUT_VARIANCE, DescriptorUtilsKt.getBuiltIns(parameterDescriptor).getNullableAnyType()); } Set ins = new HashSet(); diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java index 1ca07c0bf8d..e4891384d66 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java @@ -161,7 +161,7 @@ public class TypeSubstitutor { throw new SubstitutionException("Out-projection in in-position"); case IN_IN_OUT_POSITION: // todo use the right type parameter variance and upper bound - return new TypeProjectionImpl(Variance.OUT_VARIANCE, KotlinBuiltIns.getInstance().getNullableAnyType()); + return new TypeProjectionImpl(Variance.OUT_VARIANCE, type.getConstructor().getBuiltIns().getNullableAnyType()); } } JetType substitutedType; diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java index ef292d5c1b0..b0072404fa6 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java @@ -482,15 +482,20 @@ public class TypeUtils { @Nullable private static JetType getDefaultPrimitiveNumberType(@NotNull Collection supertypes) { - JetType doubleType = KotlinBuiltIns.getInstance().getDoubleType(); + if (supertypes.isEmpty()) { + return null; + } + + KotlinBuiltIns builtIns = supertypes.iterator().next().getConstructor().getBuiltIns(); + JetType doubleType = builtIns.getDoubleType(); if (supertypes.contains(doubleType)) { return doubleType; } - JetType intType = KotlinBuiltIns.getInstance().getIntType(); + JetType intType = builtIns.getIntType(); if (supertypes.contains(intType)) { return intType; } - JetType longType = KotlinBuiltIns.getInstance().getLongType(); + JetType longType = builtIns.getLongType(); if (supertypes.contains(longType)) { return longType; } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt index 7d62c90b09a..546cb50c360 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt @@ -43,6 +43,9 @@ public fun JetType.nullability(): TypeNullability { } } +val JetType.builtIns: KotlinBuiltIns + get() = constructor.builtIns + fun JetType.makeNullable() = TypeUtils.makeNullable(this) fun JetType.makeNotNullable() = TypeUtils.makeNotNullable(this)