Unverified Commit dacf6d8d authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: Don't replace constant conditions with sideeffects (#72608)

Closes PACK-3368
Closes #72180

Previously,
```js
if (sideEffectFalse((state += 1))) {
	return
}
```
was replaced with
```js
if ("TURBOPACK compile-time falsy", 0) {
	return
}
```
which discards the side effects of both the arguments, and of the
function itself.
parent 9a591a01
...@@ -2076,18 +2076,15 @@ impl JsValue { ...@@ -2076,18 +2076,15 @@ impl JsValue {
ObjectPart::KeyValue(k, v) => k.has_side_effects() || v.has_side_effects(), ObjectPart::KeyValue(k, v) => k.has_side_effects() || v.has_side_effects(),
ObjectPart::Spread(v) => v.has_side_effects(), ObjectPart::Spread(v) => v.has_side_effects(),
}), }),
JsValue::New(_, callee, args) => { // As function bodies aren't analyzed for side-effects, we have to assume every call can
callee.has_side_effects() || args.iter().any(JsValue::has_side_effects) // have sideeffects as well.
} // Otherwise it would be
JsValue::Call(_, callee, args) => { // `func_body(callee).has_side_effects() ||
callee.has_side_effects() || args.iter().any(JsValue::has_side_effects) // callee.has_side_effects() || args.iter().any(JsValue::has_side_effects`
} JsValue::New(_, _callee, _args) => true,
JsValue::SuperCall(_, args) => args.iter().any(JsValue::has_side_effects), JsValue::Call(_, _callee, _args) => true,
JsValue::MemberCall(_, obj, prop, args) => { JsValue::SuperCall(_, _args) => true,
obj.has_side_effects() JsValue::MemberCall(_, _obj, _prop, _args) => true,
|| prop.has_side_effects()
|| args.iter().any(JsValue::has_side_effects)
}
JsValue::Member(_, obj, prop) => obj.has_side_effects() || prop.has_side_effects(), JsValue::Member(_, obj, prop) => obj.has_side_effects() || prop.has_side_effects(),
JsValue::Function(_, _, _) => false, JsValue::Function(_, _, _) => false,
JsValue::Url(_, _) => false, JsValue::Url(_, _) => false,
......
...@@ -900,6 +900,10 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -900,6 +900,10 @@ pub(crate) async fn analyse_ecmascript_module_internal(
span: _, span: _,
in_try: _, in_try: _,
} => { } => {
// Don't replace condition with it's truth-y value, if it has side effects (e.g.
// function calls)
let condition_has_side_effects = condition.has_side_effects();
let condition = analysis_state let condition = analysis_state
.link_value(condition, ImportAttributes::empty_ref()) .link_value(condition, ImportAttributes::empty_ref())
.await?; .await?;
...@@ -911,10 +915,12 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -911,10 +915,12 @@ pub(crate) async fn analyse_ecmascript_module_internal(
} }
macro_rules! condition { macro_rules! condition {
($expr:expr) => { ($expr:expr) => {
analysis.add_code_gen(ConstantCondition::new( if !condition_has_side_effects {
Value::new($expr), analysis.add_code_gen(ConstantCondition::new(
Vc::cell(condition_ast_path.to_vec()), Value::new($expr),
)); Vc::cell(condition_ast_path.to_vec()),
));
}
}; };
} }
macro_rules! active { macro_rules! active {
......
...@@ -55,9 +55,7 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2) ...@@ -55,9 +55,7 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2)
| undefined | undefined
| ???*0* | ???*0*
| b | b
| null
| pj(a, b, c) | pj(a, b, c)
| b["child"]
| cj(a, b, b["type"], b["pendingProps"], c) | cj(a, b, b["type"], b["pendingProps"], c)
| yj(a, b, c) | yj(a, b, c)
| ej(a, b, c) | ej(a, b, c)
...@@ -117,7 +115,10 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2) ...@@ -117,7 +115,10 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2)
*anonymous function 131212* = (...) => sl(null, a, b, !(1), c) *anonymous function 131212* = (...) => sl(null, a, b, !(1), c)
*anonymous function 131315* = (...) => (a["_reactRootContainer"] ? !(0) : !(1)) *anonymous function 131315* = (...) => (a["_reactRootContainer"] ? ???*0* : !(1))
- *0* !(0)
⚠️ sequence with side effects
⚠️ This value might have side effects
*anonymous function 131389* = (...) => undefined *anonymous function 131389* = (...) => undefined
...@@ -577,14 +578,17 @@ Fh = Uf(Dh) ...@@ -577,14 +578,17 @@ Fh = Uf(Dh)
Fi = (...) => di()["memoizedState"] Fi = (...) => di()["memoizedState"]
Fj = (...) => (undefined | null | (???*0* ? b : null) | ???*1* | b["child"]) Fj = (...) => (undefined | ???*0* | null | (???*1* ? b : null) | b["child"])
- *0* unsupported expression - *0* null
⚠️ This value might have side effects
- *1* b
⚠️ sequence with side effects ⚠️ sequence with side effects
⚠️ This value might have side effects ⚠️ This value might have side effects
- *1* unsupported expression
⚠️ This value might have side effects
Fk = (...) => null Fk = (...) => (???*0* | null)
- *0* null
⚠️ sequence with side effects
⚠️ This value might have side effects
G = (...) => undefined G = (...) => undefined
...@@ -722,7 +726,7 @@ Jh = (...) => undefined ...@@ -722,7 +726,7 @@ Jh = (...) => undefined
Ji = (...) => undefined Ji = (...) => undefined
Jj = (...) => (undefined | ???*0* | null | (???*3* ? ???*4* : null)) Jj = (...) => (undefined | ???*0* | (???*3* ? ???*4* : null) | null)
- *0* (???*1* ? ???*2* : null) - *0* (???*1* ? ???*2* : null)
⚠️ sequence with side effects ⚠️ sequence with side effects
⚠️ This value might have side effects ⚠️ This value might have side effects
...@@ -1289,7 +1293,10 @@ Vk = (...) => undefined ...@@ -1289,7 +1293,10 @@ Vk = (...) => undefined
W = (...) => undefined W = (...) => undefined
Wa = (...) => (!(1) | !(0) | ((a !== c) ? !(0) : !(1))) Wa = (...) => (!(1) | !(0) | ((a !== c) ? ???*0* : !(1)))
- *0* !(0)
⚠️ sequence with side effects
⚠️ This value might have side effects
Wb = (...) => (b["dehydrated"] | null) Wb = (...) => (b["dehydrated"] | null)
...@@ -1373,7 +1380,10 @@ Ya = (...) => A( ...@@ -1373,7 +1380,10 @@ Ya = (...) => A(
- *2* unsupported expression - *2* unsupported expression
⚠️ This value might have side effects ⚠️ This value might have side effects
Yb = (...) => (((b !== a) ? null : a) | a | b | ((c["stateNode"]["current"] === c) ? a : b)) Yb = (...) => (((b !== a) ? null : a) | ???*0* | ((c["stateNode"]["current"] === c) ? a : b))
- *0* a
⚠️ sequence with side effects
⚠️ This value might have side effects
Yc = (...) => ( Yc = (...) => (
| a | a
...@@ -5763,7 +5773,10 @@ kh = (...) => undefined ...@@ -5763,7 +5773,10 @@ kh = (...) => undefined
ki = (...) => c(*anonymous function 67764*) ki = (...) => c(*anonymous function 67764*)
kj = (...) => ($i(a, b, f) | b["child"]) kj = (...) => (???*0* | b["child"])
- *0* $i(a, b, f)
⚠️ sequence with side effects
⚠️ This value might have side effects
kk = (...) => undefined kk = (...) => undefined
...@@ -5990,7 +6003,10 @@ n#292 = ( ...@@ -5990,7 +6003,10 @@ n#292 = (
n#404 = (a | t["payload"]) n#404 = (a | t["payload"])
n#431 = (...) => l n#431 = (...) => (???*0* | l)
- *0* l
⚠️ sequence with side effects
⚠️ This value might have side effects
n#449 = r(e, u, h[w], k) n#449 = r(e, u, h[w], k)
...@@ -6356,7 +6372,10 @@ t#292 = ((0 !== ???*0*) | [] | Bd | Td | new t(x, `${w}enter`, n, c, e) | k | vf ...@@ -6356,7 +6372,10 @@ t#292 = ((0 !== ???*0*) | [] | Bd | Td | new t(x, `${w}enter`, n, c, e) | k | vf
t#404 = h t#404 = h
t#431 = (...) => l t#431 = (...) => (???*0* | l)
- *0* l
⚠️ sequence with side effects
⚠️ This value might have side effects
t#454 = r(e, m, n["value"], k) t#454 = r(e, m, n["value"], k)
......
...@@ -64,9 +64,7 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2) ...@@ -64,9 +64,7 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2)
| undefined | undefined
| ???*0* | ???*0*
| b | b
| null
| pj(a, b, c) | pj(a, b, c)
| b["child"]
| cj(a, b, b["type"], b["pendingProps"], c) | cj(a, b, b["type"], b["pendingProps"], c)
| yj(a, b, c) | yj(a, b, c)
| ej(a, b, c) | ej(a, b, c)
...@@ -126,7 +124,10 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2) ...@@ -126,7 +124,10 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2)
*anonymous function 131212* = (...) => sl(null, a, b, !(1), c) *anonymous function 131212* = (...) => sl(null, a, b, !(1), c)
*anonymous function 131315* = (...) => (a["_reactRootContainer"] ? !(0) : !(1)) *anonymous function 131315* = (...) => (a["_reactRootContainer"] ? ???*0* : !(1))
- *0* !(0)
⚠️ sequence with side effects
⚠️ This value might have side effects
*anonymous function 131389* = (...) => undefined *anonymous function 131389* = (...) => undefined
...@@ -713,14 +714,17 @@ Fh = {"current": {}} ...@@ -713,14 +714,17 @@ Fh = {"current": {}}
Fi = (...) => di()["memoizedState"] Fi = (...) => di()["memoizedState"]
Fj = (...) => (undefined | null | (???*0* ? b : null) | ???*1* | b["child"]) Fj = (...) => (undefined | ???*0* | null | (???*1* ? b : null) | b["child"])
- *0* unsupported expression - *0* null
⚠️ This value might have side effects
- *1* b
⚠️ sequence with side effects ⚠️ sequence with side effects
⚠️ This value might have side effects ⚠️ This value might have side effects
- *1* unsupported expression
⚠️ This value might have side effects
Fk = (...) => null Fk = (...) => (???*0* | null)
- *0* null
⚠️ sequence with side effects
⚠️ This value might have side effects
G = (...) => undefined G = (...) => undefined
...@@ -997,7 +1001,7 @@ Jh = (...) => undefined ...@@ -997,7 +1001,7 @@ Jh = (...) => undefined
Ji = (...) => undefined Ji = (...) => undefined
Jj = (...) => (undefined | ???*0* | null | (???*3* ? ???*4* : null)) Jj = (...) => (undefined | ???*0* | (???*3* ? ???*4* : null) | null)
- *0* (???*1* ? ???*2* : null) - *0* (???*1* ? ???*2* : null)
⚠️ sequence with side effects ⚠️ sequence with side effects
⚠️ This value might have side effects ⚠️ This value might have side effects
...@@ -2161,7 +2165,10 @@ Vk = (...) => undefined ...@@ -2161,7 +2165,10 @@ Vk = (...) => undefined
W = (...) => undefined W = (...) => undefined
Wa = (...) => (!(1) | !(0) | ((a !== c) ? !(0) : !(1))) Wa = (...) => (!(1) | !(0) | ((a !== c) ? ???*0* : !(1)))
- *0* !(0)
⚠️ sequence with side effects
⚠️ This value might have side effects
Wb = (...) => (b["dehydrated"] | null) Wb = (...) => (b["dehydrated"] | null)
...@@ -2202,9 +2209,7 @@ Wk = ( ...@@ -2202,9 +2209,7 @@ Wk = (
| undefined | undefined
| ???*1* | ???*1*
| b | b
| null
| pj(a, b, c) | pj(a, b, c)
| b["child"]
| cj(a, b, b["type"], b["pendingProps"], c) | cj(a, b, b["type"], b["pendingProps"], c)
| yj(a, b, c) | yj(a, b, c)
| ej(a, b, c) | ej(a, b, c)
...@@ -2270,7 +2275,10 @@ Ya = (...) => A( ...@@ -2270,7 +2275,10 @@ Ya = (...) => A(
- *2* unsupported expression - *2* unsupported expression
⚠️ This value might have side effects ⚠️ This value might have side effects
Yb = (...) => (((b !== a) ? null : a) | a | b | ((c["stateNode"]["current"] === c) ? a : b)) Yb = (...) => (((b !== a) ? null : a) | ???*0* | ((c["stateNode"]["current"] === c) ? a : b))
- *0* a
⚠️ sequence with side effects
⚠️ This value might have side effects
Yc = (...) => ( Yc = (...) => (
| a | a
...@@ -5917,14 +5925,24 @@ b#1001 = ???*0* ...@@ -5917,14 +5925,24 @@ b#1001 = ???*0*
- *0* arguments[1] - *0* arguments[1]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
b#1004 = (???*0* | ???*1* | 0 | ???*3*) b#1004 = (???*0* | ???*1* | ???*3* | 0 | ???*6*)
- *0* arguments[1] - *0* arguments[1]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
- *1* ???*2*["name"] - *1* ???*2*["name"]
⚠️ unknown object ⚠️ unknown object
- *2* arguments[2] - *2* arguments[2]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
- *3* updated with update expression - *3* ???*4*["name"]
⚠️ unknown object
⚠️ This value might have side effects
- *4* ???*5*["querySelectorAll"](
`input[name=${FreeVar(JSON)["stringify"](`${b}`)}][type="radio"]`
)
⚠️ unknown callee object
⚠️ This value might have side effects
- *5* c
⚠️ circular variable reference
- *6* updated with update expression
⚠️ This value might have side effects ⚠️ This value might have side effects
b#101 = ???*0* b#101 = ???*0*
...@@ -9026,9 +9044,7 @@ b#868 = ???*0* ...@@ -9026,9 +9044,7 @@ b#868 = ???*0*
| undefined | undefined
| ???*2* | ???*2*
| b | b
| null
| pj(a, b, c) | pj(a, b, c)
| b["child"]
| cj(a, b, b["type"], b["pendingProps"], c) | cj(a, b, b["type"], b["pendingProps"], c)
| yj(a, b, c) | yj(a, b, c)
| ej(a, b, c) | ej(a, b, c)
...@@ -9757,22 +9773,32 @@ c#1001 = ( ...@@ -9757,22 +9773,32 @@ c#1001 = (
- *16* arguments[0] - *16* arguments[0]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
c#1004 = (???*0* | ???*1* | ???*3*) c#1004 = (???*0* | ???*1* | ???*3* | ???*6*)
- *0* arguments[2] - *0* arguments[2]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
- *1* ???*2*["parentNode"] - *1* ???*2*["parentNode"]
⚠️ unknown object ⚠️ unknown object
- *2* arguments[2] - *2* arguments[2]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
- *3* ???*4*["querySelectorAll"](`input[name=${???*5*}][type="radio"]`) - *3* ???*4*["parentNode"]
⚠️ unknown object
⚠️ This value might have side effects
- *4* ???*5*["querySelectorAll"](
`input[name=${FreeVar(JSON)["stringify"](`${b}`)}][type="radio"]`
)
⚠️ unknown callee object ⚠️ unknown callee object
⚠️ This value might have side effects ⚠️ This value might have side effects
- *4* arguments[2] - *5* c
⚠️ circular variable reference
- *6* ???*7*["querySelectorAll"](`input[name=${???*8*}][type="radio"]`)
⚠️ unknown callee object
⚠️ This value might have side effects
- *7* arguments[2]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
- *5* ???*6*["stringify"](b) - *8* ???*9*["stringify"](b)
⚠️ unknown callee object ⚠️ unknown callee object
⚠️ This value might have side effects ⚠️ This value might have side effects
- *6* FreeVar(JSON) - *9* FreeVar(JSON)
⚠️ unknown global ⚠️ unknown global
⚠️ This value might have side effects ⚠️ This value might have side effects
...@@ -12662,7 +12688,7 @@ ck = (...) => undefined ...@@ -12662,7 +12688,7 @@ ck = (...) => undefined
cl = (...) => a cl = (...) => a
d#1004 = ???*0* d#1004 = ???*0*
- *0* ???*1*[(???*2* | ???*3* | 0 | ???*5*)] - *0* ???*1*[(???*2* | ???*3* | ???*5* | 0 | ???*8*)]
⚠️ unknown object ⚠️ unknown object
⚠️ This value might have side effects ⚠️ This value might have side effects
- *1* arguments[2] - *1* arguments[2]
...@@ -12673,7 +12699,17 @@ d#1004 = ???*0* ...@@ -12673,7 +12699,17 @@ d#1004 = ???*0*
⚠️ unknown object ⚠️ unknown object
- *4* arguments[2] - *4* arguments[2]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
- *5* updated with update expression - *5* ???*6*["name"]
⚠️ unknown object
⚠️ This value might have side effects
- *6* ???*7*["querySelectorAll"](
`input[name=${FreeVar(JSON)["stringify"](`${b}`)}][type="radio"]`
)
⚠️ unknown callee object
⚠️ This value might have side effects
- *7* c
⚠️ circular variable reference
- *8* updated with update expression
⚠️ This value might have side effects ⚠️ This value might have side effects
d#1013 = ( d#1013 = (
...@@ -15131,7 +15167,7 @@ e#1004 = (???*0* | null) ...@@ -15131,7 +15167,7 @@ e#1004 = (???*0* | null)
- *0* ???*1*[Pf] - *0* ???*1*[Pf]
⚠️ unknown object ⚠️ unknown object
⚠️ This value might have side effects ⚠️ This value might have side effects
- *1* ???*2*[(???*3* | ???*4* | 0 | ???*6*)] - *1* ???*2*[(???*3* | ???*4* | ???*6* | 0 | ???*9*)]
⚠️ unknown object ⚠️ unknown object
⚠️ This value might have side effects ⚠️ This value might have side effects
- *2* arguments[2] - *2* arguments[2]
...@@ -15142,7 +15178,17 @@ e#1004 = (???*0* | null) ...@@ -15142,7 +15178,17 @@ e#1004 = (???*0* | null)
⚠️ unknown object ⚠️ unknown object
- *5* arguments[2] - *5* arguments[2]
⚠️ function calls are not analysed yet ⚠️ function calls are not analysed yet
- *6* updated with update expression - *6* ???*7*["name"]
⚠️ unknown object
⚠️ This value might have side effects
- *7* ???*8*["querySelectorAll"](
`input[name=${FreeVar(JSON)["stringify"](`${b}`)}][type="radio"]`
)
⚠️ unknown callee object
⚠️ This value might have side effects
- *8* c
⚠️ circular variable reference
- *9* updated with update expression
⚠️ This value might have side effects ⚠️ This value might have side effects
e#1013 = ( e#1013 = (
...@@ -20069,7 +20115,10 @@ kh = (...) => undefined ...@@ -20069,7 +20115,10 @@ kh = (...) => undefined
ki = (...) => c(*anonymous function 67764*) ki = (...) => c(*anonymous function 67764*)
kj = (...) => ($i(a, b, f) | b["child"]) kj = (...) => (???*0* | b["child"])
- *0* $i(a, b, f)
⚠️ sequence with side effects
⚠️ This value might have side effects
kk = (...) => undefined kk = (...) => undefined
...@@ -20516,7 +20565,10 @@ n#404 = ???*0* ...@@ -20516,7 +20565,10 @@ n#404 = ???*0*
- *0* max number of linking steps reached - *0* max number of linking steps reached
⚠️ This value might have side effects ⚠️ This value might have side effects
n#431 = (...) => l n#431 = (...) => (???*0* | l)
- *0* l
⚠️ sequence with side effects
⚠️ This value might have side effects
n#449 = ???*0* n#449 = ???*0*
- *0* max number of linking steps reached - *0* max number of linking steps reached
...@@ -21008,7 +21060,10 @@ t#404 = ???*0* ...@@ -21008,7 +21060,10 @@ t#404 = ???*0*
- *0* max number of linking steps reached - *0* max number of linking steps reached
⚠️ This value might have side effects ⚠️ This value might have side effects
t#431 = (...) => l t#431 = (...) => (???*0* | l)
- *0* l
⚠️ sequence with side effects
⚠️ This value might have side effects
t#454 = ???*0* t#454 = ???*0*
- *0* max number of linking steps reached - *0* max number of linking steps reached
......
...@@ -172,3 +172,27 @@ it('should analyze numeric additions',()=>{ ...@@ -172,3 +172,27 @@ it('should analyze numeric additions',()=>{
require("fail"); require("fail");
} }
}) })
it('should consider side-effects of conditions', () => {
let sideEffects = 0;
function sideEffectFalse(value) {
sideEffects += value;
return false
}
function calculate() {
var state = 0
if (sideEffectFalse((state += 1))) {
state += 10
return state
}
if (sideEffectFalse(state)) {
state += 10
return state
}
return state
}
expect(calculate()).toBe(1)
expect(sideEffects).toBe(2);
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment