FIR checker: SENSELESS_(COMPARISON|NULL_IN_WHEN)

Currently DFA does not set "definitely equal to null" for access to variables that got assigned `null`. For example, FIR should mark the following line as SENSELESS_COMPARISON due to `s = null` above.
 https://github.com/JetBrains/kotlin/blob/d1531f9cdd5852352c0133198706125dc63b6007/compiler/testData/diagnostics/tests/smartCasts/alwaysNull.fir.kt#L6

The problem is at https://github.com/JetBrains/kotlin/blob/7e9f27436a77de1c76e3705da7aa1fbe8938336b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt#L1104

For null assignment, ideally the type should be `Nothing?`. This is
addressed in a followup commit instead.
This commit is contained in:
Tianyu Geng
2021-07-20 09:46:18 -07:00
committed by teamcityserver
parent 4726dcce40
commit c7272f6986
108 changed files with 1410 additions and 1485 deletions
@@ -18,21 +18,21 @@ import otherpackage.*
// TESTCASE NUMBER: 1
fun case_1(x: Any) {
if (x === null) {
if (<!SENSELESS_COMPARISON!>x === null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any & kotlin.Nothing")!>x<!>
}
}
// TESTCASE NUMBER: 2
fun case_2(x: Nothing) {
if (x == null) {
if (<!SENSELESS_COMPARISON!>x == null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Nothing")!>x<!>
}
}
// TESTCASE NUMBER: 3
fun case_3() {
if (Object.prop_2 != null)
if (<!SENSELESS_COMPARISON!>Object.prop_2 != null<!>)
else {
Object.prop_2
}
@@ -40,7 +40,7 @@ fun case_3() {
// TESTCASE NUMBER: 4
fun case_4(x: Char) {
if (x == null && true) {
if (<!SENSELESS_COMPARISON!>x == null<!> && true) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Char & kotlin.Nothing")!>x<!>
}
}
@@ -49,33 +49,33 @@ fun case_4(x: Char) {
fun case_5() {
val x: Unit = kotlin.Unit
if (x == null) <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Unit & kotlin.Nothing")!>x<!>
if (<!SENSELESS_COMPARISON!>x == null<!>) <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Unit & kotlin.Nothing")!>x<!>
}
// TESTCASE NUMBER: 6
fun case_6(x: EmptyClass) {
val y = true
if (x == null && !y) {
if (<!SENSELESS_COMPARISON!>x == null<!> && !y) {
<!DEBUG_INFO_EXPRESSION_TYPE("EmptyClass & kotlin.Nothing")!>x<!>
}
}
// TESTCASE NUMBER: 7
fun case_7() {
if (anonymousTypeProperty == null || <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>anonymousTypeProperty<!> == null) {
if (<!SENSELESS_COMPARISON!>anonymousTypeProperty == null<!> || <!SENSELESS_COMPARISON!><!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>anonymousTypeProperty<!> == null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>anonymousTypeProperty<!>
}
}
// TESTCASE NUMBER: 8
fun case_8(x: TypealiasString) {
if (x == null && <!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString & kotlin.Nothing")!>x<!> == null) <!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString")!>x<!>
if (<!SENSELESS_COMPARISON!>x == null<!> && <!SENSELESS_COMPARISON!><!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString & kotlin.Nothing")!>x<!> == null<!>) <!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString")!>x<!>
}
// TESTCASE NUMBER: 9
fun case_9(x: TypealiasString) {
if (x != null) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {
} else if (false) {
<!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString & kotlin.Nothing")!>x<!>
@@ -86,8 +86,8 @@ fun case_9(x: TypealiasString) {
fun case_10() {
val a = Class()
if (a.prop_5 != null || true) {
if (a.prop_5 == null) {
if (<!SENSELESS_COMPARISON!>a.prop_5 != null<!> || true) {
if (<!SENSELESS_COMPARISON!>a.prop_5 == null<!>) {
a.prop_5
}
}
@@ -97,12 +97,12 @@ fun case_10() {
fun case_11(x: TypealiasString, y: TypealiasString) {
val z: TypealiasString = TypealiasString()
if (x != null) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {
} else {
if (y == null) {
if (stringProperty != null) {
if (false || false || false || z == null || false) {
if (<!SENSELESS_COMPARISON!>y == null<!>) {
if (<!SENSELESS_COMPARISON!>stringProperty != null<!>) {
if (false || false || false || <!SENSELESS_COMPARISON!>z == null<!> || false) {
<!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString & kotlin.Nothing")!>x<!>
}
}
@@ -111,13 +111,13 @@ fun case_11(x: TypealiasString, y: TypealiasString) {
}
// TESTCASE NUMBER: 12
fun case_12(x: TypealiasString, y: TypealiasString) = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>if (x != null) "1"
else if (y !== null) <!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString & kotlin.Nothing")!>x<!>
fun case_12(x: TypealiasString, y: TypealiasString) = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>if (<!SENSELESS_COMPARISON!>x != null<!>) "1"
else if (<!SENSELESS_COMPARISON!>y !== null<!>) <!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString & kotlin.Nothing")!>x<!>
else "-1"<!>
// TESTCASE NUMBER: 13
fun case_13(x: otherpackage.EmptyClass13) =
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>if (x != null) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>if (<!SENSELESS_COMPARISON!>x != null<!>) {
1
} else <!DEBUG_INFO_EXPRESSION_TYPE("otherpackage.EmptyClass13 & kotlin.Nothing")!>x<!><!>
@@ -132,22 +132,22 @@ class A14 {
fun case_14() {
val a = A14()
if (a.x == null) {
if (a.x == null) {
if (a.x == null) {
if (a.x === null) {
if (a.x == null) {
if (a.x == null) {
if (a.x == null) {
if (a.x == null) {
if (a.x === null) {
if (a.x == null) {
if (a.x == null) {
if (a.x == null) {
if (a.x == null) {
if (a.x == null) {
if (a.x == null) {
if (a.x === null) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x === null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x === null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x == null<!>) {
if (<!SENSELESS_COMPARISON!>a.x === null<!>) {
a.x
}
}
@@ -169,7 +169,7 @@ fun case_14() {
// TESTCASE NUMBER: 15
fun case_15(x: TypealiasString) {
val t = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>if (true && x != null) "" else {
val t = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>if (true && <!SENSELESS_COMPARISON!>x != null<!>) "" else {
<!DEBUG_INFO_EXPRESSION_TYPE("TypealiasString")!>x<!>
}<!>
}
@@ -178,7 +178,7 @@ fun case_15(x: TypealiasString) {
fun case_16() {
val x: TypealiasNothing = return
if (x == null) {
if (<!SENSELESS_COMPARISON!>x == null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("TypealiasNothing")!>x<!>
}
}
@@ -188,13 +188,13 @@ fun case_16() {
* UNEXPECTED BEHAVIOUR
* ISSUES: KT-28329
*/
val case_17 = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>if (true && true && intProperty != null) 0 else {
val case_17 = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>if (true && true && <!SENSELESS_COMPARISON!>intProperty != null<!>) 0 else {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>intProperty<!>
}<!>
//TESTCASE NUMBER: 18
fun case_18(a: DeepObject.A.B.C.D.E.F.G.J) {
if (a == null) {
if (<!SENSELESS_COMPARISON!>a == null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("DeepObject.A.B.C.D.E.F.G.J & kotlin.Nothing")!>a<!>
}
}
@@ -219,7 +219,7 @@ fun case_19(b: Boolean) {
}
} else null
if (a != null && a.B19 != null && a.B19.C19 != null && a.B19.C19.D19 != null && a.B19.C19.D19.x == null) {
if (a != null && a.B19 != null && a.B19.C19 != null && a.B19.C19.D19 != null && <!SENSELESS_COMPARISON!>a.B19.C19.D19.x == null<!>) {
a.B19.C19.D19.x
}
}
@@ -234,30 +234,30 @@ fun case_20() {
}
}
if (a.B19.C19.D19 === null) {
if (<!SENSELESS_COMPARISON!>a.B19.C19.D19 === null<!>) {
a.B19.C19.D19
}
}
// TESTCASE NUMBER: 21
fun case_21() {
if (EnumClassWithProperty.B.prop_1 == null || false || false || false || false || false || false || false) {
if (<!SENSELESS_COMPARISON!>EnumClassWithProperty.B.prop_1 == null<!> || false || false || false || false || false || false || false) {
EnumClassWithProperty.B.prop_1
}
}
// TESTCASE NUMBER: 22
fun case_22(a: (() -> Unit)) {
if (a == null) {
if (<!SENSELESS_COMPARISON!>a == null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Unit")!>a()<!>
}
}
// TESTCASE NUMBER: 23
fun case_23(a: ((Float) -> Int), b: Float) {
if (a == null && b == null) {
if (<!SENSELESS_COMPARISON!>a == null<!> && <!SENSELESS_COMPARISON!>b == null<!>) {
val x = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>a(<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Float & kotlin.Nothing")!>b<!>)<!>
if (x !== null) {
if (<!SENSELESS_COMPARISON!>x !== null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>x<!>
}
}
@@ -269,20 +269,20 @@ fun case_23(a: ((Float) -> Int), b: Float) {
* ISSUES: KT-28329
*/
fun case_24(a: ((() -> Unit) -> Unit), b: (() -> Unit)) {
if (false || false || a == null && b === null) {
if (false || false || <!SENSELESS_COMPARISON!>a == null<!> && <!SENSELESS_COMPARISON!>b === null<!>) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Function1<kotlin.Function0<kotlin.Unit>, kotlin.Unit>")!>a<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Function0<kotlin.Unit>")!>b<!>
}
}
// TESTCASE NUMBER: 25
fun case_25(a: (() -> Unit) -> Unit, b: (() -> Unit) -> Unit = if (a == null) <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Function1<kotlin.Function0<kotlin.Unit>, kotlin.Unit> & kotlin.Nothing")!>a<!> else {{}}) {
fun case_25(a: (() -> Unit) -> Unit, b: (() -> Unit) -> Unit = if (<!SENSELESS_COMPARISON!>a == null<!>) <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Function1<kotlin.Function0<kotlin.Unit>, kotlin.Unit> & kotlin.Nothing")!>a<!> else {{}}) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Function1<kotlin.Function0<kotlin.Unit>, kotlin.Unit>")!>a<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Function1<kotlin.Function0<kotlin.Unit>, kotlin.Unit>")!>b<!>
}
// TESTCASE NUMBER: 26
fun case_26(a: Int, b: Int = if (a === null) <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int & kotlin.Nothing")!>a<!> else 0) {
fun case_26(a: Int, b: Int = if (<!SENSELESS_COMPARISON!>a === null<!>) <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int & kotlin.Nothing")!>a<!> else 0) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>a<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>b<!>
}