JS backend: fix KT-6037 (default arguments in simple function with expression as body)
#KT-6037 Fixed
This commit is contained in:
@@ -71,4 +71,8 @@ public class DefaultArgumentsTest extends SingleFileTranslationTest {
|
|||||||
public void testComplexExpressionAsDefaultArgument() throws Exception {
|
public void testComplexExpressionAsDefaultArgument() throws Exception {
|
||||||
checkFooBoxIsOk();
|
checkFooBoxIsOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDefaultArgumentsInFunctionWithExpressionAsBody() throws Exception {
|
||||||
|
checkFooBoxIsOk();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ public final class FunctionBodyTranslator extends AbstractTranslator {
|
|||||||
JetExpression jetBodyExpression = declaration.getBodyExpression();
|
JetExpression jetBodyExpression = declaration.getBodyExpression();
|
||||||
assert jetBodyExpression != null : "Cannot translate a body of an abstract function.";
|
assert jetBodyExpression != null : "Cannot translate a body of an abstract function.";
|
||||||
JsBlock jsBlock = new JsBlock(setDefaultValueForArguments(descriptor, context()));
|
JsBlock jsBlock = new JsBlock(setDefaultValueForArguments(descriptor, context()));
|
||||||
jsBlock.getStatements().addAll(mayBeWrapWithReturn(Translation.translateExpression(jetBodyExpression, context())).getStatements());
|
jsBlock.getStatements().addAll(mayBeWrapWithReturn(Translation.translateExpression(jetBodyExpression, context(), jsBlock)).getStatements());
|
||||||
return jsBlock;
|
return jsBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
// KT-6037: KT-6037 Javascript default function arguments fill code generated in wrong order on method without "return keyword"
|
||||||
|
package foo
|
||||||
|
|
||||||
|
inline fun id<T>(x: T) = x
|
||||||
|
|
||||||
|
fun test(arg: Int = 10) = id(arg)
|
||||||
|
|
||||||
|
fun foo(value: String = "K") = "O" + try { value } catch(e: Exception) { "..." }
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
|
||||||
|
assertEquals(10, test())
|
||||||
|
assertEquals(100, test(100))
|
||||||
|
assertEquals("OK", foo())
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user