Fix IR compilation for empty script
#KT-46646 fixed
This commit is contained in:
committed by
TeamCityServer
parent
3a0e3798ec
commit
d365d7c784
@@ -49,20 +49,17 @@ class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationG
|
||||
|
||||
val importedScripts = descriptor.implicitReceivers.filterIsInstanceTo(HashSet<ScriptDescriptor>())
|
||||
|
||||
val startOffset = ktScript.pureStartOffset
|
||||
val endOffset = ktScript.pureEndOffset
|
||||
|
||||
fun makeParameter(descriptor: ParameterDescriptor, origin: IrDeclarationOrigin, index: Int = -1): IrValueParameter {
|
||||
val type = descriptor.type.toIrType()
|
||||
val varargElementType = descriptor.varargElementType?.toIrType()
|
||||
return context.symbolTable.declareValueParameter(
|
||||
startOffset, endOffset,
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
origin,
|
||||
descriptor,
|
||||
type
|
||||
) { symbol ->
|
||||
context.irFactory.createValueParameter(
|
||||
startOffset, endOffset,
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
origin, symbol, context.symbolTable.nameProvider.nameForDeclaration(descriptor),
|
||||
if (index != -1) index else descriptor.indexOrMinusOne,
|
||||
type, varargElementType,
|
||||
@@ -102,7 +99,8 @@ class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationG
|
||||
|
||||
irScript.explicitCallParameters = descriptor.explicitConstructorParameters.map { valueParameterDescriptor ->
|
||||
context.irFactory.createValueParameter(
|
||||
startOffset, endOffset, IrDeclarationOrigin.SCRIPT_CALL_PARAMETER, IrValueParameterSymbolImpl(),
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
IrDeclarationOrigin.SCRIPT_CALL_PARAMETER, IrValueParameterSymbolImpl(),
|
||||
valueParameterDescriptor.name, parametersIndex++,
|
||||
valueParameterDescriptor.type.toIrType(), valueParameterDescriptor.varargElementType?.toIrType(),
|
||||
valueParameterDescriptor.isCrossinline, valueParameterDescriptor.isNoinline,
|
||||
@@ -120,10 +118,12 @@ class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationG
|
||||
// TODO: do not keep direct links
|
||||
val type = providedProperty.type.toIrType()
|
||||
val valueParameter = context.symbolTable.declareValueParameter(
|
||||
startOffset, endOffset, IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, parameter, type
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, parameter, type
|
||||
) { symbol ->
|
||||
context.irFactory.createValueParameter(
|
||||
startOffset, endOffset, IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, symbol, descriptor.name,
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, symbol, descriptor.name,
|
||||
parametersIndex, type, null, isCrossinline = false, isNoinline = false, isHidden = false, isAssignable = false
|
||||
).also { it.parent = irScript }
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ class PsiIrFileEntry(val psiFile: PsiFile) : IrFileEntry {
|
||||
override fun getColumnNumber(offset: Int): Int {
|
||||
if (offset < 0) return -1
|
||||
val lineNumber = getLineNumber(offset)
|
||||
if (lineNumber < 0) return -1
|
||||
return offset - lineStartOffsets[lineNumber]
|
||||
}
|
||||
|
||||
|
||||
+22
-1
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.test.TestJdkKind
|
||||
import org.jetbrains.kotlin.utils.PathUtil
|
||||
import org.junit.Assert
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.script.experimental.annotations.KotlinScript
|
||||
import kotlin.script.experimental.api.*
|
||||
@@ -45,6 +46,19 @@ class ScriptCliCompilationTest : TestCase() {
|
||||
Assert.assertEquals("Hello from basic script!", out)
|
||||
}
|
||||
|
||||
fun testEmptyScript() {
|
||||
val emptyFile = Files.createTempFile("empty",".kts").toFile()
|
||||
try {
|
||||
Assert.assertTrue(
|
||||
"Script file is not empty",
|
||||
emptyFile.exists() && emptyFile.isFile && emptyFile.length() == 0L
|
||||
)
|
||||
checkRun(emptyFile)
|
||||
} finally {
|
||||
emptyFile.delete()
|
||||
}
|
||||
}
|
||||
|
||||
fun testSimpleScriptWithArgs() {
|
||||
val out = checkRun("hello_args.kts", listOf("kotlin"))
|
||||
Assert.assertEquals("Hello, kotlin!", out)
|
||||
@@ -92,9 +106,16 @@ class ScriptCliCompilationTest : TestCase() {
|
||||
args: List<String> = emptyList(),
|
||||
scriptDef: KClass<*>? = null,
|
||||
classpath: List<File> = emptyList()
|
||||
): String = checkRun(File(testDataPath, scriptFileName), args, scriptDef, classpath)
|
||||
|
||||
private fun checkRun(
|
||||
scriptFile: File,
|
||||
args: List<String> = emptyList(),
|
||||
scriptDef: KClass<*>? = null,
|
||||
classpath: List<File> = emptyList()
|
||||
): String =
|
||||
captureOut {
|
||||
val res = runCompiler(File(testDataPath, scriptFileName), args, scriptDef, classpath)
|
||||
val res = runCompiler(scriptFile, args, scriptDef, classpath)
|
||||
val resMessage = lazy {
|
||||
"Compilation results:\n" + res.second.toString()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user