Minor. Extract common parts from DelegatingDataFlow::equate/disequate

This commit is contained in:
Denis Zharkov
2018-07-17 15:41:53 +03:00
parent bf1f94c185
commit 851d760ac1
@@ -68,7 +68,7 @@ internal class DelegatingDataFlowInfo private constructor(
// TODO: remove me in version 1.3! I'm very dirty hack! // TODO: remove me in version 1.3! I'm very dirty hack!
// In normal circumstances this should be always true // In normal circumstances this should be always true
recordUnstable: Boolean = true recordUnstable: Boolean = true
): Boolean { ) {
if (value.isStable || recordUnstable) { if (value.isStable || recordUnstable) {
map[value] = nullability map[value] = nullability
} }
@@ -111,8 +111,6 @@ internal class DelegatingDataFlowInfo private constructor(
} }
} }
} }
return nullability != getCollectedNullability(value)
} }
override fun getCollectedTypes(key: DataFlowValue, languageVersionSettings: LanguageVersionSettings) = override fun getCollectedTypes(key: DataFlowValue, languageVersionSettings: LanguageVersionSettings) =
@@ -189,30 +187,47 @@ internal class DelegatingDataFlowInfo private constructor(
override fun equate( override fun equate(
a: DataFlowValue, b: DataFlowValue, identityEquals: Boolean, languageVersionSettings: LanguageVersionSettings a: DataFlowValue, b: DataFlowValue, identityEquals: Boolean, languageVersionSettings: LanguageVersionSettings
): DataFlowInfo = equateOrDisequate(a, b, languageVersionSettings, identityEquals, isEquate = true)
override fun disequate(
a: DataFlowValue, b: DataFlowValue, languageVersionSettings: LanguageVersionSettings
): DataFlowInfo = equateOrDisequate(a, b, languageVersionSettings, identityEquals = false, isEquate = false)
private fun equateOrDisequate(
a: DataFlowValue,
b: DataFlowValue,
languageVersionSettings: LanguageVersionSettings,
identityEquals: Boolean,
isEquate: Boolean
): DataFlowInfo { ): DataFlowInfo {
val resultNullabilityInfo = hashMapOf<DataFlowValue, Nullability>() val resultNullabilityInfo = hashMapOf<DataFlowValue, Nullability>()
val newTypeInfoBuilder = newTypeInfoBuilder()
val nullabilityOfA = getStableNullability(a) val nullabilityOfA = getStableNullability(a)
val nullabilityOfB = getStableNullability(b) val nullabilityOfB = getStableNullability(b)
val newANullability = nullabilityOfA.refine(if (isEquate) nullabilityOfB else nullabilityOfB.invert())
val newBNullability = nullabilityOfB.refine(if (isEquate) nullabilityOfA else nullabilityOfA.invert())
val newTypeInfoBuilder = newTypeInfoBuilder() putNullabilityAndTypeInfo(
var changed = resultNullabilityInfo,
putNullabilityAndTypeInfo( a,
resultNullabilityInfo, newANullability,
a, languageVersionSettings,
nullabilityOfA.refine(nullabilityOfB), newTypeInfoBuilder
languageVersionSettings, )
newTypeInfoBuilder
) or putNullabilityAndTypeInfo(
putNullabilityAndTypeInfo( resultNullabilityInfo,
resultNullabilityInfo, b,
b, newBNullability,
nullabilityOfB.refine(nullabilityOfA), languageVersionSettings,
languageVersionSettings, newTypeInfoBuilder
newTypeInfoBuilder )
)
var changed = getCollectedNullability(a) != newANullability || getCollectedNullability(b) != newBNullability
// NB: == has no guarantees of type equality, see KT-11280 for the example // NB: == has no guarantees of type equality, see KT-11280 for the example
if (identityEquals || !nullabilityOfA.canBeNonNull() || !nullabilityOfB.canBeNonNull()) { if (isEquate && (identityEquals || !nullabilityOfA.canBeNonNull() || !nullabilityOfB.canBeNonNull())) {
newTypeInfoBuilder.putAll(a, getStableTypes(b, false, languageVersionSettings)) newTypeInfoBuilder.putAll(a, getStableTypes(b, false, languageVersionSettings))
newTypeInfoBuilder.putAll(b, getStableTypes(a, false, languageVersionSettings)) newTypeInfoBuilder.putAll(b, getStableTypes(a, false, languageVersionSettings))
if (a.type != b.type) { if (a.type != b.type) {
@@ -230,34 +245,6 @@ internal class DelegatingDataFlowInfo private constructor(
return if (changed) create(this, resultNullabilityInfo, newTypeInfoBuilder) else this return if (changed) create(this, resultNullabilityInfo, newTypeInfoBuilder) else this
} }
override fun disequate(
a: DataFlowValue, b: DataFlowValue, languageVersionSettings: LanguageVersionSettings
): DataFlowInfo {
val resultNullabilityInfo = hashMapOf<DataFlowValue, Nullability>()
val nullabilityOfA = getStableNullability(a)
val nullabilityOfB = getStableNullability(b)
val newTypeInfoBuilder = newTypeInfoBuilder()
val changed =
putNullabilityAndTypeInfo(
resultNullabilityInfo,
a,
nullabilityOfA.refine(nullabilityOfB.invert()),
languageVersionSettings,
newTypeInfoBuilder
) or
putNullabilityAndTypeInfo(
resultNullabilityInfo,
b,
nullabilityOfB.refine(nullabilityOfA.invert()),
languageVersionSettings,
newTypeInfoBuilder
)
return if (changed) create(this, resultNullabilityInfo, newTypeInfoBuilder) else this
}
override fun establishSubtyping( override fun establishSubtyping(
value: DataFlowValue, type: KotlinType, languageVersionSettings: LanguageVersionSettings value: DataFlowValue, type: KotlinType, languageVersionSettings: LanguageVersionSettings
): DataFlowInfo { ): DataFlowInfo {