From 5d3f723df960d02cdc900c2f65360a2094bf0111 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Fri, 1 Nov 2024 16:14:04 +0000 Subject: [PATCH 1/2] Kotlin extractor: use special 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. --- .../src/main/kotlin/KotlinFileExtractor.kt | 9 ++++++++- .../library-tests/classes/genericExprTypes.expected | 4 ++-- .../library-tests/classes/genericExprTypes.expected | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt index cf41f9639cfd..d8ee7406c395 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt @@ -5745,7 +5745,14 @@ open class KotlinFileExtractor( ) = exprIdOrFresh(overrideId).also { val type = useType(t) - tw.writeExprs_nullliteral(it, type.javaResult.id, parent, idx) + // Match Java by using a special for nulls, rather than Kotlin's view of this which is + // kotlin.Nothing?, the type that can only contain null. + val nullTypeName = "" + 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) } diff --git a/java/ql/test-kotlin1/library-tests/classes/genericExprTypes.expected b/java/ql/test-kotlin1/library-tests/classes/genericExprTypes.expected index 95594fa9c69e..b350fd8d7bb9 100644 --- a/java/ql/test-kotlin1/library-tests/classes/genericExprTypes.expected +++ b/java/ql/test-kotlin1/library-tests/classes/genericExprTypes.expected @@ -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 | | | 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 | | | 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 | | new Object(...) { ... } | diff --git a/java/ql/test-kotlin2/library-tests/classes/genericExprTypes.expected b/java/ql/test-kotlin2/library-tests/classes/genericExprTypes.expected index 383b5ace4bff..a61d001d270c 100644 --- a/java/ql/test-kotlin2/library-tests/classes/genericExprTypes.expected +++ b/java/ql/test-kotlin2/library-tests/classes/genericExprTypes.expected @@ -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 | | | 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 | | | 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 | | new Object(...) { ... } | From 81ff3945330eb5ced6102db86399a84c1a9e623b Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Fri, 1 Nov 2024 19:02:28 +0000 Subject: [PATCH 2/2] Be explicit about Kotlin database type --- java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt index d8ee7406c395..42617078ff54 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt @@ -5748,7 +5748,7 @@ open class KotlinFileExtractor( // Match Java by using a special for nulls, rather than Kotlin's view of this which is // kotlin.Nothing?, the type that can only contain null. val nullTypeName = "" - val javaNullType = tw.getLabelFor( + val javaNullType = tw.getLabelFor( "@\"type;$nullTypeName\"", { tw.writePrimitives(it, nullTypeName) } )