Skip to content

Commit 5d3f723

Browse files
committed
Kotlin extractor: use special <nulltype> for null literals
This matches the Java extractor's treatment of these literals, and so enables dataflow type-tracking to avoid special-casing Kotlin. Natively, Kotlin would regard this as kotlin.Nothing?, the type that can only contain null (kotlin.Nothing without a ? can take nothing at all), which gets Java-ified as java.lang.Void, and this will continue to be used when a null type has to be "boxed", as in representing substituted generic constraints with no possible type.
1 parent cec0544 commit 5d3f723

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5745,7 +5745,14 @@ open class KotlinFileExtractor(
57455745
) =
57465746
exprIdOrFresh<DbNullliteral>(overrideId).also {
57475747
val type = useType(t)
5748-
tw.writeExprs_nullliteral(it, type.javaResult.id, parent, idx)
5748+
// Match Java by using a special <nulltype> for nulls, rather than Kotlin's view of this which is
5749+
// kotlin.Nothing?, the type that can only contain null.
5750+
val nullTypeName = "<nulltype>"
5751+
val javaNullType = tw.getLabelFor(
5752+
"@\"type;$nullTypeName\"",
5753+
{ tw.writePrimitives(it, nullTypeName) }
5754+
)
5755+
tw.writeExprs_nullliteral(it, javaNullType, parent, idx)
57495756
tw.writeExprsKotlinType(it, type.kotlinResult.id)
57505757
extractExprContext(it, locId, callable, enclosingStmt)
57515758
}

java/ql/test-kotlin1/library-tests/classes/genericExprTypes.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
| generic_anonymous.kt:13:27:13:47 | get(...) | int |
4646
| generic_anonymous.kt:13:40:13:40 | i | int |
4747
| generic_anonymous.kt:17:9:17:29 | T0 | T0 |
48-
| generic_anonymous.kt:17:26:17:29 | null | Void |
48+
| generic_anonymous.kt:17:26:17:29 | null | <nulltype> |
4949
| generic_anonymous.kt:21:9:21:29 | T1 | T1 |
50-
| generic_anonymous.kt:21:26:21:29 | null | Void |
50+
| generic_anonymous.kt:21:26:21:29 | null | <nulltype> |
5151
| generic_anonymous.kt:24:5:32:5 | Unit | Unit |
5252
| generic_anonymous.kt:25:9:31:9 | Unit | Unit |
5353
| generic_anonymous.kt:26:13:26:37 | <Stmt> | new Object(...) { ... } |

java/ql/test-kotlin2/library-tests/classes/genericExprTypes.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
| generic_anonymous.kt:13:27:13:47 | get(...) | int |
4646
| generic_anonymous.kt:13:40:13:40 | i | int |
4747
| generic_anonymous.kt:17:9:17:29 | T0 | T0 |
48-
| generic_anonymous.kt:17:26:17:29 | null | Void |
48+
| generic_anonymous.kt:17:26:17:29 | null | <nulltype> |
4949
| generic_anonymous.kt:21:9:21:29 | T1 | T1 |
50-
| generic_anonymous.kt:21:26:21:29 | null | Void |
50+
| generic_anonymous.kt:21:26:21:29 | null | <nulltype> |
5151
| generic_anonymous.kt:24:5:32:5 | Unit | Unit |
5252
| generic_anonymous.kt:25:9:31:9 | Unit | Unit |
5353
| generic_anonymous.kt:26:13:26:37 | <Stmt> | new Object(...) { ... } |

0 commit comments

Comments
 (0)