Skip to content

Commit 6c2c458

Browse files
ligeeSpace Team
authored andcommitted
Scripting: fix jvmTarget processing from script annotations
#KT-87076 fixed
1 parent 12fc2f6 commit 6c2c458

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,51 @@ class MainKtsIT {
229229
)
230230
}
231231
}
232+
233+
@Test
234+
fun testWithDifferrentJvmTarget() {
235+
val jvmTarget = System.getProperty("java.runtime.version")?.substringBefore(".")
236+
if (jvmTarget?.toIntOrNull()?.let { it >= 9 } != true) return
237+
withTempDir("main.kts.jvmtarget") { jardir ->
238+
val libsrc = jardir.resolve("src.kt").apply {
239+
writeText(
240+
"""
241+
fun testFun() = "hello"
242+
inline fun <T> runInline(block: () -> T): T = block()
243+
""".trimIndent())
244+
}
245+
runWithKotlinc(
246+
arrayOf("-jvm-target", jvmTarget, "-d", jardir.resolve("lib.jar").absolutePath.toString(), libsrc.absolutePath)
247+
)
248+
fun makeScript(jvmTarget: String): String = """
249+
@file:CompilerOptions("-jvm-target", "$jvmTarget")
250+
@file:DependsOn("${jardir.resolve("lib.jar").absoluteFile.platformIndependentPathString()}")
251+
println(runInline(::testFun))
252+
""".trimIndent()
253+
254+
val scrErr = jardir.resolve("serr.main.kts").apply { writeText(makeScript("1.8")) }
255+
val scrOk = jardir.resolve("sok.main.kts").apply { writeText(makeScript(jvmTarget)) }
256+
257+
val mainKtsJar = File("dist/kotlinc/lib/kotlin-main-kts.jar")
258+
259+
runWithK2JVMCompiler(
260+
scrErr.absolutePath,
261+
expectedExitCode = 1,
262+
expectedSomeErrPatterns = listOf(".*cannot inline bytecode built with JVM target $jvmTarget.*"),
263+
classpath = listOf(mainKtsJar)
264+
)
265+
runWithK2JVMCompiler(
266+
scrOk.absolutePath,
267+
listOf("hello"),
268+
classpath = listOf(mainKtsJar)
269+
)
270+
runWithKotlincAndMainKts(
271+
scrOk.absolutePath,
272+
listOf("hello"),
273+
)
274+
}
275+
}
276+
232277
}
233278

234279
private fun File.platformIndependentPathString(): String = path.replace(File.separatorChar, '/')

plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/K2ScriptingCompilerEnvironment.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ internal interface K2ScriptingCompilerEnvironmentInternal : K2ScriptingCompilerE
6262
val compilerContext: SharedScriptCompilationContext
6363
val packagePartProvider: PackagePartProvider
6464
val sessionFactoryContext: FirJvmSessionFactory.Context
65+
fun updateContext(configuration: CompilerConfiguration)
6566
}
6667

6768
internal open class K2ScriptingCompilerEnvironmentImpl(
@@ -76,8 +77,18 @@ internal open class K2ScriptingCompilerEnvironmentImpl(
7677
override val extensionRegistrars: List<FirExtensionRegistrar>,
7778
override val sharedLibrarySession: FirSession,
7879
override var dummySessionForAnnotationResolution: FirSession?,
79-
override val sessionFactoryContext: FirJvmSessionFactory.Context
80-
) : K2ScriptingCompilerEnvironmentInternal
80+
override var sessionFactoryContext: FirJvmSessionFactory.Context
81+
) : K2ScriptingCompilerEnvironmentInternal {
82+
83+
override fun updateContext(configuration: CompilerConfiguration) {
84+
val previous = sessionFactoryContext
85+
sessionFactoryContext = FirJvmSessionFactory.Context(
86+
configuration = configuration,
87+
projectEnvironment = previous.projectEnvironment,
88+
librariesScope = previous.librariesScope,
89+
)
90+
}
91+
}
8192

8293
open class ScriptingModuleDataProvider(private val baseName: String, baseLibraryPaths: List<Path>) : ModuleDataProvider() {
8394

plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmK2CompilerImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class ScriptJvmK2CompilerImpl(
203203
ignoredOptionsReportingState,
204204
true
205205
)
206+
state.updateContext(compilerConfiguration)
206207
}
207208

208209
if (reportingCtx.messageCollector.hasErrors()) return failure(reportingCtx.diagnosticsCollector)

plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ fun runWithK2JVMCompiler(
170170
scriptPath: String,
171171
expectedOutPatterns: List<String> = emptyList(),
172172
expectedExitCode: Int = 0,
173+
expectedSomeErrPatterns: List<String>? = null,
173174
classpath: List<File> = emptyList(),
174175
skipScriptArgument: Boolean = false,
175176
disableScriptCompilationCache: Boolean = true,
@@ -190,7 +191,7 @@ fun runWithK2JVMCompiler(
190191
}
191192
add(scriptPath)
192193
}
193-
runWithK2JVMCompiler(args.toTypedArray(), expectedOutPatterns, expectedExitCode)
194+
runWithK2JVMCompiler(args.toTypedArray(), expectedOutPatterns, expectedExitCode, expectedSomeErrPatterns = expectedSomeErrPatterns)
194195
}
195196

196197
fun runWithK2JVMCompiler(

0 commit comments

Comments
 (0)