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.
This commit is contained in:
Chris Smowton
2024-11-01 16:14:04 +00:00
parent cec0544ca5
commit 5d3f723df9
3 changed files with 12 additions and 5 deletions

View File

@@ -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)
}