FIR: drop some usages of ConeTypeContext

This commit is contained in:
Mikhail Glukhikh
2021-12-13 18:42:16 +03:00
committed by TeamCityServer
parent 81ff9b820a
commit d1194e5fd2
11 changed files with 44 additions and 64 deletions
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
import org.jetbrains.kotlin.types.Variance
import kotlin.reflect.KClass
/**
@@ -57,11 +56,12 @@ fun checkUpperBoundViolated(
val typeRefAndSourcesForArguments = extractArgumentsTypeRefAndSource(typeRef) ?: return
val typeArgumentsWithSourceInfo = type.typeArguments.withIndex().map { (index, projection) ->
val (argTypeRef, source) =
if (!isAbbreviatedType)
if (!isAbbreviatedType) {
typeRefAndSourcesForArguments.getOrNull(index) ?: return
else
} else {
// For abbreviated arguments we use the whole typeRef as a place to report
FirTypeRefSource(null, typeRef.source)
}
TypeArgumentWithSourceInfo(projection, argTypeRef, source)
}
@@ -86,8 +86,7 @@ private class FE10LikeConeSubstitutor(
}
val result =
projection.type!!.updateNullabilityIfNeeded(type)
?.withCombinedAttributesFrom(type, useSiteSession.typeContext)
projection.type!!.updateNullabilityIfNeeded(type)?.withCombinedAttributesFrom(type)
?: return null
if (type.isUnsafeVarianceType(useSiteSession)) {
@@ -103,7 +102,7 @@ private class FE10LikeConeSubstitutor(
private fun ConeKotlinType.withProjection(projection: ConeTypeProjection): ConeKotlinType {
if (projection.kind == ProjectionKind.INVARIANT) return this
return withAttributes(ConeAttributes.create(listOf(OriginalProjectionTypeAttribute(projection))), useSiteSession.typeContext)
return withAttributes(ConeAttributes.create(listOf(OriginalProjectionTypeAttribute(projection))))
}
override fun substituteArgument(projection: ConeTypeProjection, lookupTag: ConeClassLikeLookupTag, index: Int): ConeTypeProjection? {
@@ -246,10 +246,7 @@ class ConeTypeSystemCommonBackendContextForTypeMapping(
require(it is ConeKotlinType)
}
@Suppress("UNCHECKED_CAST")
return defaultType().withArguments(
(arguments as List<ConeKotlinType>).toTypedArray(),
session.typeContext,
)
return defaultType().withArguments((arguments as List<ConeKotlinType>).toTypedArray())
}
override fun TypeParameterMarker.representativeUpperBound(): ConeKotlinType {
@@ -76,7 +76,7 @@ fun FirSymbolProvider.getSymbolByLookupTag(lookupTag: ConeClassLikeLookupTag): F
return lookupTag.toSymbol(session)
}
fun ConeKotlinType.withParameterNameAnnotation(valueParameter: FirValueParameter, context: ConeTypeContext): ConeKotlinType {
fun ConeKotlinType.withParameterNameAnnotation(valueParameter: FirValueParameter): ConeKotlinType {
if (valueParameter.name == SpecialNames.NO_NAME_PROVIDED || valueParameter.name == SpecialNames.UNDERSCORE_FOR_UNUSED_VAR) return this
// Existing @ParameterName annotation takes precedence
if (attributes.customAnnotations.getAnnotationsByClassId(StandardNames.FqNames.parameterNameClassId).isNotEmpty()) return this
@@ -100,16 +100,16 @@ fun ConeKotlinType.withParameterNameAnnotation(valueParameter: FirValueParameter
}
val attributesWithParameterNameAnnotation =
ConeAttributes.create(listOf(CustomAnnotationTypeAttribute(listOf(parameterNameAnnotationCall))))
return withCombinedAttributesFrom(attributesWithParameterNameAnnotation, context)
return withCombinedAttributesFrom(attributesWithParameterNameAnnotation)
}
fun ConeKotlinType.withCombinedAttributesFrom(other: ConeKotlinType, context: ConeTypeContext): ConeKotlinType =
withCombinedAttributesFrom(other.attributes, context)
fun ConeKotlinType.withCombinedAttributesFrom(other: ConeKotlinType): ConeKotlinType =
withCombinedAttributesFrom(other.attributes)
private fun ConeKotlinType.withCombinedAttributesFrom(other: ConeAttributes, context: ConeTypeContext): ConeKotlinType {
private fun ConeKotlinType.withCombinedAttributesFrom(other: ConeAttributes): ConeKotlinType {
if (other.isEmpty()) return this
val combinedConeAttributes = attributes.add(other)
return withAttributes(combinedConeAttributes, context)
return withAttributes(combinedConeAttributes)
}
fun ConeKotlinType.findClassRepresentation(
@@ -77,7 +77,7 @@ fun ConeClassLikeType.directExpansionType(
val resultType = expandedConeType(typeAlias)
?.applyNullabilityFrom(useSiteSession, this)
?.applyAttributesFrom(useSiteSession, this)
?.applyAttributesFrom(this)
?: return null
if (resultType.typeArguments.isEmpty()) return resultType
@@ -93,11 +93,10 @@ private fun ConeClassLikeType.applyNullabilityFrom(
}
private fun ConeClassLikeType.applyAttributesFrom(
session: FirSession,
abbreviation: ConeClassLikeType
): ConeClassLikeType {
val combinedAttributes = attributes.add(abbreviation.attributes)
return withAttributes(combinedAttributes, session.typeContext)
return withAttributes(combinedAttributes)
}
private fun mapTypeAliasArguments(
@@ -135,7 +134,7 @@ private fun mapTypeAliasArguments(
is ConeDefinitelyNotNullType,
is ConeTypeParameterTypeImpl,
is ConeFlexibleType -> {
mappedType.withAttributes(type.attributes.add(mappedType.attributes), useSiteSession.typeContext)
mappedType.withAttributes(type.attributes.add(mappedType.attributes))
}
null -> return mappedProjection
else -> mappedType
@@ -105,7 +105,7 @@ abstract class AbstractConeSubstitutor(protected val typeContext: ConeTypeContex
private fun ConeDefinitelyNotNullType.substituteOriginal(): ConeKotlinType? {
val substituted = substituteOrNull(original)
?.withNullability(ConeNullability.NOT_NULL, typeContext)
?.withAttributes(original.attributes, typeContext)
?.withAttributes(original.attributes)
?: return null
return ConeDefinitelyNotNullType.create(substituted, typeContext) ?: substituted
}
@@ -193,7 +193,7 @@ class ConeSubstitutorByMap(
if (type !is ConeTypeParameterType) return null
val result =
substitution[type.lookupTag.symbol].updateNullabilityIfNeeded(type)
?.withCombinedAttributesFrom(type, useSiteSession.typeContext)
?.withCombinedAttributesFrom(type)
?: return null
if (type.isUnsafeVarianceType(useSiteSession)) {
return useSiteSession.typeApproximator.approximateToSuperType(
@@ -223,7 +223,7 @@ fun createTypeSubstitutorByTypeConstructor(map: Map<TypeConstructorMarker, ConeK
override fun substituteType(type: ConeKotlinType): ConeKotlinType? {
if (type !is ConeLookupTagBasedType && type !is ConeStubType) return null
val new = map[type.typeConstructor(context)] ?: return null
return new.approximateIntegerLiteralType().updateNullabilityIfNeeded(type)?.withCombinedAttributesFrom(type, context)
return new.approximateIntegerLiteralType().updateNullabilityIfNeeded(type)?.withCombinedAttributesFrom(type)
}
}
}
@@ -235,7 +235,7 @@ internal class TypeSubstitutorByTypeConstructor(
override fun substituteType(type: ConeKotlinType): ConeKotlinType? {
if (type !is ConeLookupTagBasedType && type !is ConeStubType) return null
val new = map[type.typeConstructor(typeContext)] ?: return null
return new.approximateIntegerLiteralType().updateNullabilityIfNeeded(type)?.withCombinedAttributesFrom(type, typeContext)
return new.approximateIntegerLiteralType().updateNullabilityIfNeeded(type)?.withCombinedAttributesFrom(type)
}
}
@@ -274,17 +274,17 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
override fun KotlinTypeMarker.removeAnnotations(): KotlinTypeMarker {
require(this is ConeKotlinType)
return withAttributes(ConeAttributes.Empty, this@ConeInferenceContext)
return withAttributes(ConeAttributes.Empty)
}
override fun SimpleTypeMarker.replaceArguments(newArguments: List<TypeArgumentMarker>): SimpleTypeMarker {
require(this is ConeKotlinType)
return this.withArguments(newArguments.cast<List<ConeTypeProjection>>().toTypedArray(), this@ConeInferenceContext)
return this.withArguments(newArguments.cast<List<ConeTypeProjection>>().toTypedArray())
}
override fun SimpleTypeMarker.replaceArguments(replacement: (TypeArgumentMarker) -> TypeArgumentMarker): SimpleTypeMarker {
require(this is ConeKotlinType)
return this.withArguments({ replacement(it).cast() }, this@ConeInferenceContext)
return this.withArguments { replacement(it) as ConeTypeProjection }
}
override fun KotlinTypeMarker.hasExactAnnotation(): Boolean {
@@ -387,7 +387,7 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
override fun KotlinTypeMarker.removeExactAnnotation(): KotlinTypeMarker {
require(this is ConeKotlinType)
return withAttributes(attributes.remove(CompilerConeAttributes.Exact), this@ConeInferenceContext)
return withAttributes(attributes.remove(CompilerConeAttributes.Exact))
}
override fun TypeConstructorMarker.toErrorType(): SimpleTypeMarker {
@@ -415,10 +415,7 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
@Suppress("UNCHECKED_CAST")
val newCustomAttributes = (newAttributes as List<ConeAttribute<*>>).filter { it.isCustomAttribute() }
val attributesToKeep = this.attributes.filterNot { it.isCustomAttribute() }
return withAttributes(
ConeAttributes.create(newCustomAttributes + attributesToKeep),
this@ConeInferenceContext
)
return withAttributes(ConeAttributes.create(newCustomAttributes + attributesToKeep))
}
override fun TypeConstructorMarker.getApproximatedIntegerLiteralType(): KotlinTypeMarker {
@@ -89,7 +89,7 @@ fun ConeKotlinType.makeConeTypeDefinitelyNotNullOrNotNull(typeContext: ConeTypeC
return ConeDefinitelyNotNullType.create(this, typeContext) ?: this.withNullability(ConeNullability.NOT_NULL, typeContext)
}
fun <T : ConeKotlinType> T.withArguments(arguments: Array<out ConeTypeProjection>, typeSystemContext: ConeTypeContext): T {
fun <T : ConeKotlinType> T.withArguments(arguments: Array<out ConeTypeProjection>): T {
if (this.typeArguments === arguments) {
return this
}
@@ -98,15 +98,15 @@ fun <T : ConeKotlinType> T.withArguments(arguments: Array<out ConeTypeProjection
return when (this) {
is ConeClassErrorType -> this
is ConeClassLikeTypeImpl -> ConeClassLikeTypeImpl(lookupTag, arguments, nullability.isNullable) as T
is ConeDefinitelyNotNullType -> ConeDefinitelyNotNullType(original.withArguments(arguments, typeSystemContext)) as T
is ConeDefinitelyNotNullType -> ConeDefinitelyNotNullType(original.withArguments(arguments)) as T
else -> error("Not supported: $this: ${this.render()}")
}
}
fun <T : ConeKotlinType> T.withArguments(replacement: (ConeTypeProjection) -> ConeTypeProjection, typeSystemContext: ConeTypeContext) =
withArguments(typeArguments.map(replacement).toTypedArray(), typeSystemContext)
fun <T : ConeKotlinType> T.withArguments(replacement: (ConeTypeProjection) -> ConeTypeProjection) =
withArguments(typeArguments.map(replacement).toTypedArray())
fun <T : ConeKotlinType> T.withAttributes(attributes: ConeAttributes, typeSystemContext: ConeTypeContext): T {
fun <T : ConeKotlinType> T.withAttributes(attributes: ConeAttributes): T {
if (this.attributes == attributes) {
return this
}
@@ -115,16 +115,10 @@ fun <T : ConeKotlinType> T.withAttributes(attributes: ConeAttributes, typeSystem
return when (this) {
is ConeClassErrorType -> this
is ConeClassLikeTypeImpl -> ConeClassLikeTypeImpl(lookupTag, typeArguments, nullability.isNullable, attributes)
is ConeDefinitelyNotNullType -> ConeDefinitelyNotNullType(original.withAttributes(attributes, typeSystemContext))
is ConeDefinitelyNotNullType -> ConeDefinitelyNotNullType(original.withAttributes(attributes))
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)
)
is ConeRawType -> ConeRawType(lowerBound.withAttributes(attributes), upperBound.withAttributes(attributes))
is ConeFlexibleType -> ConeFlexibleType(lowerBound.withAttributes(attributes), upperBound.withAttributes(attributes))
is ConeTypeVariableType -> ConeTypeVariableType(nullability, lookupTag, attributes)
is ConeCapturedType -> ConeCapturedType(
captureStatus, lowerType, nullability, constructor, attributes, isProjectionNotNull,
@@ -252,7 +246,7 @@ fun FirTypeRef.isUnsafeVarianceType(session: FirSession): Boolean {
fun FirTypeRef.hasEnhancedNullability(): Boolean =
coneTypeSafe<ConeKotlinType>()?.hasEnhancedNullability == true
fun FirTypeRef.withoutEnhancedNullability(typeSystemContext: ConeTypeContext): FirTypeRef {
fun FirTypeRef.withoutEnhancedNullability(): FirTypeRef {
require(this is FirResolvedTypeRef)
if (!hasEnhancedNullability()) return this
return buildResolvedTypeRef {
@@ -261,7 +255,6 @@ fun FirTypeRef.withoutEnhancedNullability(typeSystemContext: ConeTypeContext): F
ConeAttributes.create(
this@withoutEnhancedNullability.type.attributes.filter { it != CompilerConeAttributes.EnhancedNullability }
),
typeSystemContext,
)
annotations += this@withoutEnhancedNullability.annotations
}
@@ -323,14 +316,13 @@ fun FirTypeRef.approximated(
fun FirTypeRef.approximatedIfNeededOrSelf(
approximator: ConeTypeApproximator,
containingCallableVisibility: Visibility?,
typeSystemContext: ConeTypeContext,
isInlineFunction: Boolean = false,
): FirTypeRef {
val approximated = if (containingCallableVisibility == Visibilities.Public || containingCallableVisibility == Visibilities.Protected)
approximatedForPublicPosition(approximator)
else
this
return approximated.hideLocalTypeIfNeeded(containingCallableVisibility, isInlineFunction).withoutEnhancedNullability(typeSystemContext)
return approximated.hideLocalTypeIfNeeded(containingCallableVisibility, isInlineFunction).withoutEnhancedNullability()
}
fun FirTypeRef.approximatedForPublicPosition(approximator: ConeTypeApproximator): FirTypeRef =
@@ -403,15 +395,15 @@ fun FirDeclaration.visibilityForApproximation(container: FirDeclaration?): Visib
}
fun ConeTypeContext.captureFromArgumentsInternal(type: ConeKotlinType, status: CaptureStatus): ConeKotlinType? {
internal fun ConeTypeContext.captureFromArgumentsInternal(type: ConeKotlinType, status: CaptureStatus): ConeKotlinType? {
val capturedArguments = captureArguments(type, status) ?: return null
return if (type is ConeFlexibleType) {
ConeFlexibleType(
type.lowerBound.withArguments(capturedArguments, this),
type.upperBound.withArguments(capturedArguments, this),
type.lowerBound.withArguments(capturedArguments),
type.upperBound.withArguments(capturedArguments),
)
} else {
type.withArguments(capturedArguments, this)
type.withArguments(capturedArguments)
}
}
@@ -466,7 +458,7 @@ fun ConeTypeContext.captureArguments(type: ConeKotlinType, status: CaptureStatus
return newArguments
}
fun ConeTypeContext.captureFromExpressionInternal(type: ConeKotlinType): ConeKotlinType? {
internal fun ConeTypeContext.captureFromExpressionInternal(type: ConeKotlinType): ConeKotlinType? {
if (type !is ConeIntersectionType && type !is ConeFlexibleType) {
return captureFromArgumentsInternal(type, CaptureStatus.FROM_EXPRESSION)
}
@@ -491,12 +483,12 @@ fun ConeTypeContext.captureFromExpressionInternal(type: ConeKotlinType): ConeKot
typeToReplace.intersectedTypes.map { componentType ->
val capturedArguments = findCorrespondingCapturedArgumentsForType(componentType)
?: return@map componentType
componentType.withArguments(capturedArguments, this)
componentType.withArguments(capturedArguments)
}
} else {
val capturedArguments = findCorrespondingCapturedArgumentsForType(typeToReplace)
?: return listOf(typeToReplace)
listOf(typeToReplace.withArguments(capturedArguments, this))
listOf(typeToReplace.withArguments(capturedArguments))
}
}
@@ -441,7 +441,7 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() {
private fun createFunctionalType(typeRef: FirFunctionTypeRef): ConeClassLikeType {
val parameters =
listOfNotNull(typeRef.receiverTypeRef?.coneType) +
typeRef.valueParameters.map { it.returnTypeRef.coneType.withParameterNameAnnotation(it, session.typeContext) } +
typeRef.valueParameters.map { it.returnTypeRef.coneType.withParameterNameAnnotation(it) } +
listOf(typeRef.returnTypeRef.coneType)
val classId = if (typeRef.isSuspend) {
StandardClassIds.SuspendFunctionN(typeRef.parametersCount)
@@ -344,7 +344,7 @@ class FirCallCompletionResultsWriterTransformer(
private fun ConeKotlinType.removeExactAttribute(): ConeKotlinType {
if (attributes.contains(CompilerConeAttributes.Exact)) {
return withAttributes(attributes.remove(CompilerConeAttributes.Exact), session.typeContext)
return withAttributes(attributes.remove(CompilerConeAttributes.Exact))
}
return this
@@ -910,7 +910,7 @@ internal class FirDeclarationCompletionResultsWriter(
typeRef.resolvedTypeFromPrototype(it)
} ?: typeRef
if (data is ApproximationData.ApproximateByStatus) {
return result.approximatedIfNeededOrSelf(typeApproximator, data.visibility, typeContext, data.isInline)
return result.approximatedIfNeededOrSelf(typeApproximator, data.visibility, data.isInline)
}
return result
}
@@ -319,7 +319,6 @@ class FirSyntheticCallGenerator(
val returnType = rightArgumentType.resolvedTypeFromPrototype(
rightArgumentType.type.withAttributes(
ConeAttributes.create(listOf(CompilerConeAttributes.Exact)),
session.typeContext,
)
)
@@ -627,7 +627,6 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
returnExpression.resultType.approximatedIfNeededOrSelf(
session.typeApproximator,
simpleFunction?.visibilityForApproximation(),
transformer.session.typeContext,
simpleFunction?.isInline == true
)
)
@@ -989,7 +988,6 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
expectedType.approximatedIfNeededOrSelf(
session.typeApproximator,
backingField.visibilityForApproximation(),
session.typeContext,
)
)
)
@@ -1014,7 +1012,6 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
expectedType.approximatedIfNeededOrSelf(
session.typeApproximator,
variable.visibilityForApproximation(),
session.typeContext,
)
)
)