From c195ade201ed82d0b7c10ee9029e1b44e7eac9a9 Mon Sep 17 00:00:00 2001 From: SvyatoslavScherbina Date: Tue, 7 Jul 2020 18:19:14 +0700 Subject: [PATCH] Fix KT-39548: workaround MinGW bug with .ctors.65534 section (#4249) --- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 14 ++++++- backend.native/tests/build.gradle | 26 +++++++++++++ .../org/jetbrains/kotlin/genTestKT39548.kt | 37 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 build-tools/src/main/kotlin/org/jetbrains/kotlin/genTestKT39548.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 64ea98e9be0..8e8a1ed741b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -27,6 +27,7 @@ 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.konan.target.CompilerOutputKind +import org.jetbrains.kotlin.konan.target.Family import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.descriptorUtil.classId @@ -2353,7 +2354,18 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map + // Test infrastructure doesn't support generated source; + // workaround by specifying dummy source: + task.source = "does/not/exist/kt39548.dummy.kt" + task.enabled = isWindowsTarget(project) + + if (task.enabled) { + konanArtifacts { + program(name, targets: [target.name]) { + baseDir "$testOutputLocal/$name" + srcFiles "$buildDir/kt39548/kt39548.kt" // Generated by genKt39548 task. + extraOpts task.flags + extraOpts project.globalTestArgs + } + } + + UtilsKt.findKonanBuildTask(project, name, target).dependsOn(genKt39548) + } +} + linkTest("deserialized_members") { goldValue= "first level\n"+ diff --git a/build-tools/src/main/kotlin/org/jetbrains/kotlin/genTestKT39548.kt b/build-tools/src/main/kotlin/org/jetbrains/kotlin/genTestKT39548.kt new file mode 100644 index 00000000000..f8a7e3298e1 --- /dev/null +++ b/build-tools/src/main/kotlin/org/jetbrains/kotlin/genTestKT39548.kt @@ -0,0 +1,37 @@ +package org.jetbrains.kotlin + +import java.io.File + +fun genTestKT39548(file: File) { + val longName = StringBuilder().apply { + repeat(10_000_000) { + append('a') + } + } + + val text = """ + import kotlin.test.* + + fun $longName(): Int = 42 + fun same(value: T): T = value + val globalInt1: Int = same(1) + val globalStringA: String = same("a") + @ThreadLocal val threadLocalInt2: Int = same(2) + @ThreadLocal val threadLocalStringB: String = same("b") + + fun main() { + // Ensure function don't get DCEd: + val resultOfFunctionWithLongName = $longName() + assertEquals(42, resultOfFunctionWithLongName) + + // Check that top-level initializers did run as expected: + assertEquals(1, globalInt1) + assertEquals("a", globalStringA) + assertEquals(2, threadLocalInt2) + assertEquals("b", threadLocalStringB) + } + """.trimIndent() + + file.parentFile.mkdirs() + file.writeText(text) +}