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.
This commit is contained in:
Alexey Tsvetkov
2014-10-13 16:45:47 +04:00
parent be82d4c661
commit 5fb5cea4a3
3 changed files with 40 additions and 1 deletions
@@ -90,7 +90,7 @@ class FunctionInlineMutator {
renameLocalNames(namingContext, invokedFunction);
removeStatementsAfterTopReturn();
if (canBeExpression(body)) {
if (isResultNeeded && canBeExpression(body)) {
resultExpr = asExpression(body);
body.getStatements().clear();
@@ -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();
@@ -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"
}