JS/Inlining: minor improvements and clarifications after code review. Test whether expression without side effect does not prevent to relocate another expressions

This commit is contained in:
Alexey Andreev
2016-06-06 12:37:37 +03:00
parent 6b99d4e1c5
commit 07d5a4506c
10 changed files with 63 additions and 23 deletions
@@ -77,6 +77,9 @@ fun VariableAccessInfo.constructAccessExpression(ref: JsExpression): JsExpressio
return ref
} else {
return if (value !is JsEmptyExpression) {
// This is useful when passing AST to TemporaryAssignmentElimination. It can bring
// property assignment like `obj.propertyName = $tmp` to places where `$tmp` gets its value,
// but only when it's sure that no side effects possible.
(ref as? HasMetadata)?.let { it.sideEffects = SideEffectKind.PURE }
JsAstUtils.assignment(ref, value!!)
}
@@ -75,7 +75,7 @@ object DefaultVariableAccessCase : VariableAccessCase() {
override fun VariableAccessInfo.dispatchReceiver(): JsExpression {
val accessor = JsNameRef(variableName, dispatchReceiver!!)
val descriptor = callableDescriptor
if (descriptor is PropertyDescriptor && !JsDescriptorUtils.sideEffectsPossible(descriptor)) {
if (descriptor is PropertyDescriptor && !JsDescriptorUtils.sideEffectsPossibleOnRead(descriptor)) {
accessor.sideEffects = SideEffectKind.DEPENDS_ON_STATE
}
return constructAccessExpression(accessor)
@@ -147,7 +147,7 @@ public final class JsDescriptorUtils {
return accessorDescriptor == null || accessorDescriptor.isDefault();
}
public static boolean sideEffectsPossible(@NotNull PropertyDescriptor property) {
public static boolean sideEffectsPossibleOnRead(@NotNull PropertyDescriptor property) {
return !isDefaultAccessor(property.getGetter()) || ModalityKt.isOverridableOrOverrides(property) ||
isStaticInitializationPossible(property);
}
@@ -0,0 +1,29 @@
// CHECK_VARS_COUNT: function=box count=1
package foo
var log = ""
class A() {
var x = 23
}
fun sideEffect(): Int {
log += "sideEffect();"
return 42
}
fun bar(a: Int, b: Int) {
log += "bar($a, $b);"
}
inline fun test(a: Int, b: Int) {
bar(b, a)
}
fun box(): String {
val a = A()
test(sideEffect(), a.x)
assertEquals("sideEffect();bar(23, 42);", log)
return "OK"
}
@@ -4,18 +4,18 @@ function f(x) {
function box() {
var result1, result2, $a, $b;
if (f(true)) {
$a = "1";
}
result1 = $a;
if (result1 !== "1") return "fail1: " + result1;
if (f(false)) {
$b = "1";
}
result2 = $b;
if (result2 !== void 0) return "fail2: " + result2;
return "OK";
}