From 5fb5cea4a32fadb0bab7ca1aac40292b4df167d8 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Mon, 13 Oct 2014 16:45:47 +0400 Subject: [PATCH] JS inline: fixed bug with simple return inline function, when result is not used. When function body can be expression (contains only one statement, and it's return), result was saved to resultExpr, not body. But when result was not used, resultExpr was not used then. Fixed by checking, that result is used in first place. --- .../k2js/inline/FunctionInlineMutator.java | 2 +- .../k2js/test/semantics/InlineTest.java | 4 +++ .../simpleReturnFunctionWithResultUnused.kt | 35 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 js/js.translator/testData/inline/cases/simpleReturnFunctionWithResultUnused.kt diff --git a/js/js.inliner/src/org/jetbrains/k2js/inline/FunctionInlineMutator.java b/js/js.inliner/src/org/jetbrains/k2js/inline/FunctionInlineMutator.java index 387df918e08..215a33fd792 100644 --- a/js/js.inliner/src/org/jetbrains/k2js/inline/FunctionInlineMutator.java +++ b/js/js.inliner/src/org/jetbrains/k2js/inline/FunctionInlineMutator.java @@ -90,7 +90,7 @@ class FunctionInlineMutator { renameLocalNames(namingContext, invokedFunction); removeStatementsAfterTopReturn(); - if (canBeExpression(body)) { + if (isResultNeeded && canBeExpression(body)) { resultExpr = asExpression(body); body.getStatements().clear(); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java index dd2918021e0..d2f9f14b71d 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/InlineTest.java @@ -248,6 +248,10 @@ public final class InlineTest extends SingleFileTranslationTest { public void testIncrementProperty() throws Exception { checkFooBoxIsOkWithInlineDirectives(); } + + public void testSimpleReturnFunctionWithResultUnused() throws Exception { + checkFooBoxIsOkWithInlineDirectives(); + } private void checkFooBoxIsOkWithInlineDirectives() throws Exception { checkFooBoxIsOk(); diff --git a/js/js.translator/testData/inline/cases/simpleReturnFunctionWithResultUnused.kt b/js/js.translator/testData/inline/cases/simpleReturnFunctionWithResultUnused.kt new file mode 100644 index 00000000000..0724b541780 --- /dev/null +++ b/js/js.translator/testData/inline/cases/simpleReturnFunctionWithResultUnused.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package foo + +var flag = false +fun toggle(): Boolean { + flag = !flag + + return flag +} + +inline fun run(noinline f: () -> Int): Int { + return f() +} + +fun box(): String { + run({ toggle(); 4 }) + assertEquals(true, flag) + + return "OK" +} \ No newline at end of file