FIR DFA: align return-implies checker closer to smartcasts

This commit is contained in:
pyos
2022-11-08 20:21:41 +01:00
committed by teamcity
parent 512deeb07b
commit 6ee6d019ee
5 changed files with 12 additions and 10 deletions
@@ -195,8 +195,10 @@ object FirReturnsImpliesAnalyzer : FirControlFlowChecker() {
}
private fun RealVariable.nullabilityStatement(builtinTypes: BuiltinTypes, isNull: Boolean) =
// TODO: opposite for builtinTypes.nullableNothingType.type
if (isNull) this typeNotEq builtinTypes.anyType.type else this typeEq builtinTypes.anyType.type
if (isNull)
this typeEq builtinTypes.nullableNothingType.type andTypeNotEq builtinTypes.anyType.type
else
this typeEq builtinTypes.anyType.type andTypeNotEq builtinTypes.nullableNothingType.type
private fun TypeStatement.singleton(): TypeStatements =
mapOf(variable to this)
@@ -31,9 +31,9 @@ fun testNullWhenNull(x: Int?) {
// NB. it is the same function as `nullWhenNull`, but annotations specifies other facet of the function behaviour
fun notNullWhenNotNull (x: Int?): Int? {
contract {
<!WRONG_IMPLIES_CONDITION!>contract {
returns(null) implies (x == null)
}
}<!>
return x?.inc()
}
@@ -20,7 +20,7 @@ inline fun case_1(value_1: Int?, block: () -> Unit): Boolean {
// TESTCASE NUMBER: 2
inline fun <T> T?.case_2(value_1: Int?, value_2: Any?, block: () -> Unit): Boolean? {
<!WRONG_IMPLIES_CONDITION!>contract {
<!WRONG_IMPLIES_CONDITION, WRONG_IMPLIES_CONDITION!>contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
returns(true) implies (value_1 == null && this@case_2 == null && value_2 !is Boolean?)
returns(false) implies (value_2 is Boolean?)
@@ -8,7 +8,7 @@ import kotlin.contracts.*
// TESTCASE NUMBER: 3
fun <T> T?.case_3(value_1: Int?, value_2: Boolean): Boolean {
<!WRONG_IMPLIES_CONDITION!>contract {
<!WRONG_IMPLIES_CONDITION, WRONG_IMPLIES_CONDITION!>contract {
returns(true) implies (value_1 != null)
returns(false) implies (value_1 == null && !value_2)
returns(null) implies (value_1 == null && value_2)
@@ -30,7 +30,7 @@ fun case_4(value_1: Number, block: (() -> Unit)?): Boolean? {
// TESTCASE NUMBER: 5
fun String?.case_5(value_1: Number?): Boolean? {
<!WRONG_IMPLIES_CONDITION!>contract {
<!WRONG_IMPLIES_CONDITION, WRONG_IMPLIES_CONDITION!>contract {
returns(true) implies (value_1 == null)
returns(false) implies (this@case_5 == null)
returnsNotNull() implies (value_1 is Int)
@@ -41,7 +41,7 @@ fun String?.case_5(value_1: Number?): Boolean? {
// TESTCASE NUMBER: 6
fun <T> T?.case_6(value_1: Number, value_2: String?): Boolean? {
<!WRONG_IMPLIES_CONDITION!>contract {
<!WRONG_IMPLIES_CONDITION, WRONG_IMPLIES_CONDITION, WRONG_IMPLIES_CONDITION!>contract {
returns(true) implies (this@case_6 == null)
returns(false) implies (value_1 is Int)
returns(null) implies (this@case_6 is String)
@@ -19,11 +19,11 @@ fun <T> T?.case_3(value_1: Int?, value_2: Boolean): Boolean {
// TESTCASE NUMBER: 4
fun case_4(value_1: Number, block: (() -> Unit)?): Boolean? {
contract {
<!WRONG_IMPLIES_CONDITION!>contract {
returns(true) implies (value_1 is Int)
returns(false) implies (block == null)
returns(null) implies (block != null)
}
}<!>
return <!SENSELESS_COMPARISON!>value_1 == null<!>
}