Skip to content

Commit 7889d9c

Browse files
committed
Kotlin: ignore enhanced nullability when extracting primitive types
Otherwise we'll mistake `@NotNull Integer` for `int` and similar, causing a mismatch vs. Java signatures.
1 parent 8e8fb3d commit 7889d9c

File tree

7 files changed

+49
-1
lines changed

7 files changed

+49
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature
2222
import org.jetbrains.kotlin.load.java.JvmAbi
2323
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
2424
import org.jetbrains.kotlin.load.java.structure.*
25+
import org.jetbrains.kotlin.load.java.typeEnhancement.hasEnhancedNullability
2526
import org.jetbrains.kotlin.load.kotlin.getJvmModuleNameForDeserializedDescriptor
2627
import org.jetbrains.kotlin.name.FqName
2728
import org.jetbrains.kotlin.name.NameUtils
@@ -669,7 +670,8 @@ open class KotlinUsesExtractor(
669670
otherIsPrimitive: Boolean,
670671
javaClass: IrClass,
671672
kotlinPackageName: String, kotlinClassName: String): TypeResults {
672-
val javaResult = if ((context == TypeContext.RETURN || (context == TypeContext.OTHER && otherIsPrimitive)) && !s.isNullable() && primitiveName != null) {
673+
// Note the use of `hasEnhancedNullability` here covers cases like `@NotNull Integer`, which must be extracted as `Integer` not `int`.
674+
val javaResult = if ((context == TypeContext.RETURN || (context == TypeContext.OTHER && otherIsPrimitive)) && !s.isNullable() && s.kotlinType?.hasEnhancedNullability() != true && primitiveName != null) {
673675
val label: Label<DbPrimitive> = tw.getLabelFor("@\"type;$primitiveName\"", {
674676
tw.writePrimitives(it, primitiveName)
675677
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.jetbrains.annotations;
2+
import java.lang.annotation.*;
3+
4+
// Stub of @NotNull:
5+
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})
6+
public @interface NotNull { }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import org.jetbrains.annotations.NotNull;
2+
3+
public class Test {
4+
5+
public @NotNull Integer f(@NotNull Integer p) { return p; }
6+
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
exprs
2+
| Test.java:5:19:5:25 | Integer | Integer |
3+
| Test.java:5:38:5:44 | Integer | Integer |
4+
| Test.java:5:58:5:58 | p | Integer |
5+
| user.kt:2:3:2:16 | x | int |
6+
| user.kt:2:11:2:11 | t | Test |
7+
| user.kt:2:13:2:16 | <implicit not null> | int |
8+
| user.kt:2:13:2:16 | f(...) | Integer |
9+
| user.kt:2:13:2:16 | int | int |
10+
| user.kt:2:15:2:15 | 5 | int |
11+
| user.kt:3:10:3:10 | x | int |
12+
#select
13+
| Test.java:5:27:5:27 | f | Integer |
14+
| user.kt:1:1:4:1 | f | Test |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from create_database_utils import *
2+
import glob
3+
4+
os.mkdir('build')
5+
runSuccessfully(["javac"] + glob.glob("*.java") + ["-d", "build"])
6+
run_codeql_database_create(["javac " + " ".join(glob.glob("*.java")) + " -d build", "kotlinc user.kt -cp build"], lang="java")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java
2+
3+
query predicate exprs(Expr e, string t) {
4+
e.getEnclosingCallable().getDeclaringType().fromSource() and t = e.getType().toString()
5+
}
6+
7+
from Method m
8+
where m.fromSource()
9+
select m, m.getAParamType().toString()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fun f(t: Test): Int {
2+
val x = t.f(5)
3+
return x
4+
}

0 commit comments

Comments
 (0)