Skip to content

Commit

Permalink
Kotlin extractor: use special <nulltype> for null literals
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smowton committed Nov 1, 2024
1 parent cec0544 commit 5d3f723
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5745,7 +5745,14 @@ open class KotlinFileExtractor(
) =
exprIdOrFresh<DbNullliteral>(overrideId).also {
val type = useType(t)
tw.writeExprs_nullliteral(it, type.javaResult.id, parent, idx)
// Match Java by using a special <nulltype> for nulls, rather than Kotlin's view of this which is
// kotlin.Nothing?, the type that can only contain null.
val nullTypeName = "<nulltype>"
val javaNullType = tw.getLabelFor(
"@\"type;$nullTypeName\"",
{ tw.writePrimitives(it, nullTypeName) }
)
tw.writeExprs_nullliteral(it, javaNullType, parent, idx)
tw.writeExprsKotlinType(it, type.kotlinResult.id)
extractExprContext(it, locId, callable, enclosingStmt)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
| generic_anonymous.kt:13:27:13:47 | get(...) | int |
| generic_anonymous.kt:13:40:13:40 | i | int |
| generic_anonymous.kt:17:9:17:29 | T0 | T0 |
| generic_anonymous.kt:17:26:17:29 | null | Void |
| generic_anonymous.kt:17:26:17:29 | null | <nulltype> |
| generic_anonymous.kt:21:9:21:29 | T1 | T1 |
| generic_anonymous.kt:21:26:21:29 | null | Void |
| generic_anonymous.kt:21:26:21:29 | null | <nulltype> |
| generic_anonymous.kt:24:5:32:5 | Unit | Unit |
| generic_anonymous.kt:25:9:31:9 | Unit | Unit |
| generic_anonymous.kt:26:13:26:37 | <Stmt> | new Object(...) { ... } |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
| generic_anonymous.kt:13:27:13:47 | get(...) | int |
| generic_anonymous.kt:13:40:13:40 | i | int |
| generic_anonymous.kt:17:9:17:29 | T0 | T0 |
| generic_anonymous.kt:17:26:17:29 | null | Void |
| generic_anonymous.kt:17:26:17:29 | null | <nulltype> |
| generic_anonymous.kt:21:9:21:29 | T1 | T1 |
| generic_anonymous.kt:21:26:21:29 | null | Void |
| generic_anonymous.kt:21:26:21:29 | null | <nulltype> |
| generic_anonymous.kt:24:5:32:5 | Unit | Unit |
| generic_anonymous.kt:25:9:31:9 | Unit | Unit |
| generic_anonymous.kt:26:13:26:37 | <Stmt> | new Object(...) { ... } |
Expand Down

0 comments on commit 5d3f723

Please sign in to comment.