Repository: detekt/detekt (kotlin)

Issue:
K2 compiler crashes Detekt
<!-- The bug you're experiencing might have already be reported! -->
<!-- Please search in the [issues](https://github.com/detekt/detekt/issues) before creating one. -->

## Expected Behavior
After updating our project to use K2 compiler we expect we don't get a crash when we run `detekt`

## Observed Behavior
In our project when we run detekt we are getting similar exceptions to this one:
```
> Exception while analyzing expression in (135,15) in //Forlago/modules/feature-foo/src/main/kotlin/com/leinardi/forlago/feature/foo/ui/foo/FooScreen.kt

  Attachments:
  causeThrowable
  java.lang.IllegalArgumentException: fromIndex(1) > toIndex(0)
        at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
        at java.base/java.util.ArrayList.subList(ArrayList.java:1108)
        at org.jetbrains.kotlin.builtins.FunctionTypesKt.getValueParameterTypesFromFunctionType(functionTypes.kt:178)
....
```

## Steps to Reproduce
1. Update your project to use K2 compiler. To achieve that you have to update your `gradle.properties` file adding:

```
# K2
android.experimental.lint.version=8.3.0-alpha17
android.lint.useK2Uast=true
kapt.use.k2=true
kotlin.experimental.tryK2=true
```
2. Run `detekt` and we are getting several exceptions similar to:
```
> Task :modules:feature-debug:detektDebug FAILED
ERROR: Exception while analyzing expression in (200,11) in //Users/javierarroyo/Projects/Personal/Forlago/modules/feature-debug/src/main/kotlin/com/leinardi/forlago/feature/debug/ui/DebugScreen.kt

Attachments:
causeThrowable
java.lang.IllegalArgumentException: fromIndex(1) > toIndex(0)
        at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
        at java.base/java.util.ArrayList.subList(ArrayList.java:1108)
        at org.jetbrains.kotlin.builtins.FunctionTypesKt.getValueParameterTypesFromFunctionType(functionTypes.kt:178)
        at org.jetbrains.kotlin.resolve.FunctionDescriptorResolver.getValueParameters(FunctionDescriptorResolver.kt:380)
```

### Github example
You can find a project sample with the issue here:
- https://github.com/leinardi/Forlago/pull/48


## Your Environment
<!-- Include as many relevant details about the environment you experienced the bug in -->
* Version of detekt used: 1.23.4
* Version of Gradle used (if applicable): 8.5
* Gradle scan link (add `--scan` option when running the gradle task):
* Operating System and version: Mac OS Monterey - v12.7.1
* Link to your project (if it's a public repository): https://github.com/leinardi/Forlago/pull/48

Pull request:
Fixes #6715
Fixes #7458 

This is safe to release with alpha/beta dependencies in my opinion. poko 0.16.0-beta01 is based on Kotlin 2.0.0-RC3 which is the same as 2.0.0. kctfork is only used in tests.

Patch:
diff --git a/detekt-api/build.gradle.kts b/detekt-api/build.gradle.kts
index 1a14a5c2340c..dd493cc659a6 100644
--- a/detekt-api/build.gradle.kts
+++ b/detekt-api/build.gradle.kts
@@ -2,7 +2,7 @@ plugins {
     id("module")
     id("public-api")
     id("java-test-fixtures")
-    id("dev.drewhamilton.poko") version "0.15.3"
+    id("dev.drewhamilton.poko") version "0.16.0-beta01"
 }
 
 dependencies {
diff --git a/detekt-compiler-plugin/gradle.properties b/detekt-compiler-plugin/gradle.properties
index 4598e2834281..d289f803d9f1 100644
--- a/detekt-compiler-plugin/gradle.properties
+++ b/detekt-compiler-plugin/gradle.properties
@@ -1,4 +1,4 @@
-kotlinCompilerChecksum=eb7b68e01029fa67bc8d060ee54c12018f2c60ddc438cf21db14517229aa693b
+kotlinCompilerChecksum=ef578730976154fd2c5968d75af8c2703b3de84a78dffe913f670326e149da3b
 
 kotlin.code.style=official
 systemProp.sonar.host.url=http://localhost:9000
diff --git a/detekt-parser/src/main/kotlin/io/github/detekt/parser/BindingContextUtils.kt b/detekt-parser/src/main/kotlin/io/github/detekt/parser/BindingContextUtils.kt
index 4f1a3176f211..e0f35bd64080 100644
--- a/detekt-parser/src/main/kotlin/io/github/detekt/parser/BindingContextUtils.kt
+++ b/detekt-parser/src/main/kotlin/io/github/detekt/parser/BindingContextUtils.kt
@@ -36,7 +36,7 @@ fun generateBindingContext(
         TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
             environment.project,
             files,
-            NoScopeRecordCliBindingTrace(),
+            NoScopeRecordCliBindingTrace(environment.project),
             environment.configuration,
             environment::createPackagePartProvider
         )
diff --git a/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt b/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt
index 1beb4a3230b3..ad6a6d0a2a6a 100644
--- a/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt
+++ b/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt
@@ -13,8 +13,8 @@ import io.gitlab.arturbosch.detekt.api.configWithFallback
 import io.gitlab.arturbosch.detekt.api.simplePatternToRegex
 import io.gitlab.arturbosch.detekt.rules.fqNameOrNull
 import io.gitlab.arturbosch.detekt.rules.isCalling
-import org.jetbrains.kotlin.backend.common.serialization.findPackage
 import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
+import org.jetbrains.kotlin.descriptors.findPackage
 import org.jetbrains.kotlin.name.FqName
 import org.jetbrains.kotlin.psi.KtCallExpression
 import org.jetbrains.kotlin.psi.KtExpression
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index f6202ebdc8ad..01ac09023bdf 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -1,7 +1,7 @@
 [versions]
 dokka = "1.9.20"
 jacoco = "0.8.12"
-kotlin = "1.9.24"
+kotlin = "2.0.0"
 coroutines = "1.8.1"
 ktlint = "1.2.1"
 junit = "5.10.2"
@@ -42,7 +42,7 @@ assertj = "org.assertj:assertj-core:3.25.3"
 classgraph = "io.github.classgraph:classgraph:4.8.172"
 snakeyaml = "org.snakeyaml:snakeyaml-engine:2.7"
 jcommander = "org.jcommander:jcommander:1.83"
-kotlinCompileTesting = { module = "dev.zacsweers.kctfork:core", version = "0.4.1" }
+kotlinCompileTesting = { module = "dev.zacsweers.kctfork:core", version = "0.5.0-alpha07" }
 jetbrains-annotations = "org.jetbrains:annotations:24.1.0"
 
 [plugins]


Test patch:
diff --git a/detekt-test-utils/src/test/kotlin/io/github/detekt/test/utils/KotlinScriptEngineTest.kt b/detekt-test-utils/src/test/kotlin/io/github/detekt/test/utils/KotlinScriptEngineTest.kt
index dfdeaa34cd41..8b91ed380167 100644
--- a/detekt-test-utils/src/test/kotlin/io/github/detekt/test/utils/KotlinScriptEngineTest.kt
+++ b/detekt-test-utils/src/test/kotlin/io/github/detekt/test/utils/KotlinScriptEngineTest.kt
@@ -47,7 +47,7 @@ class KotlinScriptEngineTest {
 
         assertThatThrownBy { KotlinScriptEngine.compile(codeWithMissingImport) }
             .isInstanceOf(IllegalStateException::class.java)
-            .hasMessage("ERROR Unresolved reference: Random (script.main.kts:2:5)")
+            .hasMessage("ERROR Unresolved reference 'Random'. (script.main.kts:2:5)")
     }
 
     @RepeatedTest(10)
diff --git a/detekt-test/src/main/kotlin/io/gitlab/arturbosch/detekt/test/KotlinCoreEnvironmentExtensions.kt b/detekt-test/src/main/kotlin/io/gitlab/arturbosch/detekt/test/KotlinCoreEnvironmentExtensions.kt
index 0ad839be0fb3..16b5d8c398d6 100644
--- a/detekt-test/src/main/kotlin/io/gitlab/arturbosch/detekt/test/KotlinCoreEnvironmentExtensions.kt
+++ b/detekt-test/src/main/kotlin/io/gitlab/arturbosch/detekt/test/KotlinCoreEnvironmentExtensions.kt
@@ -10,7 +10,7 @@ fun KotlinCoreEnvironment.createBindingContext(files: List<KtFile>): BindingCont
     TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
         this.project,
         files,
-        NoScopeRecordCliBindingTrace(),
+        NoScopeRecordCliBindingTrace(this.project),
         this.configuration,
         this::createPackagePartProvider
     ).bindingContext