Disable some simplifier special cases when isCondition is true.

Summary:
Release note: none

If the x and y part of a c ? x : y are simplified under the assumption that they will only be used contexts where they are converted to Booleans, then it is not safe to use them in equality comparisons. Hence the simplification rules that do so must be disabled in this situation.
Closes https://github.com/facebook/prepack/pull/1698

Differential Revision: D7511274

Pulled By: hermanventer

fbshipit-source-id: f0329c00523cb3c06de9e0dde3ab55104dbb07c7
This commit is contained in:
Herman Venter 2018-04-04 19:42:50 -07:00 committed by Facebook Github Bot
parent e549463ce9
commit 6f62fa6f51
2 changed files with 20 additions and 4 deletions

View File

@ -152,10 +152,12 @@ function simplify(realm, value: Value, isCondition: boolean = false): Value {
if (!notc.mightNotBeFalse()) return x;
invariant(notc instanceof AbstractValue);
if (Path.implies(notc)) return y;
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "===", value, x))) return x;
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "!==", value, x))) return y;
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "!==", value, y))) return x;
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "===", value, y))) return y;
if (!isCondition) {
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "===", value, x))) return x;
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "!==", value, x))) return y;
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "!==", value, y))) return x;
if (Path.implies(AbstractValue.createFromBinaryOp(realm, "===", value, y))) return y;
}
// c ? x : x <=> x
if (x.equals(y)) return x;
// x ? x : y <=> x || y

View File

@ -0,0 +1,14 @@
let ob = global.__abstract ? __makeSimple(__abstract("object", "({a: 1})")) : {a : 1};
let a = ob.a;
let _1aw_ = !a;
let _1bx_ = _1aw_ ? null : ob;
let _1bw_ = !_1bx_;
if (!_1bw_) {
var y = _1bw_ ? 123 : 456;
}
inspect = function() { return y; }