Fix failed tests

This commit is contained in:
Irene Dea
2021-12-06 19:44:52 -08:00
committed by Dmitriy Novozhilov
parent 3c4989b672
commit a98e2c4e03
22 changed files with 78 additions and 73 deletions
@@ -58,10 +58,10 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
arguments: List<TypeArgumentMarker>,
nullable: Boolean,
isExtensionFunction: Boolean,
annotations: List<AnnotationMarker>?
attributes: List<AnnotationMarker>?
): SimpleTypeMarker {
val attributesList = annotations?.filterIsInstanceTo<ConeAttribute<*>, MutableList<ConeAttribute<*>>>(mutableListOf())
val attributes: ConeAttributes = if (isExtensionFunction) {
val attributesList = attributes?.filterIsInstanceTo<ConeAttribute<*>, MutableList<ConeAttribute<*>>>(mutableListOf())
val coneAttributes: ConeAttributes = if (isExtensionFunction) {
require(constructor is ConeClassLikeLookupTag && constructor.isBuiltinFunctionalType())
// We don't want to create new instance of ConeAttributes which
// contains only CompilerConeAttributes.ExtensionFunctionType
@@ -81,12 +81,12 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
constructor,
(arguments as List<ConeTypeProjection>).toTypedArray(),
nullable,
attributes,
coneAttributes,
)
is ConeTypeParameterLookupTag -> ConeTypeParameterTypeImpl(
constructor,
nullable,
attributes
coneAttributes
)
else -> error("!")
}
@@ -420,6 +420,12 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
return attributes.toList()
}
override fun KotlinTypeMarker.hasCustomAttributes(): Boolean {
require(this is ConeKotlinType)
val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute
return this.attributes.any { it !in compilerAttributes && it !is CustomAnnotationTypeAttribute }
}
override fun KotlinTypeMarker.getCustomAttributes(): List<AnnotationMarker> {
require(this is ConeKotlinType)
val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute
@@ -145,7 +145,7 @@ class PassingProgressionAsCollectionCallChecker(private val kotlinCallResolver:
private fun makeCollectionOfAnyType(builtIns: KotlinBuiltIns): KotlinType =
KotlinTypeFactory.simpleNotNullType(
Annotations.EMPTY,
TypeAttributes.Empty,
builtIns.collection,
listOf(TypeProjectionImpl(builtIns.nullableAnyType))
)
@@ -162,7 +162,7 @@ class PassingProgressionAsCollectionCallChecker(private val kotlinCallResolver:
val newType = intersectTypes(
listOf(
KotlinTypeFactory.simpleNotNullType(
Annotations.EMPTY,
TypeAttributes.Empty,
builtIns.collection,
listOf(TypeProjectionImpl(progressionOrRangeElementType))
),
@@ -332,10 +332,10 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
arguments: List<TypeArgumentMarker>,
nullable: Boolean,
isExtensionFunction: Boolean,
annotations: List<AnnotationMarker>?
attributes: List<AnnotationMarker>?
): SimpleTypeMarker {
val ourAnnotations = annotations?.filterIsInstance<IrConstructorCall>()
require(ourAnnotations?.size == annotations?.size)
val ourAnnotations = attributes?.filterIsInstance<IrConstructorCall>()
require(ourAnnotations?.size == attributes?.size)
return IrSimpleTypeImpl(
constructor as IrClassifierSymbol,
nullable,
@@ -406,6 +406,10 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
return this.annotations.map { object : AnnotationMarker, IrElement by it {} }
}
override fun KotlinTypeMarker.hasCustomAttributes(): Boolean {
return false
}
override fun KotlinTypeMarker.getCustomAttributes(): List<AnnotationMarker> {
require(this is IrType)
return emptyList()
@@ -15,7 +15,7 @@ import org.jetbrains.kotlin.types.model.*
object NewCommonSuperTypeCalculator {
fun TypeSystemCommonSuperTypesContext.commonSuperType(types: List<KotlinTypeMarker>): KotlinTypeMarker {
val maxDepth = types.maxOfOrNull { it.typeDepth() } ?: 0
return commonSuperType(types, -maxDepth, true).replaceTypeAttributes(unionTypeAttributes(types))
return commonSuperType(types, -maxDepth, true).let { it.replaceTypeAttributes(unionTypeAttributes(listOf(it) + types)) }
}
private fun TypeSystemCommonSuperTypesContext.commonSuperType(
@@ -411,7 +411,7 @@ class PostponedArgumentInputTypesResolver(
argument.isFunctionExpressionWithReceiver() -> true
else -> parameterTypesInfo.isExtensionFunction
},
annotations = parameterTypesInfo.annotations
attributes = parameterTypesInfo.annotations
)
getBuilder().addSubtypeConstraint(
@@ -59,14 +59,18 @@ class MutableVariableWithConstraints private constructor(
&& previousConstraint.type == constraint.type
&& previousConstraint.isNullabilityConstraint == constraint.isNullabilityConstraint
) {
val noNewCustomAttributes = with(context) {
val previousType = previousConstraint.type
val type = constraint.type
(!previousType.hasCustomAttributes() && !type.hasCustomAttributes()) ||
(previousType.getCustomAttributes() == type.getCustomAttributes())
}
if (newConstraintIsUseless(previousConstraint, constraint)) {
// Preserve constraints with different custom type attributes.
// This allows us to union type attributes in NewCommonSuperTypeCalculator.kt
with(context) {
val prevAttributes = previousConstraint.type.getCustomAttributes()
if (prevAttributes.isEmpty() || prevAttributes == constraint.type.getCustomAttributes()) {
return previousConstraint to false
}
if (noNewCustomAttributes) {
return previousConstraint to false
}
}
@@ -75,7 +79,7 @@ class MutableVariableWithConstraints private constructor(
ConstraintKind.UPPER -> constraint.kind.isLower()
ConstraintKind.EQUALITY -> true
}
if (isMatchingForSimplification) {
if (isMatchingForSimplification && noNewCustomAttributes) {
val actualConstraint = if (constraint.kind != ConstraintKind.EQUALITY) {
Constraint(
ConstraintKind.EQUALITY,
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations
import org.jetbrains.kotlin.resolve.calls.inference.isCaptured
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor
import org.jetbrains.kotlin.resolve.calls.inference.substitute
@@ -80,7 +80,7 @@ interface TypeSystemTypeFactoryContext: TypeSystemBuiltInsContext {
arguments: List<TypeArgumentMarker>,
nullable: Boolean,
isExtensionFunction: Boolean = false,
annotations: List<AnnotationMarker>? = null
attributes: List<AnnotationMarker>? = null
): SimpleTypeMarker
fun createTypeArgument(type: KotlinTypeMarker, variance: TypeVariance): TypeArgumentMarker
@@ -500,6 +500,8 @@ interface TypeSystemContext : TypeSystemOptimizationContext {
fun KotlinTypeMarker.getAttributes(): List<AnnotationMarker>
fun KotlinTypeMarker.hasCustomAttributes(): Boolean
fun KotlinTypeMarker.getCustomAttributes(): List<AnnotationMarker>
fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy
@@ -78,9 +78,6 @@ class CapturedType(
override val attributes: TypeAttributes = TypeAttributes.Empty
) : SimpleType(), SubtypingRepresentatives, CapturedTypeMarker {
override val annotations: Annotations
get() = attributes.toDefaultAnnotations()
override val arguments: List<TypeProjection>
get() = listOf()
@@ -5,31 +5,24 @@
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations
import kotlin.reflect.KClass
class AnnotationsTypeAttribute(val annotations: Annotations) : TypeAttribute<AnnotationsTypeAttribute>() {
constructor(annotations: List<AnnotationDescriptor>) : this(Annotations.create(annotations))
override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute = this
override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null
override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null
override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute = this
override fun add(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute {
if (other == null) return this
return AnnotationsTypeAttribute(Annotations.create(annotations + other.annotations))
return AnnotationsTypeAttribute(composeAnnotations(annotations, other.annotations))
}
override fun isSubtypeOf(other: AnnotationsTypeAttribute?): Boolean = true
override val key: KClass<out AnnotationsTypeAttribute>
get() = AnnotationsTypeAttribute::class
override fun equals(other: Any?): Boolean {
if (other !is AnnotationsTypeAttribute) return false
return annotations == other.annotations
}
}
val TypeAttributes.annotationsAttribute: AnnotationsTypeAttribute? by TypeAttributes.attributeAccessor<AnnotationsTypeAttribute>()
@@ -27,9 +27,6 @@ open class ErrorType @JvmOverloads internal constructor(
override val isMarkedNullable: Boolean = false,
open val presentableName: String = "???"
) : SimpleType() {
override val annotations: Annotations
get() = Annotations.EMPTY
override val attributes: TypeAttributes
get() = TypeAttributes.Empty
@@ -52,6 +52,8 @@ sealed class KotlinType : Annotated, KotlinTypeMarker {
abstract val isMarkedNullable: Boolean
abstract val memberScope: MemberScope
abstract val attributes: TypeAttributes
override val annotations: Annotations
get() = attributes.annotations
abstract fun unwrap(): UnwrappedType
@@ -125,7 +127,6 @@ abstract class WrappedType : KotlinType() {
open fun isComputed(): Boolean = true
protected abstract val delegate: KotlinType
override val annotations: Annotations get() = delegate.annotations
override val constructor: TypeConstructor get() = delegate.constructor
override val arguments: List<TypeProjection> get() = delegate.arguments
override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable
@@ -210,12 +211,11 @@ abstract class FlexibleType(val lowerBound: SimpleType, val upperBound: SimpleTy
abstract fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String
override val annotations: Annotations get() = delegate.annotations
override val attributes: TypeAttributes get() = delegate.attributes
override val constructor: TypeConstructor get() = delegate.constructor
override val arguments: List<TypeProjection> get() = delegate.arguments
override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable
override val memberScope: MemberScope get() = delegate.memberScope
override val attributes: TypeAttributes get() = delegate.attributes
override fun toString(): String = DescriptorRenderer.DEBUG_TEXT.renderType(this)
@@ -206,7 +206,6 @@ private class SimpleTypeImpl(
@TypeRefinement
override val hasNotTrivialRefinementFactory: Boolean get() = true
override val annotations: Annotations get() = Annotations.EMPTY
override val attributes: TypeAttributes get() = TypeAttributes.Empty
override fun replaceAttributes(newAttributes: TypeAttributes) =
@@ -254,7 +253,6 @@ class SupposititiousSimpleType(private val realType: SimpleType, val overwritten
override val arguments: List<TypeProjection> = realType.arguments
override val isMarkedNullable: Boolean = realType.isMarkedNullable
override val memberScope: MemberScope = realType.memberScope
override val annotations: Annotations = realType.annotations
override val attributes: TypeAttributes get() = realType.attributes
}
@@ -275,8 +273,6 @@ private class SimpleTypeWithAttributes(
delegate: SimpleType,
override val attributes: TypeAttributes
) : DelegatingSimpleTypeImpl(delegate) {
override val annotations: Annotations
get() = attributes.toDefaultAnnotations()
@TypeRefinement
override fun replaceDelegate(delegate: SimpleType) = SimpleTypeWithAttributes(delegate, attributes)
}
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.types.model.DefinitelyNotNullTypeMarker
abstract class DelegatingSimpleType : SimpleType() {
protected abstract val delegate: SimpleType
override val annotations: Annotations get() = delegate.annotations
override val constructor: TypeConstructor get() = delegate.constructor
override val arguments: List<TypeProjection> get() = delegate.arguments
override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable
@@ -58,9 +58,6 @@ abstract class AbstractStubType(val originalTypeVariable: TypeConstructor, overr
override val arguments: List<TypeProjection>
get() = emptyList()
override val annotations: Annotations
get() = Annotations.EMPTY
override val attributes: TypeAttributes
get() = TypeAttributes.Empty
@@ -135,7 +135,7 @@ class TypeAliasExpander(
replaceAttributes(createdCombinedAttributes(newAttributes))
private fun SimpleType.combineAttributes(newAttributes: TypeAttributes): SimpleType =
if (isError) this else replaceAttributes(newAttributes = createdCombinedAttributes(newAttributes))
if (isError) this else replace(newAttributes = createdCombinedAttributes(newAttributes))
private fun KotlinType.createdCombinedAttributes(newAttributes: TypeAttributes): TypeAttributes {
if (isError) return attributes
@@ -25,7 +25,9 @@ object DefaultTypeAttributeTranslator : TypeAttributeTranslator {
typeConstructor: TypeConstructor?,
containingDeclaration: DeclarationDescriptor?
): TypeAttributes {
return TypeAttributes.create(listOf(AnnotationsTypeAttribute(annotations)))
return if (annotations.isEmpty())
TypeAttributes.Empty else
TypeAttributes.create(listOf(AnnotationsTypeAttribute(annotations)))
}
override fun toAnnotations(attributes: TypeAttributes): Annotations {
@@ -51,7 +51,7 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
}
}
val Empty: TypeAttributes = TypeAttributes(listOf(AnnotationsTypeAttribute(Annotations.EMPTY)))
val Empty: TypeAttributes = TypeAttributes(emptyList())
fun create(attributes: List<TypeAttribute<*>>): TypeAttributes {
return if (attributes.isEmpty()) {
@@ -70,6 +70,8 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
}
}
val size: Int get() = arrayMap.size
fun union(other: TypeAttributes): TypeAttributes {
return perform(other) { this.union(it) }
}
@@ -87,15 +89,11 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
return arrayMap[index] != null
}
@OptIn(ExperimentalStdlibApi::class)
operator fun plus(attribute: TypeAttribute<*>): TypeAttributes {
if (attribute in this) return this
if (isEmpty()) return TypeAttributes(attribute)
val newAttributes = buildList {
addAll(this)
add(attribute)
}
return TypeAttributes(newAttributes)
val newAttributes = this.toList() + attribute
return create(newAttributes)
}
fun remove(attribute: TypeAttribute<*>): TypeAttributes {
@@ -119,6 +117,7 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
override val typeRegistry: TypeRegistry<TypeAttribute<*>, TypeAttribute<*>>
get() = Companion
}
fun TypeAttributes.toDefaultAnnotations(): Annotations =
@@ -127,6 +126,9 @@ fun TypeAttributes.toDefaultAnnotations(): Annotations =
fun Annotations.toDefaultAttributes(): TypeAttributes = DefaultTypeAttributeTranslator.toAttributes(this)
fun TypeAttributes.replaceAnnotations(newAnnotations: Annotations): TypeAttributes {
val withoutCustom = (annotationsAttribute?.let { this.remove(it) } ?: this)
return withoutCustom.add(newAnnotations.toDefaultAttributes())
if (annotations === newAnnotations) return this
val withoutAnnotations = annotationsAttribute?.let { this.remove(it) } ?: this
// Check if iterator hasNext to handle FilteredAnnotations.isEmpty() with OldInference
if (!newAnnotations.iterator().hasNext() && newAnnotations.isEmpty()) return withoutAnnotations
return withoutAnnotations.plus(AnnotationsTypeAttribute(newAnnotations))
}
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
abstract class TypeSubstitution {
companion object {
@@ -50,6 +51,7 @@ abstract class TypeSubstitution {
override fun filterAnnotations(annotations: Annotations) = this@TypeSubstitution.filterAnnotations(annotations)
override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) =
this@TypeSubstitution.prepareTopLevelType(topLevelType, position)
override fun isEmpty() = this@TypeSubstitution.isEmpty()
}
}
@@ -144,23 +146,26 @@ fun KotlinType.replace(
): KotlinType {
if ((newArguments.isEmpty() || newArguments === arguments) && newAnnotations === annotations) return this
val newAttributes = attributes.replaceAnnotations(
// Specially handle FilteredAnnotations here due to FilteredAnnotations.isEmpty()
if (newAnnotations is FilteredAnnotations && newAnnotations.isEmpty()) Annotations.EMPTY else newAnnotations
)
return when (val unwrapped = unwrap()) {
is FlexibleType -> KotlinTypeFactory.flexibleType(
unwrapped.lowerBound.replace(newArguments, newAnnotations),
unwrapped.upperBound.replace(newArgumentsForUpperBound, newAnnotations)
unwrapped.lowerBound.replace(newArguments, newAttributes),
unwrapped.upperBound.replace(newArgumentsForUpperBound, newAttributes)
)
is SimpleType -> unwrapped.replace(newArguments, newAnnotations)
is SimpleType -> unwrapped.replace(newArguments, newAttributes)
}
}
@JvmOverloads
fun SimpleType.replace(
newArguments: List<TypeProjection> = arguments,
newAnnotations: Annotations = annotations
newAttributes: TypeAttributes = attributes
): SimpleType {
if (newArguments.isEmpty() && newAnnotations === annotations) return this
val newAttributes = attributes.replaceAnnotations(newAnnotations)
if (newArguments.isEmpty() && newAttributes === attributes) return this
if (newArguments.isEmpty()) {
return replaceAttributes(newAttributes)
@@ -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.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.types.typeUtil.isSignedOrUnsignedNumberType as classicIsSignedOrUnsignedNumberType
@@ -516,12 +517,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
arguments: List<TypeArgumentMarker>,
nullable: Boolean,
isExtensionFunction: Boolean,
annotations: List<AnnotationMarker>?
attributes: List<AnnotationMarker>?
): SimpleTypeMarker {
require(constructor is TypeConstructor, constructor::errorMessage)
val ourAnnotations = annotations?.filterIsInstance<AnnotationDescriptor>()
require(ourAnnotations?.size == annotations?.size)
val ourAnnotations = attributes?.firstIsInstanceOrNull<AnnotationsTypeAttribute>()?.annotations?.toList()
fun createExtensionFunctionAnnotation() = BuiltInAnnotationDescriptor(builtIns, FqNames.extensionFunctionType, emptyMap())
@@ -673,6 +673,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
return this.attributes.toList()
}
override fun KotlinTypeMarker.hasCustomAttributes(): Boolean {
require(this is KotlinType, this::errorMessage)
return this.attributes.size > 1
}
override fun KotlinTypeMarker.getCustomAttributes(): List<AnnotationMarker> {
require(this is KotlinType, this::errorMessage)
return this.attributes.filterNot { it is AnnotationsTypeAttribute }
@@ -207,16 +207,13 @@ class NewCapturedType(
captureStatus: CaptureStatus, lowerType: UnwrappedType?, projection: TypeProjection, typeParameter: TypeParameterDescriptor
) : this(captureStatus, NewCapturedTypeConstructor(projection, typeParameter = typeParameter), lowerType)
override val annotations: Annotations
get() = attributes.toDefaultAnnotations()
override val arguments: List<TypeProjection> get() = listOf()
override val memberScope: MemberScope // todo what about foo().bar() where foo() return captured type?
get() = ErrorUtils.createErrorScope("No member resolution should be done on captured type!", true)
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable)
NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable, isProjectionNotNull)
override fun makeNullableAsSpecified(newNullability: Boolean) =
NewCapturedType(captureStatus, constructor, lowerType, attributes, newNullability)