JS: when both clauses of if become empty during optimization, remove if entirely. Make condition and then clause of JsIf non-nullable. Fix #KT-13912
This commit is contained in:
committed by
Alexey Andreev
parent
b5358122e2
commit
d2fdc7ffc0
+3
@@ -285,6 +285,9 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
||||
return new JsConditional(testExpression, jsThenExpression, jsElseExpression).source(expression);
|
||||
}
|
||||
}
|
||||
if (thenStatement == null) {
|
||||
thenStatement = JsEmpty.INSTANCE;
|
||||
}
|
||||
JsIf ifStatement = new JsIf(testExpression, thenStatement, elseStatement);
|
||||
return ifStatement.source(expression);
|
||||
}
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
var global = "";
|
||||
|
||||
function id(value) {
|
||||
global += value + ";";
|
||||
return value;
|
||||
}
|
||||
|
||||
function test(x) {
|
||||
id(x);
|
||||
id(x + 1);
|
||||
}
|
||||
|
||||
function box() {
|
||||
test(23);
|
||||
if (global != "23;24;") return "fail";
|
||||
return "OK";
|
||||
}
|
||||
js/js.translator/testData/js-optimizer/redundant-label-removal/emptyIfConditionPreserved.original.js
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
var global = "";
|
||||
|
||||
function id(value) {
|
||||
global += value + ";";
|
||||
return value;
|
||||
}
|
||||
|
||||
function test(x) {
|
||||
$outer: {
|
||||
if (id(x) + id(x + 1) > 0) {
|
||||
break $outer;
|
||||
}
|
||||
else {
|
||||
break $outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function box() {
|
||||
test(23);
|
||||
if (global != "23;24;") return "fail";
|
||||
return "OK";
|
||||
}
|
||||
js/js.translator/testData/js-optimizer/redundant-label-removal/ifWithEmptyThenAndNoElse.optimized.js
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
function test(x) {
|
||||
return "OK";
|
||||
}
|
||||
|
||||
function box() {
|
||||
return test(23);
|
||||
}
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
function test(x) {
|
||||
$outer: {
|
||||
if (x > 10) {
|
||||
break $outer;
|
||||
}
|
||||
}
|
||||
return "OK";
|
||||
}
|
||||
|
||||
function box() {
|
||||
return test(23);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
fun test1(): String {
|
||||
run {
|
||||
if (false) {
|
||||
}
|
||||
}
|
||||
return "O"
|
||||
}
|
||||
|
||||
fun test2(): String {
|
||||
1.let {
|
||||
if (false) {
|
||||
}
|
||||
}
|
||||
return "K"
|
||||
}
|
||||
|
||||
fun box() = test1() + test2()
|
||||
Reference in New Issue
Block a user