Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78d2ef5

Browse files
committed
Kotlin: Towards better equality checking
1 parent 5da15ca commit 78d2ef5

4 files changed

Lines changed: 219 additions & 188 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,22 +1544,22 @@ open class KotlinFileExtractor(
15441544
binopDisp(id)
15451545
}
15461546
// != gets desugared into not and ==. Here we resugar it.
1547+
// TODO: This is wrong. Kotlin `a == b` is `a?.equals(b) ?: (b === null)`
15471548
c.origin == EXCLEQ && isFunction("kotlin", "Boolean", "not") && c.valueArgumentsCount == 0 && dr != null && dr is IrCall && isBuiltinCall(dr, "EQEQ") -> {
15481549
val id = tw.getFreshIdLabel<DbNeexpr>()
15491550
val type = useType(c.type)
15501551
tw.writeExprs_neexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
15511552
binop(id, dr, callable)
15521553
}
1553-
// compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrBuiltIns.kt
1554-
isBuiltinCall(c, "EQEQ") -> {
1555-
if(c.origin != EQEQ) {
1556-
logger.warnElement(Severity.ErrorSevere, "Unexpected origin for EQEQ: ${c.origin}", c)
1557-
}
1558-
val id = tw.getFreshIdLabel<DbEqexpr>()
1554+
c.origin == EXCLEQEQ && isFunction("kotlin", "Boolean", "not") && c.valueArgumentsCount == 0 && dr != null && dr is IrCall && isBuiltinCall(dr, "EQEQEQ") -> {
1555+
val id = tw.getFreshIdLabel<DbNeexpr>()
15591556
val type = useType(c.type)
1560-
tw.writeExprs_eqexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1561-
binop(id, c, callable)
1557+
tw.writeExprs_neexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1558+
binop(id, dr, callable)
15621559
}
1560+
// We need to handle all the builtin operators defines in BuiltInOperatorNames in
1561+
// compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrBuiltIns.kt
1562+
// as they can't be extracted as external dependencies.
15631563
isBuiltinCall(c, "less") -> {
15641564
if(c.origin != LT) {
15651565
logger.warnElement(Severity.ErrorSevere, "Unexpected origin for LT: ${c.origin}", c)
@@ -1596,6 +1596,25 @@ open class KotlinFileExtractor(
15961596
tw.writeExprs_geexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
15971597
binop(id, c, callable)
15981598
}
1599+
isBuiltinCall(c, "EQEQ") -> {
1600+
if(c.origin != EQEQ) {
1601+
logger.warnElement(Severity.ErrorSevere, "Unexpected origin for EQEQ: ${c.origin}", c)
1602+
}
1603+
// TODO: This is wrong. Kotlin `a == b` is `a?.equals(b) ?: (b === null)`
1604+
val id = tw.getFreshIdLabel<DbEqexpr>()
1605+
val type = useType(c.type)
1606+
tw.writeExprs_eqexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1607+
binop(id, c, callable)
1608+
}
1609+
isBuiltinCall(c, "EQEQEQ") -> {
1610+
if(c.origin != EQEQEQ) {
1611+
logger.warnElement(Severity.ErrorSevere, "Unexpected origin for EQEQEQ: ${c.origin}", c)
1612+
}
1613+
val id = tw.getFreshIdLabel<DbEqexpr>()
1614+
val type = useType(c.type)
1615+
tw.writeExprs_eqexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1616+
binop(id, c, callable)
1617+
}
15991618
else -> {
16001619
val id = tw.getFreshIdLabel<DbMethodaccess>()
16011620
val type = useType(c.type)

java/ql/test/kotlin/library-tests/exprs/binop.expected

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
| exprs.kt:23:15:23:20 | ... <= ... | exprs.kt:23:15:23:15 | x | exprs.kt:23:20:23:20 | y |
99
| exprs.kt:24:15:24:19 | ... > ... | exprs.kt:24:15:24:15 | x | exprs.kt:24:19:24:19 | y |
1010
| exprs.kt:25:15:25:20 | ... >= ... | exprs.kt:25:15:25:15 | x | exprs.kt:25:20:25:20 | y |
11-
| exprs.kt:50:16:50:26 | ... + ... | exprs.kt:50:16:50:19 | str1 | exprs.kt:50:23:50:26 | str2 |
12-
| exprs.kt:53:12:53:23 | ... > ... | exprs.kt:53:12:53:19 | variable | exprs.kt:53:23:53:23 | 0 |
13-
| exprs.kt:57:12:57:20 | ... + ... | exprs.kt:57:12:57:14 | 123 | exprs.kt:57:18:57:20 | 456 |
14-
| exprs.kt:83:8:83:16 | ... != ... | exprs.kt:83:8:83:8 | r | exprs.kt:83:13:83:16 | null |
11+
| exprs.kt:26:15:26:21 | ... == ... | exprs.kt:26:15:26:15 | x | exprs.kt:26:21:26:21 | y |
12+
| exprs.kt:27:15:27:21 | ... != ... | exprs.kt:27:15:27:15 | x | exprs.kt:27:21:27:21 | y |
13+
| exprs.kt:52:16:52:26 | ... + ... | exprs.kt:52:16:52:19 | str1 | exprs.kt:52:23:52:26 | str2 |
14+
| exprs.kt:55:12:55:23 | ... > ... | exprs.kt:55:12:55:19 | variable | exprs.kt:55:23:55:23 | 0 |
15+
| exprs.kt:59:12:59:20 | ... + ... | exprs.kt:59:12:59:14 | 123 | exprs.kt:59:18:59:20 | 456 |
16+
| exprs.kt:85:8:85:16 | ... != ... | exprs.kt:85:8:85:8 | r | exprs.kt:85:13:85:16 | null |

0 commit comments

Comments
 (0)