From 13bff10567e802617e755a0abba112bcf4ac546c Mon Sep 17 00:00:00 2001 From: Irene Dea Date: Mon, 20 Dec 2021 02:03:54 -0800 Subject: [PATCH] Fix union related failures. --- .../kotlin/fir/types/ConeInferenceContext.kt | 19 +++++++++++-------- .../jetbrains/kotlin/fir/types/TypeUtils.kt | 4 ++++ .../kotlin/ir/types/IrTypeSystemContext.kt | 2 +- .../calls/NewCommonSuperTypeCalculator.kt | 2 +- .../kotlin/types/model/TypeSystemContext.kt | 2 +- .../kotlin/types/AnnotationsTypeAttribute.kt | 4 ++-- .../types/checker/ClassicTypeSystemContext.kt | 8 +++++--- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt index bf8859163de..1796015c7b7 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt @@ -405,16 +405,19 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo return (types as List).map { it.attributes }.reduce { x, y -> x.union(y) }.toList() } - override fun KotlinTypeMarker.replaceTypeAttributes(newAttributes: List): KotlinTypeMarker { + private fun AnnotationMarker.isCustomAttribute(): Boolean { + val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute + return this !in compilerAttributes && this !is CustomAnnotationTypeAttribute + } + + override fun KotlinTypeMarker.replaceCustomAttributes(newAttributes: List): KotlinTypeMarker { require(this is ConeKotlinType) - if (newAttributes.isEmpty()) return this @Suppress("UNCHECKED_CAST") - return createSimpleType( - this.typeConstructor(), - this.getArguments(), - this.isNullable, - this.isExtensionFunctionType, - newAttributes as List> + val newCustomAttributes = (newAttributes as List>).filter { it.isCustomAttribute() } + val attributesToKeep = this.attributes.filterNot { it.isCustomAttribute() } + return withAttributes( + ConeAttributes.create(newCustomAttributes + attributesToKeep), + this@ConeInferenceContext ) } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt index bee86c01c1a..870b3fac6f0 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt @@ -113,6 +113,10 @@ fun T.withAttributes(attributes: ConeAttributes, typeSystem is ConeClassLikeTypeImpl -> ConeClassLikeTypeImpl(lookupTag, typeArguments, nullability.isNullable, attributes) is ConeDefinitelyNotNullType -> ConeDefinitelyNotNullType(original.withAttributes(attributes, typeSystemContext)) is ConeTypeParameterTypeImpl -> ConeTypeParameterTypeImpl(lookupTag, nullability.isNullable, attributes) + is ConeRawType -> ConeRawType( + lowerBound.withAttributes(attributes, typeSystemContext), + upperBound.withAttributes(attributes, typeSystemContext) + ) is ConeFlexibleType -> ConeFlexibleType( lowerBound.withAttributes(attributes, typeSystemContext), upperBound.withAttributes(attributes, typeSystemContext) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt index 56837cc4936..e716747a79b 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt @@ -383,7 +383,7 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon override fun findCommonIntegerLiteralTypesSuperType(explicitSupertypes: List): SimpleTypeMarker? = irBuiltIns.intType as IrSimpleType - override fun KotlinTypeMarker.replaceTypeAttributes(newAttributes: List): KotlinTypeMarker = this + override fun KotlinTypeMarker.replaceCustomAttributes(newAttributes: List): KotlinTypeMarker = this override fun unionTypeAttributes(types: List): List = emptyList() diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt index 1b9051e4a62..17b63553b94 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt @@ -15,7 +15,7 @@ import org.jetbrains.kotlin.types.model.* object NewCommonSuperTypeCalculator { fun TypeSystemCommonSuperTypesContext.commonSuperType(types: List): KotlinTypeMarker { val maxDepth = types.maxOfOrNull { it.typeDepth() } ?: 0 - return commonSuperType(types, -maxDepth, true).let { it.replaceTypeAttributes(unionTypeAttributes(listOf(it) + types)) } + return commonSuperType(types, -maxDepth, true).replaceCustomAttributes(unionTypeAttributes(types)) } private fun TypeSystemCommonSuperTypesContext.commonSuperType( diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index a7b929e9c7c..9a42a4b2199 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -136,7 +136,7 @@ interface TypeSystemCommonSuperTypesContext : TypeSystemContext, TypeSystemTypeF fun unionTypeAttributes(types: List): List - fun KotlinTypeMarker.replaceTypeAttributes(newAttributes: List): KotlinTypeMarker + fun KotlinTypeMarker.replaceCustomAttributes(newAttributes: List): KotlinTypeMarker } // This interface is only used to declare that implementing class is supposed to be used as a TypeSystemInferenceExtensionContext component diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt b/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt index 8bc7630d753..d4379c4d0ff 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt @@ -10,9 +10,9 @@ import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations import kotlin.reflect.KClass class AnnotationsTypeAttribute(val annotations: Annotations) : TypeAttribute() { - override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute = this + override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null - override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute = this + override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null override fun add(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute { if (other == null) return this diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index 1b32aad3973..cb33caeb1bd 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.resolve.unsubstitutedUnderlyingType import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.types.typeUtil.* +import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.types.typeUtil.isSignedOrUnsignedNumberType as classicIsSignedOrUnsignedNumberType @@ -644,12 +645,13 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy return (types as List).map { it.unwrap().attributes }.reduce { x, y -> x.union(y) }.toList() } - override fun KotlinTypeMarker.replaceTypeAttributes(newAttributes: List): KotlinTypeMarker { + override fun KotlinTypeMarker.replaceCustomAttributes(newAttributes: List): KotlinTypeMarker { require(this is KotlinType) - if (newAttributes.isEmpty()) return this @Suppress("UNCHECKED_CAST") + val attributes = (newAttributes as List>).filterNot { it is AnnotationsTypeAttribute }.toMutableList() + attributes.addIfNotNull(this.attributes.annotationsAttribute) return this.unwrap().replaceAttributes( - TypeAttributes.create(newAttributes as List>) + TypeAttributes.create(attributes) ) }