JVM: correctly merge typed null values

1. merge(null of type A, null of type B) = null of unknown type;
2. merge(null of type A, something of type B) = merge(unknown null, B).

^KT-52311 Fixed
This commit is contained in:
pyos
2022-05-10 15:29:51 +02:00
committed by teamcity
parent dcdd1cd14e
commit 513ef575ce
11 changed files with 160 additions and 2 deletions
@@ -161,7 +161,7 @@ private fun Type.isIntLike(): Boolean = when (sort) {
}
// Represents [ACONST_NULL, CHECKCAST Type] sequence result.
internal class TypedNullValue(type: Type) : BasicValue(type)
internal class TypedNullValue(type: Type) : StrictBasicValue(type)
// Preserves nulls through CHECKCASTS.
private class NullCheckcastAwareOptimizationBasicInterpreter : OptimizationBasicInterpreter() {
@@ -171,4 +171,12 @@ private class NullCheckcastAwareOptimizationBasicInterpreter : OptimizationBasic
}
return super.unaryOperation(insn, value)
}
}
override fun merge(v: BasicValue, w: BasicValue): BasicValue =
when {
v is TypedNullValue && w is TypedNullValue -> if (v.type == w.type) v else StrictBasicValue.NULL_VALUE
v is TypedNullValue -> super.merge(StrictBasicValue.NULL_VALUE, w)
w is TypedNullValue -> super.merge(v, StrictBasicValue.NULL_VALUE)
else -> super.merge(v, w)
}
}