Skip to content

Commit 6f3d4f5

Browse files
committed
Adjust Gradle configuration to gradually introduce warnings as errors in a safe manner
* Also, add tests that verify our disabled assertions * Fix nullability in AgentPremain that used to work by accident (because we disabled those assertions)
1 parent 4a5892f commit 6f3d4f5

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

build.gradle

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,7 @@ configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != core
168168
apply plugin: "bom-conventions"
169169

170170
// Configure subprojects with Kotlin sources
171-
configure(subprojects.findAll { !sourceless.contains(it.name) }) {
172-
// Use atomicfu plugin, it also adds all the necessary dependencies
173-
apply plugin: 'kotlinx-atomicfu'
174-
175-
// Configure options for all Kotlin compilation tasks
176-
tasks.withType(AbstractKotlinCompile).all {
177-
kotlinOptions.freeCompilerArgs += OptInPreset.optInAnnotations.collect { "-Xopt-in=" + it }
178-
kotlinOptions.freeCompilerArgs += "-progressive"
179-
// Remove null assertions to get smaller bytecode on Android
180-
kotlinOptions.freeCompilerArgs += ["-Xno-param-assertions", "-Xno-receiver-assertions", "-Xno-call-assertions"]
181-
}
182-
}
171+
apply plugin: "configure-compilation-conventions"
183172

184173
if (build_snapshot_train) {
185174
println "Hacking test tasks, removing stress and flaky tests"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import org.jetbrains.kotlin.gradle.tasks.*
6+
7+
configure(subprojects) {
8+
if (name in sourceless) return@configure
9+
apply(plugin = "kotlinx-atomicfu")
10+
tasks.withType(KotlinCompile::class).all {
11+
kotlinOptions {
12+
val newOptions =
13+
listOf(
14+
"-progressive", "-Xno-param-assertions", "-Xno-receiver-assertions",
15+
"-Xno-call-assertions"
16+
) + optInAnnotations.map { "-Xopt-in=$it" }
17+
freeCompilerArgs = freeCompilerArgs + newOptions
18+
}
19+
}
20+
}

kotlinx-coroutines-core/jvm/src/debug/AgentPremain.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ internal object AgentPremain {
3636

3737
internal object DebugProbesTransformer : ClassFileTransformer {
3838
override fun transform(
39-
loader: ClassLoader,
39+
loader: ClassLoader?,
4040
className: String,
4141
classBeingRedefined: Class<*>?,
4242
protectionDomain: ProtectionDomain,
4343
classfileBuffer: ByteArray?
4444
): ByteArray? {
45-
if (className != "kotlin/coroutines/jvm/internal/DebugProbesKt") {
45+
if (loader == null || className != "kotlin/coroutines/jvm/internal/DebugProbesKt") {
4646
return null
4747
}
4848
/*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
package kotlinx.coroutines
5+
6+
import kotlinx.coroutines.*
7+
import org.junit.Test
8+
import kotlin.test.*
9+
10+
11+
class NoParamAssertionsTest : TestBase() {
12+
// These tests verify that we haven't omitted "-Xno-param-assertions" and "-Xno-receiver-assertions"
13+
14+
@Test
15+
fun testNoReceiverAssertion() {
16+
val function: (ThreadLocal<Int>, Int) -> ThreadContextElement<Int> = ThreadLocal<Int>::asContextElement
17+
@Suppress("UNCHECKED_CAST")
18+
val unsafeCasted = function as ((ThreadLocal<Int>?, Int) -> ThreadContextElement<Int>)
19+
unsafeCasted(null, 42)
20+
}
21+
22+
@Test
23+
fun testNoParamAssertion() {
24+
val function: (ThreadLocal<Any>, Any) -> ThreadContextElement<Any> = ThreadLocal<Any>::asContextElement
25+
@Suppress("UNCHECKED_CAST")
26+
val unsafeCasted = function as ((ThreadLocal<Any?>?, Any?) -> ThreadContextElement<Any>)
27+
unsafeCasted(ThreadLocal.withInitial { Any() }, null)
28+
}
29+
}

0 commit comments

Comments
 (0)