Skip to content

Commit 843f847

Browse files
authored
Merge pull request github#10921 from smowton/smowton/fix/ignore-enhanced-nullability
Kotlin: ignore enhanced nullability when extracting primitive types
2 parents 89ca7e2 + b80bf4a commit 843f847

File tree

9 files changed

+61
-1
lines changed

9 files changed

+61
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.codeql
22

33
import com.github.codeql.utils.*
44
import com.github.codeql.utils.versions.codeQlWithHasQuestionMark
5+
import com.github.codeql.utils.versions.getKotlinType
56
import com.github.codeql.utils.versions.isRawType
67
import com.semmle.extractor.java.OdasaOutput
78
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
@@ -22,6 +23,7 @@ import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature
2223
import org.jetbrains.kotlin.load.java.JvmAbi
2324
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
2425
import org.jetbrains.kotlin.load.java.structure.*
26+
import org.jetbrains.kotlin.load.java.typeEnhancement.hasEnhancedNullability
2527
import org.jetbrains.kotlin.load.kotlin.getJvmModuleNameForDeserializedDescriptor
2628
import org.jetbrains.kotlin.name.FqName
2729
import org.jetbrains.kotlin.name.NameUtils
@@ -674,7 +676,8 @@ open class KotlinUsesExtractor(
674676
otherIsPrimitive: Boolean,
675677
javaClass: IrClass,
676678
kotlinPackageName: String, kotlinClassName: String): TypeResults {
677-
val javaResult = if ((context == TypeContext.RETURN || (context == TypeContext.OTHER && otherIsPrimitive)) && !s.isNullable() && primitiveName != null) {
679+
// Note the use of `hasEnhancedNullability` here covers cases like `@NotNull Integer`, which must be extracted as `Integer` not `int`.
680+
val javaResult = if ((context == TypeContext.RETURN || (context == TypeContext.OTHER && otherIsPrimitive)) && !s.isNullable() && getKotlinType(s)?.hasEnhancedNullability() != true && primitiveName != null) {
678681
val label: Label<DbPrimitive> = tw.getLabelFor("@\"type;$primitiveName\"", {
679682
tw.writePrimitives(it, primitiveName)
680683
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.types.IrSimpleType
4+
import org.jetbrains.kotlin.ir.types.impl.IrTypeBase
5+
6+
fun getKotlinType(s: IrSimpleType) = (s as? IrTypeBase)?.kotlinType
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.types.IrSimpleType
4+
5+
fun getKotlinType(s: IrSimpleType) = s.kotlinType
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)