Skip to content

Commit cec503e

Browse files
committed
Kotlin: Fix build with 2.1.20-Beta1
1 parent 4e798b3 commit cec503e

File tree

14 files changed

+112
-43
lines changed

14 files changed

+112
-43
lines changed

docs/codeql/reusables/supported-versions-compilers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Java,"Java 7 to 22 [5]_","javac (OpenJDK and Oracle JDK),
2121

2222
Eclipse compiler for Java (ECJ) [6]_",``.java``
23-
Kotlin,"Kotlin 1.5.0 to 2.1.0\ *x*","kotlinc",``.kt``
23+
Kotlin,"Kotlin 1.5.0 to 2.1.2\ *x*","kotlinc",``.kt``
2424
JavaScript,ECMAScript 2022 or lower,Not applicable,"``.js``, ``.jsx``, ``.mjs``, ``.es``, ``.es6``, ``.htm``, ``.html``, ``.xhtm``, ``.xhtml``, ``.vue``, ``.hbs``, ``.ejs``, ``.njk``, ``.json``, ``.yaml``, ``.yml``, ``.raml``, ``.xml`` [7]_"
2525
Python [8]_,"2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13",Not applicable,``.py``
2626
Ruby [9]_,"up to 3.3",Not applicable,"``.rb``, ``.erb``, ``.gemspec``, ``Gemfile``"

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,19 @@ import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunction
2525
import org.jetbrains.kotlin.ir.expressions.*
2626
import org.jetbrains.kotlin.ir.expressions.impl.*
2727
import org.jetbrains.kotlin.ir.symbols.*
28-
import org.jetbrains.kotlin.ir.types.*
28+
import org.jetbrains.kotlin.ir.types.classFqName
29+
import org.jetbrains.kotlin.ir.types.classifierOrFail
30+
import org.jetbrains.kotlin.ir.types.classOrNull
31+
import org.jetbrains.kotlin.ir.types.isAny
32+
import org.jetbrains.kotlin.ir.types.isNullableAny
33+
import org.jetbrains.kotlin.ir.types.typeOrNull
34+
import org.jetbrains.kotlin.ir.types.typeWith
35+
import org.jetbrains.kotlin.ir.types.typeWithArguments
36+
import org.jetbrains.kotlin.ir.types.IrSimpleType
37+
import org.jetbrains.kotlin.ir.types.IrStarProjection
38+
import org.jetbrains.kotlin.ir.types.IrType
39+
import org.jetbrains.kotlin.ir.types.IrTypeArgument
40+
import org.jetbrains.kotlin.ir.types.IrTypeProjection
2941
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
3042
import org.jetbrains.kotlin.ir.util.*
3143
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
@@ -2293,7 +2305,7 @@ open class KotlinFileExtractor(
22932305
// synthesised and inherit the annotation from the delegate (which given it has
22942306
// @NotNull, is likely written in Java)
22952307
JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION.takeUnless {
2296-
t.isNullable() ||
2308+
t.isNullableCodeQL() ||
22972309
primitiveTypeMapping.getPrimitiveInfo(t) != null ||
22982310
hasExistingAnnotation(it)
22992311
}
@@ -3975,7 +3987,7 @@ open class KotlinFileExtractor(
39753987
target.parent
39763988
} else {
39773989
val st = extensionReceiverParameter.type as? IrSimpleType
3978-
if (isNullable != null && st?.isNullable() != isNullable) {
3990+
if (isNullable != null && st?.isNullableCodeQL() != isNullable) {
39793991
verboseln("Nullablility of type didn't match")
39803992
return false
39813993
}
@@ -4621,9 +4633,9 @@ open class KotlinFileExtractor(
46214633
val isPrimitiveArrayCreation = !isBuiltinCallKotlin(c, "arrayOf")
46224634
val elementType =
46234635
if (isPrimitiveArrayCreation) {
4624-
c.type.getArrayElementType(pluginContext.irBuiltIns)
4636+
c.type.getArrayElementTypeCodeQL(pluginContext.irBuiltIns)
46254637
} else {
4626-
// TODO: is there any reason not to always use getArrayElementType?
4638+
// TODO: is there any reason not to always use getArrayElementTypeCodeQL?
46274639
if (c.typeArgumentsCount == 1) {
46284640
c.getTypeArgument(0).also {
46294641
if (it == null) {

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

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,24 @@ import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
1212
import org.jetbrains.kotlin.ir.declarations.*
1313
import org.jetbrains.kotlin.ir.expressions.*
1414
import org.jetbrains.kotlin.ir.symbols.*
15-
import org.jetbrains.kotlin.ir.types.*
15+
import org.jetbrains.kotlin.ir.types.addAnnotations
16+
import org.jetbrains.kotlin.ir.types.classFqName
17+
import org.jetbrains.kotlin.ir.types.classifierOrNull
18+
import org.jetbrains.kotlin.ir.types.classOrNull
19+
import org.jetbrains.kotlin.ir.types.isAny
20+
import org.jetbrains.kotlin.ir.types.isNullableAny
21+
import org.jetbrains.kotlin.ir.types.isPrimitiveType
22+
import org.jetbrains.kotlin.ir.types.makeNullable
23+
import org.jetbrains.kotlin.ir.types.typeOrNull
24+
import org.jetbrains.kotlin.ir.types.typeWith
25+
import org.jetbrains.kotlin.ir.types.typeWithArguments
26+
import org.jetbrains.kotlin.ir.types.IrDynamicType
27+
import org.jetbrains.kotlin.ir.types.IrErrorType
28+
import org.jetbrains.kotlin.ir.types.IrSimpleType
29+
import org.jetbrains.kotlin.ir.types.IrStarProjection
30+
import org.jetbrains.kotlin.ir.types.IrType
31+
import org.jetbrains.kotlin.ir.types.IrTypeArgument
32+
import org.jetbrains.kotlin.ir.types.IrTypeProjection
1633
import org.jetbrains.kotlin.ir.types.impl.*
1734
import org.jetbrains.kotlin.ir.util.*
1835
import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature
@@ -679,7 +696,7 @@ open class KotlinUsesExtractor(
679696
private fun getInvariantNullableArrayType(arrayType: IrSimpleType): IrSimpleType =
680697
if (arrayType.isPrimitiveArray()) arrayType
681698
else {
682-
val componentType = arrayType.getArrayElementType(pluginContext.irBuiltIns)
699+
val componentType = arrayType.getArrayElementTypeCodeQL(pluginContext.irBuiltIns)
683700
val componentTypeBroadened =
684701
when (componentType) {
685702
is IrSimpleType ->
@@ -690,7 +707,7 @@ open class KotlinUsesExtractor(
690707
val unchanged =
691708
componentType == componentTypeBroadened &&
692709
(arrayType.arguments[0] as? IrTypeProjection)?.variance == Variance.INVARIANT &&
693-
componentType.isNullable()
710+
componentType.isNullableCodeQL()
694711
if (unchanged) arrayType
695712
else
696713
IrSimpleTypeImpl(
@@ -705,7 +722,7 @@ open class KotlinUsesExtractor(
705722
Kotlin arrays can be broken down as:
706723
707724
isArray(t)
708-
|- t.isBoxedArray
725+
|- t.isBoxedArrayCodeQL
709726
| |- t.isArray() e.g. Array<Boolean>, Array<Boolean?>
710727
| |- t.isNullableArray() e.g. Array<Boolean>?, Array<Boolean?>?
711728
|- t.isPrimitiveArray() e.g. BooleanArray
@@ -715,7 +732,7 @@ open class KotlinUsesExtractor(
715732
Primitive arrays are represented as e.g. boolean[].
716733
*/
717734

718-
private fun isArray(t: IrType) = t.isBoxedArray || t.isPrimitiveArray()
735+
private fun isArray(t: IrType) = t.isBoxedArrayCodeQL || t.isPrimitiveArray()
719736

720737
data class ArrayInfo(
721738
val elementTypeResults: TypeResults,
@@ -756,7 +773,7 @@ open class KotlinUsesExtractor(
756773
) {
757774
pluginContext.irBuiltIns.anyType
758775
} else {
759-
t.getArrayElementType(pluginContext.irBuiltIns)
776+
t.getArrayElementTypeCodeQL(pluginContext.irBuiltIns)
760777
}
761778

762779
val recInfo = useArrayType(elementType, t.isPrimitiveArray())
@@ -844,7 +861,7 @@ open class KotlinUsesExtractor(
844861
if (
845862
(context == TypeContext.RETURN ||
846863
(context == TypeContext.OTHER && otherIsPrimitive)) &&
847-
!s.isNullable() &&
864+
!s.isNullableCodeQL() &&
848865
getKotlinType(s)?.hasEnhancedNullability() != true &&
849866
primitiveName != null
850867
) {
@@ -860,7 +877,7 @@ open class KotlinUsesExtractor(
860877
val kotlinClassId = useClassInstance(kotlinClass, listOf()).typeResult.id
861878
val kotlinResult =
862879
if (true) TypeResult(fakeKotlinType(), "TODO", "TODO")
863-
else if (s.isNullable()) {
880+
else if (s.isNullableCodeQL()) {
864881
val kotlinSignature =
865882
"$kotlinPackageName.$kotlinClassName?" // TODO: Is this right?
866883
val kotlinLabel = "@\"kt_type;nullable;$kotlinPackageName.$kotlinClassName\""
@@ -902,21 +919,21 @@ open class KotlinUsesExtractor(
902919
return extractErrorType()
903920
}
904921
}
905-
(s.isBoxedArray && s.arguments.isNotEmpty()) || s.isPrimitiveArray() -> {
922+
(s.isBoxedArrayCodeQL && s.arguments.isNotEmpty()) || s.isPrimitiveArray() -> {
906923
val arrayInfo = useArrayType(s, false)
907924
return arrayInfo.componentTypeResults
908925
}
909926
owner is IrClass -> {
910927
val args = if (s.codeQlIsRawType()) null else s.arguments
911928

912-
return useSimpleTypeClass(owner, args, s.isNullable())
929+
return useSimpleTypeClass(owner, args, s.isNullableCodeQL())
913930
}
914931
owner is IrTypeParameter -> {
915932
val javaResult = useTypeParameter(owner)
916933
val aClassId = makeClass("kotlin", "TypeParam") // TODO: Wrong
917934
val kotlinResult =
918935
if (true) TypeResult(fakeKotlinType(), "TODO", "TODO")
919-
else if (s.isNullable()) {
936+
else if (s.isNullableCodeQL()) {
920937
val kotlinSignature = "${javaResult.signature}?" // TODO: Wrong
921938
val kotlinLabel = "@\"kt_type;nullable;type_param\"" // TODO: Wrong
922939
val kotlinId: Label<DbKt_nullable_type> =
@@ -1200,7 +1217,7 @@ open class KotlinUsesExtractor(
12001217
}
12011218

12021219
private fun extendsAdditionAllowed(t: IrType) =
1203-
if (t.isBoxedArray) {
1220+
if (t.isBoxedArrayCodeQL) {
12041221
if (t is IrSimpleType) {
12051222
arrayExtendsAdditionAllowed(t)
12061223
} else {
@@ -1493,7 +1510,7 @@ open class KotlinUsesExtractor(
14931510
}
14941511
} else {
14951512
t.classOrNull?.let { tCls ->
1496-
if (t.isBoxedArray) {
1513+
if (t.isBoxedArrayCodeQL) {
14971514
(t.arguments.singleOrNull() as? IrTypeProjection)?.let { elementTypeArg
14981515
->
14991516
val elementType = elementTypeArg.type
@@ -1506,7 +1523,7 @@ open class KotlinUsesExtractor(
15061523
)
15071524
return tCls
15081525
.typeWithArguments(listOf(newArg))
1509-
.codeQlWithHasQuestionMark(t.isNullable())
1526+
.codeQlWithHasQuestionMark(t.isNullableCodeQL())
15101527
}
15111528
}
15121529
}
@@ -2086,12 +2103,12 @@ open class KotlinUsesExtractor(
20862103
}
20872104

20882105
if (owner is IrClass) {
2089-
if (t.isBoxedArray) {
2090-
val elementType = t.getArrayElementType(pluginContext.irBuiltIns)
2106+
if (t.isBoxedArrayCodeQL) {
2107+
val elementType = t.getArrayElementTypeCodeQL(pluginContext.irBuiltIns)
20912108
val erasedElementType = erase(elementType)
20922109
return owner
20932110
.typeWith(erasedElementType)
2094-
.codeQlWithHasQuestionMark(t.isNullable())
2111+
.codeQlWithHasQuestionMark(t.isNullableCodeQL())
20952112
}
20962113

20972114
return if (t.arguments.isNotEmpty())

java/kotlin-extractor/src/main/kotlin/utils/ClassNames.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.github.codeql.utils.versions.*
55
import com.intellij.openapi.vfs.StandardFileSystems
66
import com.intellij.openapi.vfs.VirtualFile
77
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
8-
import org.jetbrains.kotlin.fir.java.JavaBinarySourceElement
8+
import org.jetbrains.kotlin.fir.java.VirtualFileBasedSourceElement
99
import org.jetbrains.kotlin.ir.IrElement
1010
import org.jetbrains.kotlin.ir.declarations.*
1111
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
@@ -89,8 +89,8 @@ fun getIrClassVirtualFile(irClass: IrClass): VirtualFile? {
8989
is BinaryJavaClass -> return element.virtualFile
9090
}
9191
}
92-
is JavaBinarySourceElement -> {
93-
return cSource.javaClass.virtualFile
92+
is VirtualFileBasedSourceElement -> {
93+
return cSource.virtualFile
9494
}
9595
is KotlinJvmBinarySourceElement -> {
9696
val binaryClass = cSource.binaryClass

java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.github.codeql.Logger
55
import com.github.codeql.getJavaEquivalentClassId
66
import com.github.codeql.utils.versions.codeQlWithHasQuestionMark
77
import com.github.codeql.utils.versions.createImplicitParameterDeclarationWithWrappedDescriptor
8+
import com.github.codeql.utils.versions.*
89
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
910
import org.jetbrains.kotlin.descriptors.ClassKind
1011
import org.jetbrains.kotlin.ir.builders.declarations.addConstructor
@@ -18,15 +19,18 @@ import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
1819
import org.jetbrains.kotlin.ir.expressions.impl.*
1920
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
2021
import org.jetbrains.kotlin.ir.symbols.impl.DescriptorlessExternalPackageFragmentSymbol
21-
import org.jetbrains.kotlin.ir.types.*
22+
import org.jetbrains.kotlin.ir.types.addAnnotations
23+
import org.jetbrains.kotlin.ir.types.classifierOrNull
24+
import org.jetbrains.kotlin.ir.types.typeWith
25+
import org.jetbrains.kotlin.ir.types.IrSimpleType
26+
import org.jetbrains.kotlin.ir.types.IrStarProjection
27+
import org.jetbrains.kotlin.ir.types.IrType
28+
import org.jetbrains.kotlin.ir.types.IrTypeArgument
29+
import org.jetbrains.kotlin.ir.types.IrTypeProjection
2230
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
2331
import org.jetbrains.kotlin.ir.types.impl.IrStarProjectionImpl
2432
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
25-
import org.jetbrains.kotlin.ir.util.classId
26-
import org.jetbrains.kotlin.ir.util.constructedClassType
27-
import org.jetbrains.kotlin.ir.util.constructors
28-
import org.jetbrains.kotlin.ir.util.kotlinFqName
29-
import org.jetbrains.kotlin.ir.util.parents
33+
import org.jetbrains.kotlin.ir.util.*
3034
import org.jetbrains.kotlin.name.FqName
3135
import org.jetbrains.kotlin.name.Name
3236
import org.jetbrains.kotlin.types.Variance
@@ -57,7 +61,7 @@ private fun IrSimpleType.substituteTypeArguments(
5761
}
5862
}
5963

60-
return IrSimpleTypeImpl(classifier, isNullable(), newArguments, annotations)
64+
return IrSimpleTypeImpl(classifier, isNullableCodeQL(), newArguments, annotations)
6165
}
6266

6367
/**
@@ -96,7 +100,7 @@ private fun subProjectedType(
96100
else {
97101
val newProjectedType =
98102
substitutedTypeArg.type.let {
99-
if (t.isNullable()) it.codeQlWithHasQuestionMark(true) else it
103+
if (t.isNullableCodeQL()) it.codeQlWithHasQuestionMark(true) else it
100104
}
101105
val newVariance = combineVariance(outerVariance, substitutedTypeArg.variance)
102106
makeTypeProjection(newProjectedType, newVariance)
@@ -113,7 +117,7 @@ private fun IrTypeArgument.upperBound(context: IrPluginContext) =
113117
when (this.variance) {
114118
Variance.INVARIANT -> this.type
115119
Variance.IN_VARIANCE ->
116-
if (this.type.isNullable()) context.irBuiltIns.anyNType
120+
if (this.type.isNullableCodeQL()) context.irBuiltIns.anyNType
117121
else context.irBuiltIns.anyType
118122
Variance.OUT_VARIANCE -> this.type
119123
}
@@ -128,7 +132,7 @@ private fun IrTypeArgument.lowerBound(context: IrPluginContext) =
128132
Variance.INVARIANT -> this.type
129133
Variance.IN_VARIANCE -> this.type
130134
Variance.OUT_VARIANCE ->
131-
if (this.type.isNullable()) context.irBuiltIns.nothingNType
135+
if (this.type.isNullableCodeQL()) context.irBuiltIns.nothingNType
132136
else context.irBuiltIns.nothingType
133137
}
134138
else -> context.irBuiltIns.nothingType
@@ -211,7 +215,7 @@ fun IrTypeArgument.withQuestionMark(b: Boolean): IrTypeArgument =
211215
this.type.let {
212216
when (it) {
213217
is IrSimpleType ->
214-
if (it.isNullable() == b) this
218+
if (it.isNullableCodeQL() == b) this
215219
else makeTypeProjection(it.codeQlWithHasQuestionMark(b), this.variance)
216220
else -> this
217221
}
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.descriptors.IrBuiltIns
4+
5+
typealias IrBuiltIns = IrBuiltIns
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.jetbrains.kotlin.fir.java
22

3+
import com.intellij.openapi.vfs.VirtualFile
34
import org.jetbrains.kotlin.descriptors.SourceElement
45
import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass
56

67
/*
78
We need this class to exist, but the compiler will never give us an
89
instance of it.
910
*/
10-
abstract class JavaBinarySourceElement private constructor(val javaClass: BinaryJavaClass) :
11-
SourceElement {}
11+
abstract class VirtualFileBasedSourceElement private constructor(val javaClass: BinaryJavaClass) : SourceElement {
12+
abstract val virtualFile: VirtualFile
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.codeql.utils.versions
2+
3+
import org.jetbrains.kotlin.ir.types.*
4+
5+
fun IrType.isNullableCodeQL(): Boolean =
6+
this.isNullable()
7+
8+
val IrType.isBoxedArrayCodeQL: Boolean by IrType::isBoxedArray
9+
10+
fun IrType.getArrayElementTypeCodeQL(irBuiltIns: IrBuiltIns): IrType =
11+
this.getArrayElementType(irBuiltIns)
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.IrBuiltIns
4+
5+
typealias IrBuiltIns = org.jetbrains.kotlin.ir.IrBuiltIns

java/kotlin-extractor/src/main/kotlin/utils/versions/v_2_0_0-RC1/JavaBinarySourceElement.kt

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)