From aa897db132eae5d0b19b28e8d08d7730a47b543f Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 10 Sep 2020 15:07:11 +0300 Subject: [PATCH] [FIR] Introduce UnsafeVariance attribute (see KT-41792) --- .../kotlin/fir/types/CompilerConeAttributes.kt | 12 +++++++++++- .../src/org/jetbrains/kotlin/fir/types/TypeUtils.kt | 10 ++++++++++ .../org/jetbrains/kotlin/fir/types/FirTypeUtils.kt | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt index b5a8011f399..ac42940b8e6 100644 --- a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.fir.types import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.utils.addToStdlib.runIf import kotlin.reflect.KClass object CompilerConeAttributes { @@ -41,8 +40,19 @@ object CompilerConeAttributes { override val key: KClass = ExtensionFunctionType::class } + + object UnsafeVariance : ConeAttribute() { + val ANNOTATION_CLASS_ID = ClassId(FqName("kotlin"), Name.identifier("UnsafeVariance")) + + override fun union(other: UnsafeVariance?): UnsafeVariance? = null + override fun intersect(other: UnsafeVariance?): UnsafeVariance? = null + override fun isSubtypeOf(other: UnsafeVariance?): Boolean = true + + override val key: KClass = UnsafeVariance::class + } } val ConeAttributes.exact: CompilerConeAttributes.Exact? by ConeAttributes.attributeAccessor() val ConeAttributes.noInfer: CompilerConeAttributes.NoInfer? by ConeAttributes.attributeAccessor() val ConeAttributes.extensionFunctionType: CompilerConeAttributes.ExtensionFunctionType? by ConeAttributes.attributeAccessor() +val ConeAttributes.unsafeVarianceType: CompilerConeAttributes.UnsafeVariance? by ConeAttributes.attributeAccessor() diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt index d2dec4d45e0..9448aa1ea46 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt @@ -161,3 +161,13 @@ fun ConeKotlinType.isExtensionFunctionType(session: FirSession): Boolean { fun FirTypeRef.isExtensionFunctionType(session: FirSession): Boolean { return coneTypeSafe()?.isExtensionFunctionType(session) == true } + +fun ConeKotlinType.isUnsafeVarianceType(session: FirSession): Boolean { + val type = this.lowerBoundIfFlexible().fullyExpandedType(session) + return type.attributes.unsafeVarianceType != null +} + +fun FirTypeRef.isUnsafeVarianceType(session: FirSession): Boolean { + return coneTypeSafe()?.isUnsafeVarianceType(session) == true +} + diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirTypeUtils.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirTypeUtils.kt index c19664af276..ad124b8b506 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirTypeUtils.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirTypeUtils.kt @@ -96,6 +96,7 @@ fun List.computeTypeAttributes(): ConeAttributes { CompilerConeAttributes.Exact.ANNOTATION_CLASS_ID -> attributes += CompilerConeAttributes.Exact CompilerConeAttributes.NoInfer.ANNOTATION_CLASS_ID -> attributes += CompilerConeAttributes.NoInfer CompilerConeAttributes.ExtensionFunctionType.ANNOTATION_CLASS_ID -> attributes += CompilerConeAttributes.ExtensionFunctionType + CompilerConeAttributes.UnsafeVariance.ANNOTATION_CLASS_ID -> attributes += CompilerConeAttributes.UnsafeVariance } } return ConeAttributes.create(attributes)