From da3cc22ce8a3c39dfed6b95ab6f4b2fde52c17e2 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Fri, 20 Apr 2018 13:32:13 +0300 Subject: [PATCH] Fixed bug in DefaultArgs lowering with vararg arguments + tests --- .../kotlin/backend/konan/KonanPhases.kt | 4 ++-- .../lower/DefaultArgumentsStubGenerator.kt | 6 +++--- backend.native/tests/build.gradle | 10 +++++++++ .../codegen/function/defaultsWithVarArg1.kt | 21 +++++++++++++++++++ .../codegen/function/defaultsWithVarArg2.kt | 12 +++++++++++ 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 backend.native/tests/codegen/function/defaultsWithVarArg1.kt create mode 100644 backend.native/tests/codegen/function/defaultsWithVarArg2.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt index 6b9b22da8fb..230dc686121 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt @@ -45,11 +45,11 @@ enum class KonanPhase(val description: String, /* ... ... */ LOWER_CALLABLES("Callable references Lowering", LOWER_DELEGATION), /* ... ... */ LOWER_LOCAL_FUNCTIONS("Local Function Lowering", LOWER_SHARED_VARIABLES, LOWER_CALLABLES), /* ... ... */ LOWER_INTEROP_PART2("Interop lowering, part 2", LOWER_LOCAL_FUNCTIONS), - /* ... ... */ LOWER_VARARG("Vararg lowering", LOWER_CALLABLES), - /* ... ... */ LOWER_COMPILE_TIME_EVAL("Compile time evaluation lowering", LOWER_VARARG), /* ... ... */ LOWER_TAILREC("tailrec lowering", LOWER_LOCAL_FUNCTIONS), /* ... ... */ LOWER_FINALLY("Finally blocks lowering", LOWER_INITIALIZERS, LOWER_LOCAL_FUNCTIONS, LOWER_TAILREC), /* ... ... */ LOWER_DEFAULT_PARAMETER_EXTENT("Default Parameter Extent Lowering", LOWER_TAILREC, LOWER_ENUMS), + /* ... ... */ LOWER_VARARG("Vararg lowering", LOWER_CALLABLES, LOWER_DEFAULT_PARAMETER_EXTENT), + /* ... ... */ LOWER_COMPILE_TIME_EVAL("Compile time evaluation lowering", LOWER_VARARG), /* ... ... */ LOWER_INNER_CLASSES("Inner classes lowering", LOWER_DEFAULT_PARAMETER_EXTENT), /* ... ... */ LOWER_BUILTIN_OPERATORS("BuiltIn Operators Lowering", LOWER_DEFAULT_PARAMETER_EXTENT), /* ... ... */ LOWER_COROUTINES("Coroutines lowering", LOWER_LOCAL_FUNCTIONS), diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentsStubGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentsStubGenerator.kt index 9be7a43b331..c337dd1c288 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentsStubGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DefaultArgumentsStubGenerator.kt @@ -45,9 +45,9 @@ import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue +import org.jetbrains.kotlin.resolve.calls.components.isVararg import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.builtIns @@ -311,8 +311,8 @@ class DefaultParameterInjector constructor(val context: CommonBackendContext): B maskValues[maskIndex] = maskValues[maskIndex] or (1 shl (i % 32)) } val valueParameterDescriptor = realDescriptor.valueParameters[i] - val pair = valueParameterDescriptor to (valueArgument ?: nullConst(expression, valueParameterDescriptor.type)) - return@mapIndexed pair + val defaultValueArgument = if (valueParameterDescriptor.isVararg) null else nullConst(expression, valueParameterDescriptor.type) + valueParameterDescriptor to (valueArgument ?: defaultValueArgument) }) maskValues.forEachIndexed { i, maskValue -> params += maskParameterDescriptor(realFunction, i) to IrConstImpl.int( diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 706cc81b0bf..21f36d02981 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -362,6 +362,16 @@ task function_defaults_from_fake_override(type: RunKonanTest) { source = "codegen/function/defaultsFromFakeOverride.kt" } +task function_defaults_with_vararg1(type: RunKonanTest) { + goldValue = "Hello , Correct!\nHello World, Correct!\n , Correct!\n" + source = "codegen/function/defaultsWithVarArg1.kt" +} + +task function_defaults_with_vararg2(type: RunKonanTest) { + goldValue = "1\n2\n42\n" + source = "codegen/function/defaultsWithVarArg2.kt" +} + task sum_3const(type: RunKonanTest) { source = "codegen/function/sum_3const.kt" } diff --git a/backend.native/tests/codegen/function/defaultsWithVarArg1.kt b/backend.native/tests/codegen/function/defaultsWithVarArg1.kt new file mode 100644 index 00000000000..020f0f46c53 --- /dev/null +++ b/backend.native/tests/codegen/function/defaultsWithVarArg1.kt @@ -0,0 +1,21 @@ +package codegen.function.defaultsWithVarArg1 + +import kotlin.test.* + +fun foo(s: String = "", vararg args: Any) { + if (args == null) { + println("Failed!") + } else { + print("$s ") + args.forEach { + print("$it") + } + println(", Correct!") + } +} + +@Test fun runTest() { + foo("Hello") + foo("Hello", "World") + foo() +} \ No newline at end of file diff --git a/backend.native/tests/codegen/function/defaultsWithVarArg2.kt b/backend.native/tests/codegen/function/defaultsWithVarArg2.kt new file mode 100644 index 00000000000..4b51b1f8c4e --- /dev/null +++ b/backend.native/tests/codegen/function/defaultsWithVarArg2.kt @@ -0,0 +1,12 @@ +package codegen.function.defaultsWithVarArg2 + +import kotlin.test.* + +fun foo(vararg arr: Int = intArrayOf(1, 2)) { + arr.forEach { println(it) } +} + +@Test fun runTest() { + foo() + foo(42) +} \ No newline at end of file