Skip to content

Add support for Kotlin 2.2.0; drop Kotlin 1.5.x #19402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
17 changes: 4 additions & 13 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "abseil-cpp", version = "20240116.1", repo_name = "absl")
bazel_dep(name = "nlohmann_json", version = "3.11.3", repo_name = "json")
bazel_dep(name = "fmt", version = "10.0.0")
bazel_dep(name = "rules_kotlin", version = "2.0.0-codeql.1")
bazel_dep(name = "rules_kotlin", version = "2.1.3-codeql.1")
bazel_dep(name = "gazelle", version = "0.40.0")
bazel_dep(name = "rules_dotnet", version = "0.17.4")
bazel_dep(name = "googletest", version = "1.14.0.bcr.1")
Expand Down Expand Up @@ -193,10 +193,6 @@ use_repo(
kotlin_extractor_deps,
"codeql_kotlin_defaults",
"codeql_kotlin_embeddable",
"kotlin-compiler-1.5.0",
"kotlin-compiler-1.5.10",
"kotlin-compiler-1.5.20",
"kotlin-compiler-1.5.30",
"kotlin-compiler-1.6.0",
"kotlin-compiler-1.6.20",
"kotlin-compiler-1.7.0",
Expand All @@ -208,10 +204,7 @@ use_repo(
"kotlin-compiler-2.0.20-Beta2",
"kotlin-compiler-2.1.0-Beta1",
"kotlin-compiler-2.1.20-Beta1",
"kotlin-compiler-embeddable-1.5.0",
"kotlin-compiler-embeddable-1.5.10",
"kotlin-compiler-embeddable-1.5.20",
"kotlin-compiler-embeddable-1.5.30",
"kotlin-compiler-2.2.0-Beta1",
"kotlin-compiler-embeddable-1.6.0",
"kotlin-compiler-embeddable-1.6.20",
"kotlin-compiler-embeddable-1.7.0",
Expand All @@ -223,10 +216,7 @@ use_repo(
"kotlin-compiler-embeddable-2.0.20-Beta2",
"kotlin-compiler-embeddable-2.1.0-Beta1",
"kotlin-compiler-embeddable-2.1.20-Beta1",
"kotlin-stdlib-1.5.0",
"kotlin-stdlib-1.5.10",
"kotlin-stdlib-1.5.20",
"kotlin-stdlib-1.5.30",
"kotlin-compiler-embeddable-2.2.0-Beta1",
"kotlin-stdlib-1.6.0",
"kotlin-stdlib-1.6.20",
"kotlin-stdlib-1.7.0",
Expand All @@ -238,6 +228,7 @@ use_repo(
"kotlin-stdlib-2.0.20-Beta2",
"kotlin-stdlib-2.1.0-Beta1",
"kotlin-stdlib-2.1.20-Beta1",
"kotlin-stdlib-2.2.0-Beta1",
)

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
Expand Down
2 changes: 1 addition & 1 deletion docs/codeql/reusables/supported-versions-compilers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Java,"Java 7 to 24 [6]_","javac (OpenJDK and Oracle JDK),

Eclipse compiler for Java (ECJ) [7]_",``.java``
Kotlin,"Kotlin 1.5.0 to 2.1.2\ *x*","kotlinc",``.kt``
Kotlin,"Kotlin 1.6.0 to 2.2.0\ *x*","kotlinc",``.kt``
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`` [8]_"
Python [9]_,"2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13",Not applicable,``.py``
Ruby [10]_,"up to 3.3",Not applicable,"``.rb``, ``.erb``, ``.gemspec``, ``Gemfile``"
Expand Down
2 changes: 1 addition & 1 deletion java/kotlin-extractor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ sourceSets {
}

jar {
archiveName = "${OUTPUT_JAR_NAME}"
archiveBaseName = "${OUTPUT_JAR_NAME}"
}

task getHomeDir {
Expand Down
3 changes: 3 additions & 0 deletions java/kotlin-extractor/deps/kotlin-compiler-2.2.0-Beta1.jar
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions java/kotlin-extractor/deps/kotlin-stdlib-2.2.0-Beta1.jar
Git LFS file not shown
2 changes: 1 addition & 1 deletion java/kotlin-extractor/dev/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io
import os

DEFAULT_VERSION = "2.0.0"
DEFAULT_VERSION = "2.1.20"


def options():
Expand Down
16 changes: 2 additions & 14 deletions java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunction
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.*
Expand Down Expand Up @@ -1619,9 +1618,7 @@ open class KotlinFileExtractor(
// (NB. kotlinc's actual implementation strategy is different -- it makes an inner class
// called InterfaceWithDefault$DefaultImpls and stores the default methods
// there to allow default method usage in Java < 8, but this is hopefully niche.
!jvmDefaultModeEnabledIsEnabled(
pluginContext.languageVersionSettings
.getFlag(JvmAnalysisFlags.jvmDefaultMode)) &&
!jvmDefaultModeEnabledIsEnabled(getJvmDefaultMode(pluginContext.languageVersionSettings)) &&
f.parentClassOrNull.let {
it != null &&
it.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB &&
Expand Down Expand Up @@ -6011,11 +6008,7 @@ open class KotlinFileExtractor(
is IrGetValue -> {
val exprParent = parent.expr(e, callable)
val owner = e.symbol.owner
if (
owner is IrValueParameter &&
owner.index == -1 &&
!owner.isExtensionReceiver()
) {
if (owner is IrValueParameter && isDispatchReceiver(owner)) {
extractThisAccess(e, owner.parent, exprParent, callable)
} else {
val isAnnotationClassParameter =
Expand Down Expand Up @@ -6966,11 +6959,6 @@ open class KotlinFileExtractor(
}
}

private fun IrValueParameter.isExtensionReceiver(): Boolean {
val parentFun = parent as? IrFunction ?: return false
return parentFun.extensionReceiverParameter == this
}

private open inner class GeneratedClassHelper(
protected val locId: Label<DbLocation>,
protected val ids: GeneratedClassLabels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2136,7 +2136,7 @@ open class KotlinUsesExtractor(
}
val parentId = parent ?: overriddenParentAttributes?.id ?: useDeclarationParentOf(vp, false)

val idxBase = overriddenParentAttributes?.valueParameters?.indexOf(vp) ?: vp.index
val idxBase = overriddenParentAttributes?.valueParameters?.indexOf(vp) ?: parameterIndexExcludingReceivers(vp)
val idxOffset =
if (
declarationParent is IrFunction &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package com.github.codeql.comments

import com.github.codeql.*
import com.github.codeql.utils.isLocalFunction
import com.github.codeql.utils.versions.isDispatchReceiver
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.util.parentClassOrNull

private fun IrValueParameter.isExtensionReceiver(): Boolean {
val parentFun = parent as? IrFunction ?: return false
return parentFun.extensionReceiverParameter == this
}

open class CommentExtractor(
protected val fileExtractor: KotlinFileExtractor,
protected val file: IrFile,
Expand All @@ -19,7 +25,7 @@ open class CommentExtractor(
protected fun getLabel(element: IrElement): Label<out DbTop>? {
if (element == file) return fileLabel

if (element is IrValueParameter && element.index == -1) {
if (element is IrValueParameter && (isDispatchReceiver(element) || element.isExtensionReceiver())) {
// Don't attribute comments to the implicit `this` parameter of a function.
return null
}
Expand Down
6 changes: 5 additions & 1 deletion java/kotlin-extractor/src/main/kotlin/utils/ClassNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ fun getIrClassVirtualFile(irClass: IrClass): VirtualFile? {
}
}
is VirtualFileBasedSourceElement -> {
return cSource.virtualFile
if (cSource.virtualFile.name.endsWith(".class")) {
// At least lately, despite VirtualFileBasedSourceElement being constructed on a BinaryJavaClass,
// this can be a .java source file.
return cSource.virtualFile
}
}
is KotlinJvmBinarySourceElement -> {
val binaryClass = cSource.binaryClass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.github.codeql.utils

import com.github.codeql.utils.versions.IrVisitor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.util.isFakeOverride
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor

class IrVisitorLookup(
private val psi2Ir: Psi2IrFacade,
private val psi: PsiElement,
private val file: IrFile
) : IrElementVisitor<Unit, MutableCollection<IrElement>> {
) : IrVisitor<Unit, MutableCollection<IrElement>>() {
private val location = psi.getLocation()

override fun visitElement(element: IrElement, data: MutableCollection<IrElement>): Unit {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.codeql.utils.versions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move the necessary bits from v_1_5* into v_1_6_0, and delete the rest, now we have dropped 1.5 support, but that can be done in a follow-up PR.


typealias IrLazyFunction = org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.codeql.utils.versions

abstract class IrVisitor<R, D> : org.jetbrains.kotlin.ir.visitors.IrElementVisitor<R, D>
abstract class IrVisitorVoid : org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.codeql.utils.versions

import org.jetbrains.kotlin.config.JvmAnalysisFlags
import org.jetbrains.kotlin.config.LanguageVersionSettings

fun getJvmDefaultMode(lvs: LanguageVersionSettings) =
lvs.getFlag(JvmAnalysisFlags.jvmDefaultMode)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.codeql.utils.versions

import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter

fun isDispatchReceiver(p: IrValueParameter) = p.index == -1 && p != (p.parent as? IrFunction)?.extensionReceiverParameter
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.codeql.utils.versions

import org.jetbrains.kotlin.ir.declarations.IrValueParameter

fun parameterIndexExcludingReceivers(vp: IrValueParameter) = vp.index
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.SYNTHETIC_OFFSET
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens
Expand Down Expand Up @@ -43,7 +42,7 @@ class CommentExtractorLighterAST(

val kDocOwners = mutableMapOf<Int, MutableList<IrElement>>()
val visitor =
object : IrElementVisitorVoid {
object : IrVisitorVoid() {
override fun visitElement(element: IrElement) {
val metadata = (element as? IrMetadataSourceOwner)?.metadata
val sourceElement = (metadata as? FirMetadataSource)?.fir?.source
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.codeql.utils.versions

typealias IrLazyFunction = org.jetbrains.kotlin.psi2ir.lazy.IrLazyFunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.codeql.utils.versions

typealias IrVisitor<R, D> = org.jetbrains.kotlin.ir.visitors.IrVisitor<R, D>
typealias IrVisitorVoid = org.jetbrains.kotlin.ir.visitors.IrVisitorVoid
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.codeql.utils.versions

import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.util.createThisReceiverParameter

fun IrClass.createImplicitParameterDeclarationWithWrappedDescriptor() =
this.createThisReceiverParameter()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.codeql.utils.versions

import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.config.jvmDefaultMode

fun getJvmDefaultMode(lvs: LanguageVersionSettings) =
lvs.jvmDefaultMode
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.codeql.utils.versions

import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.IrParameterKind

fun isDispatchReceiver(p: IrValueParameter) = p.kind == IrParameterKind.DispatchReceiver
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.codeql.utils.versions

import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter

fun parameterIndexExcludingReceivers(vp: IrValueParameter): Int {
val offset =
(vp.parent as? IrFunction)?.let { (if (it.extensionReceiverParameter != null) 1 else 0) + (if (it.dispatchReceiverParameter != null) 1 else 0) } ?: 0
return vp.indexInParameters - offset
}
5 changes: 1 addition & 4 deletions java/kotlin-extractor/versions.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# when updating this list, `bazel mod tidy` should be run from `codeql` to update `MODULE.bazel`
VERSIONS = [
"1.5.0",
"1.5.10",
"1.5.20",
"1.5.30",
"1.6.0",
"1.6.20",
"1.7.0",
Expand All @@ -15,6 +11,7 @@ VERSIONS = [
"2.0.20-Beta2",
"2.1.0-Beta1",
"2.1.20-Beta1",
"2.2.0-Beta1",
]

def _version_to_tuple(v):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"markdownMessage": "The Kotlin version installed (`999.999.999`) is too recent for this version of CodeQL. Install a version lower than 2.1.30.",
"markdownMessage": "The Kotlin version installed (`999.999.999`) is too recent for this version of CodeQL. Install a version lower than 2.2.10.",
"severity": "error",
"source": {
"extractorName": "java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
id 'org.jetbrains.kotlin.jvm' version '1.5.31'
id 'org.jetbrains.kotlin.jvm' version '1.6.0'

// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.fromSymbolOwner
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
Expand Down
4 changes: 4 additions & 0 deletions java/ql/lib/change-notes/2025-04-28-kotlin-220.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: feature
---
* Kotlin versions up to 2.2.0\ *x* are now supported. Support for the Kotlin 1.5.x series is dropped (so the minimum Kotlin version is now 1.6.0).
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ annotations
| def.kt:41:5:41:12 | Annot0k | def.kt:41:5:42:19 | Z | def.kt:5:1:21:60 | Annot0k |
| def.kt:45:1:45:8 | Annot0k | def.kt:45:1:51:1 | fn | def.kt:5:1:21:60 | Annot0k |
| def.kt:46:21:46:28 | Annot0k | def.kt:46:21:46:39 | a | def.kt:5:1:21:60 | Annot0k |
| def.kt:54:1:54:12 | Annot0k | def.kt:53:1:57:23 | getP | def.kt:5:1:21:60 | Annot0k |
| def.kt:55:1:55:12 | Annot0k | def.kt:53:1:57:23 | setP | def.kt:5:1:21:60 | Annot0k |
| def.kt:54:1:54:12 | Annot0k | def.kt:53:1:57:19 | getP | def.kt:5:1:21:60 | Annot0k |
| def.kt:55:1:55:12 | Annot0k | def.kt:53:1:57:19 | setP | def.kt:5:1:21:60 | Annot0k |
| def.kt:56:1:56:14 | Annot0k | def.kt:53:1:57:23 | p | def.kt:5:1:21:60 | Annot0k |
| def.kt:59:5:59:21 | Annot0k | def.kt:59:5:59:28 | <this> | def.kt:5:1:21:60 | Annot0k |
| use.java:10:5:10:21 | Annot0j | use.java:14:18:14:18 | Z | Annot0j.java:1:19:1:25 | Annot0j |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
| Test.java:2:17:2:17 | m | m | m |
| test.kt:3:9:4:18 | getX_prop | getX_prop | getX |
| test.kt:6:5:6:19 | getX | getX | getX |
| test.kt:8:5:10:19 | changeY | changeY | setY |
| test.kt:8:5:10:19 | y | y | getY |
| test.kt:8:5:10:14 | changeY | changeY | setY |
| test.kt:8:5:10:14 | y | y | getY |
| test.kt:12:5:13:15 | method | method | fn |
| test.kt:17:5:17:14 | p | p | p |
| test.kt:18:5:18:32 | w | w | q |
Loading