Revert "FIR: add smartcast stability to FIR"

This reverts commit 96bd2c54
This commit is contained in:
Dmitriy Novozhilov
2021-06-03 09:54:02 +03:00
parent 0497f60af3
commit 6f54e9fd5c
13 changed files with 11 additions and 87 deletions
@@ -274,11 +274,7 @@ class Fir2IrImplicitCastInserter(
}
override fun visitExpressionWithSmartcast(expressionWithSmartcast: FirExpressionWithSmartcast, data: IrElement): IrExpression {
return if (expressionWithSmartcast.smartcastStability == SmartcastStability.STABLE_VALUE) {
implicitCastOrExpression(data as IrExpression, expressionWithSmartcast.typeRef)
} else {
data as IrExpression
}
return implicitCastOrExpression(data as IrExpression, expressionWithSmartcast.typeRef)
}
override fun visitExpressionWithSmartcastToNull(
@@ -36,7 +36,6 @@ import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.name.*
import org.jetbrains.kotlin.resolve.ForbiddenNamedArgumentsTarget
import org.jetbrains.kotlin.types.SmartcastStability
fun List<FirQualifierPart>.toTypeProjections(): Array<ConeTypeProjection> =
asReversed().flatMap { it.typeArgumentList.typeArguments.map { typeArgument -> typeArgument.toConeTypeProjection() } }.toTypedArray()
@@ -286,9 +285,8 @@ fun BodyResolveComponents.transformQualifiedAccessUsingSmartcastInfo(
}
return buildExpressionWithSmartcast {
originalExpression = qualifiedAccessExpression
smartcastType = intersectedTypeRef
typeRef = intersectedTypeRef
this.typesFromSmartCast = typesFromSmartCast
smartcastStability = SmartcastStability.STABLE_VALUE
}
}
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.coneTypeSafe
import org.jetbrains.kotlin.types.SmartcastStability
interface Receiver
@@ -90,9 +89,8 @@ sealed class ImplicitReceiverValue<S : AbstractFirBasedSymbol<*>>(
} else {
buildExpressionWithSmartcast {
originalExpression = originalReceiverExpression
smartcastType = originalReceiverExpression.typeRef.resolvedTypeFromPrototype(type)
typeRef = originalReceiverExpression.typeRef.resolvedTypeFromPrototype(type)
typesFromSmartCast = listOf(type)
smartcastStability = SmartcastStability.STABLE_VALUE
}
}
implicitScope = type.scope(useSiteSession, scopeSession, FakeOverrideTypeCalculator.DoNothing)
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.fir.references.FirReference
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeProjection
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.types.SmartcastStability
import org.jetbrains.kotlin.fir.visitors.*
import org.jetbrains.kotlin.fir.FirImplementationDetail
@@ -32,8 +31,6 @@ abstract class FirExpressionWithSmartcast : FirQualifiedAccessExpression() {
abstract val originalExpression: FirQualifiedAccessExpression
abstract val typesFromSmartCast: Collection<ConeKotlinType>
abstract val originalType: FirTypeRef
abstract val smartcastType: FirTypeRef
abstract val smartcastStability: SmartcastStability
override fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R = visitor.visitExpressionWithSmartcast(this, data)
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.fir.references.FirReference
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeProjection
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.types.SmartcastStability
import org.jetbrains.kotlin.fir.visitors.*
import org.jetbrains.kotlin.fir.FirImplementationDetail
@@ -32,8 +31,6 @@ abstract class FirExpressionWithSmartcastToNull : FirExpressionWithSmartcast() {
abstract override val originalExpression: FirQualifiedAccessExpression
abstract override val typesFromSmartCast: Collection<ConeKotlinType>
abstract override val originalType: FirTypeRef
abstract override val smartcastType: FirTypeRef
abstract override val smartcastStability: SmartcastStability
override fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R = visitor.visitExpressionWithSmartcastToNull(this, data)
@@ -10,16 +10,14 @@ import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.expressions.impl.FirExpressionWithSmartcastImpl
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.types.SmartcastStability
class FirExpressionWithSmartcastBuilder {
lateinit var originalExpression: FirQualifiedAccessExpression
lateinit var smartcastType: FirTypeRef
lateinit var typeRef: FirTypeRef
lateinit var typesFromSmartCast: Collection<ConeKotlinType>
lateinit var smartcastStability: SmartcastStability
fun build(): FirExpressionWithSmartcast {
return FirExpressionWithSmartcastImpl(originalExpression, smartcastType, typesFromSmartCast, smartcastStability)
return FirExpressionWithSmartcastImpl(originalExpression, typeRef, typesFromSmartCast)
}
}
@@ -10,16 +10,14 @@ import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.expressions.impl.FirExpressionWithSmartcastToNullImpl
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.types.SmartcastStability
class FirExpressionWithSmartcastToNullBuilder {
lateinit var originalExpression: FirQualifiedAccessExpression
lateinit var smartcastType: FirTypeRef
lateinit var typeRef: FirTypeRef
lateinit var typesFromSmartCast: Collection<ConeKotlinType>
lateinit var smartcastStability: SmartcastStability
fun build(): FirExpressionWithSmartcastToNull {
return FirExpressionWithSmartcastToNullImpl(originalExpression, smartcastType, typesFromSmartCast, smartcastStability)
return FirExpressionWithSmartcastToNullImpl(originalExpression, typeRef, typesFromSmartCast)
}
}
@@ -19,13 +19,11 @@ import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.FirVisitor
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.types.SmartcastStability
internal class FirExpressionWithSmartcastImpl(
override var originalExpression: FirQualifiedAccessExpression,
override val smartcastType: FirTypeRef,
override val typesFromSmartCast: Collection<ConeKotlinType>,
override val smartcastStability: SmartcastStability
override val typeRef: FirTypeRef,
override val typesFromSmartCast: Collection<ConeKotlinType>
) : FirExpressionWithSmartcast() {
init {
assert(originalExpression.typeRef is FirResolvedTypeRef)
@@ -39,10 +37,6 @@ internal class FirExpressionWithSmartcastImpl(
override val extensionReceiver: FirExpression get() = originalExpression.extensionReceiver
override val calleeReference: FirReference get() = originalExpression.calleeReference
override val originalType: FirTypeRef get() = originalExpression.typeRef
// A FirExpressionWithSmartcast is only an effective smartcast if `smartcastStability == SmartcastStability.STABLE_VALUE`. Otherwise,
// it's the same as the `originalExpression` under the hood. The reason we still create such a smartcast expression is for diagnostics
// purpose only.
override val typeRef: FirTypeRef get() = if (smartcastStability == SmartcastStability.STABLE_VALUE) smartcastType else originalType
override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirExpressionWithSmartcast {
originalExpression = originalExpression.transformSingle(transformer, data)
@@ -16,13 +16,11 @@ import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.FirVisitor
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.types.SmartcastStability
class FirExpressionWithSmartcastToNullImpl(
override var originalExpression: FirQualifiedAccessExpression,
override val smartcastType: FirTypeRef,
override val typesFromSmartCast: Collection<ConeKotlinType>,
override val smartcastStability: SmartcastStability
override val typeRef: FirTypeRef,
override val typesFromSmartCast: Collection<ConeKotlinType>
) : FirExpressionWithSmartcastToNull() {
init {
assert(originalExpression.typeRef is FirResolvedTypeRef)
@@ -36,10 +34,6 @@ class FirExpressionWithSmartcastToNullImpl(
override val extensionReceiver: FirExpression get() = originalExpression.extensionReceiver
override val calleeReference: FirReference get() = originalExpression.calleeReference
override val originalType: FirTypeRef get() = originalExpression.typeRef
// A FirExpressionWithSmartcast is only an effective smartcast if `smartcastStability == SmartcastStability.STABLE_VALUE`. Otherwise,
// it's the same as the `originalExpression` under the hood. The reason we still create such a smartcast expression is for diagnostics
// purpose only.
override val typeRef: FirTypeRef get() = if (smartcastStability == SmartcastStability.STABLE_VALUE) smartcastType else originalType
override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirExpressionWithSmartcastToNull {
originalExpression = originalExpression.transformSingle(transformer, data)
@@ -83,6 +83,4 @@ object FieldSets {
val modality = field(modalityType, nullable = true)
val scopeProvider = field("scopeProvider", firScopeProviderType)
val smartcastStability = field(smartcastStabilityType)
}
@@ -19,7 +19,6 @@ import org.jetbrains.kotlin.fir.tree.generator.FieldSets.name
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.receivers
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.returnTypeRef
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.scopeProvider
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.smartcastStability
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.status
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.superTypeRefs
import org.jetbrains.kotlin.fir.tree.generator.FieldSets.symbol
@@ -459,8 +458,6 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
+field("originalExpression", qualifiedAccessExpression)
+field("typesFromSmartCast", "Collection<ConeKotlinType>", null, customType = coneKotlinTypeType)
+field("originalType", typeRef)
+field("smartcastType", typeRef)
+smartcastStability
}
expressionWithSmartcastToNull.configure {
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.SmartcastStability
import org.jetbrains.kotlin.types.Variance
val sourceElementType = type("fir", "FirSourceElement")
@@ -32,7 +31,6 @@ val nameType = type(Name::class)
val visibilityType = type(Visibility::class)
val effectiveVisibilityType = type("descriptors", "EffectiveVisibility")
val modalityType = type(Modality::class)
val smartcastStabilityType = type(SmartcastStability::class)
val fqNameType = type(FqName::class)
val classIdType = type(ClassId::class)
val annotationUseSiteTargetType = type(AnnotationUseSiteTarget::class)
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.types
/**
* See https://kotlinlang.org/spec/type-inference.html#smart-cast-sink-stability for explanation on smartcast stability. The "mutable value
* capturing" and "concurrent writes" categories in the spec are covered by [CAPTURED_VARIABLE] and [MUTABLE_PROPERTY] together.
* Specifically, we only do capture analysis on local mutable properties. Non-local ones are considered to be always unstable (assuming
* some concurrent writes are always present).
*/
enum class SmartcastStability(private val str: String, val description: String = str) {
// Local value, or parameter, or private / internal member value without open / custom getter,
// or protected / public member value from the same module without open / custom getter
// Smart casts are completely safe
STABLE_VALUE("stable val"),
// Member value with open / custom getter
// Smart casts are not safe
PROPERTY_WITH_GETTER("custom getter", "property that has open or custom getter"),
// Protected / public member value from another module
// Smart casts are not safe
ALIEN_PUBLIC_PROPERTY("alien public", "public API property declared in different module"),
// Local variable already captured by a changing closure
// Smart casts are not safe
CAPTURED_VARIABLE("captured var", "local variable that is captured by a changing closure"),
// Member variable regardless of its visibility
// Smart casts are not safe
MUTABLE_PROPERTY("member", "mutable property that could have been changed by this time"),
// A delegated property.
// Smart casts are not safe
DELEGATED_PROPERTY("delegate", "delegated property that could have been changed by this time"),
}