DFA: only stable values or qualified with stable receiver can be bound in x = y #KT-15792 Fixed

This commit is contained in:
Mikhail Glukhikh
2017-01-18 16:11:59 +03:00
committed by mglukhikh
parent 905e67e713
commit b7fb45f36e
6 changed files with 78 additions and 1 deletions
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.psi.KtPsiUtil
import org.jetbrains.kotlin.psi.KtVariableDeclaration
import org.jetbrains.kotlin.resolve.calls.context.ContextDependency
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
@@ -98,7 +99,7 @@ class LocalVariableResolver(
val type = typeInfo.type
if (type != null) {
val initializerDataFlowValue = DataFlowValueFactory.createDataFlowValue(initializer, type, context)
if (!propertyDescriptor.isVar) {
if (!propertyDescriptor.isVar && initializerDataFlowValue.canBeBound) {
context.trace.record(BindingContext.BOUND_INITIALIZER_VALUE, propertyDescriptor, initializerDataFlowValue)
}
// At this moment we do not take initializer value into account if type is given for a property
@@ -69,6 +69,8 @@ class DataFlowValue(val identifierInfo: IdentifierInfo,
*/
val isStable = (kind == Kind.STABLE_VALUE || kind == Kind.STABLE_VARIABLE || kind == Kind.STABLE_COMPLEX_EXPRESSION)
val canBeBound get() = identifierInfo.canBeBound
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is DataFlowValue) return false
@@ -27,6 +27,8 @@ interface IdentifierInfo {
val kind: DataFlowValue.Kind get() = OTHER
val canBeBound get() = false
object NO : IdentifierInfo {
override fun toString() = "NO_IDENTIFIER_INFO"
}
@@ -45,6 +47,9 @@ interface IdentifierInfo {
val bound: DataFlowValue?
) : IdentifierInfo {
override val canBeBound
get() = kind == STABLE_VALUE
override fun equals(other: Any?) = other is Variable && variable == other.variable
override fun hashCode() = variable.hashCode()
@@ -74,6 +79,9 @@ interface IdentifierInfo {
) : IdentifierInfo {
override val kind: DataFlowValue.Kind get() = if (receiverInfo.kind == STABLE_VALUE) selectorInfo.kind else OTHER
override val canBeBound
get() = receiverInfo.canBeBound
override fun equals(other: Any?) = other is Qualified && receiverInfo == other.receiverInfo && selectorInfo == other.selectorInfo
override fun hashCode() = 31 * receiverInfo.hashCode() + selectorInfo.hashCode()