From 8599ac065fd477b20952e8685263c57e8433e0e7 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Wed, 17 May 2017 18:32:06 +0300 Subject: [PATCH] Fixed order of arguments for inline function Default arguments are evaluated inside callee as opposed to other arguments computed at callsite. --- .../kotlin/backend/konan/lower/FunctionInlining.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt index f3cb9a48945..6e35043f543 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt @@ -247,7 +247,7 @@ private class Inliner(val currentScope: ScopeWithIr, val context: Context) { private fun buildParameterToArgument(irCall : IrCall, // Call site. irFunction: IrFunction // Function to be called. - ): MutableList { + ): List { val parameterToArgument = mutableListOf() // Result list. val functionDescriptor = irFunction.descriptor.original // Descriptor of function to be called. @@ -277,6 +277,7 @@ private class Inliner(val currentScope: ScopeWithIr, val context: Context) { valueArguments.add(0, irCall.extensionReceiver!!) } + val parametersWithDefaultToArgument = mutableListOf() functionDescriptor.valueParameters.forEach { parameterDescriptor -> // Iterate value parameter descriptors. val argument = valueArguments[parameterDescriptor.index] // Get appropriate argument from call site. when { @@ -289,7 +290,7 @@ private class Inliner(val currentScope: ScopeWithIr, val context: Context) { parameterDescriptor.hasDefaultValue() -> { // There is no argument - try default value. val defaultArgument = irFunction.getDefault(parameterDescriptor)!! - parameterToArgument += ParameterToArgument( + parametersWithDefaultToArgument += ParameterToArgument( parameterDescriptor = parameterDescriptor, argumentExpression = defaultArgument.expression ) @@ -315,7 +316,8 @@ private class Inliner(val currentScope: ScopeWithIr, val context: Context) { } } } - return parameterToArgument + return parameterToArgument + parametersWithDefaultToArgument // All arguments except default are evaluated at callsite, + // but default arguments are evaluated inside callee. } //-------------------------------------------------------------------------//