Unverified Commit 31fb9aa8 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Fix iterator side effects in spread expressions

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 4e28b0ee
...@@ -961,7 +961,7 @@ impl<'a> Visit for SideEffectVisitor<'a> { ...@@ -961,7 +961,7 @@ impl<'a> Visit for SideEffectVisitor<'a> {
class_expr.class.visit_with(self); class_expr.class.visit_with(self);
} }
Expr::Array(arr) => { Expr::Array(arr) => {
// Arrays are pure if their elements are pure // Array elements must be pure, including iteration of spread operands.
for elem in arr.elems.iter().flatten() { for elem in arr.elems.iter().flatten() {
elem.visit_with(self); elem.visit_with(self);
} }
...@@ -1147,6 +1147,22 @@ impl<'a> Visit for SideEffectVisitor<'a> { ...@@ -1147,6 +1147,22 @@ impl<'a> Visit for SideEffectVisitor<'a> {
} }
} }
fn visit_expr_or_spread(&mut self, expr: &ExprOrSpread) {
check_side_effects!(self);
if expr.spread.is_some()
&& !matches!(unparen(&expr.expr), Expr::Array(_) | Expr::Lit(Lit::Str(_)))
{
// Spreading calls Symbol.iterator and next, which may have side effects even
// when evaluating the operand is pure. Only literal arrays and strings have
// known iteration behavior (assuming built-ins are not monkey-patched).
self.mark_side_effect();
} else {
// Still check literal array elements, including any nested spreads.
expr.expr.visit_with(self);
}
}
fn visit_prop_or_spread(&mut self, prop: &PropOrSpread) { fn visit_prop_or_spread(&mut self, prop: &PropOrSpread) {
check_side_effects!(self); check_side_effects!(self);
...@@ -1733,7 +1749,7 @@ mod tests { ...@@ -1733,7 +1749,7 @@ mod tests {
"const obj = { [foo()]: value };" "const obj = { [foo()]: value };"
); );
no_side_effects!(test_spread_in_array, "const arr = [...other];"); side_effects!(test_spread_in_array, "const arr = [...other];");
no_side_effects!(test_spread_in_object, "const obj = { ...other };"); no_side_effects!(test_spread_in_object, "const obj = { ...other };");
...@@ -2505,6 +2521,78 @@ mod tests { ...@@ -2505,6 +2521,78 @@ mod tests {
); );
} }
mod spread_tests {
use super::*;
side_effects!(
test_iterator_method_side_effect,
r#"
const iterable = {
[Symbol.iterator]() {
console.log('iterator');
return { next() { return { done: true }; } };
}
};
Math.max(...iterable);
"#
);
side_effects!(
test_iterator_next_side_effect,
r#"
const iterable = {
[Symbol.iterator]() {
return {
next() {
console.log('next');
return { done: true };
}
};
}
};
Math.max(...iterable);
"#
);
side_effects!(test_unknown_iterable_in_call, "Math.max(...iterable);");
side_effects!(
test_unknown_iterable_in_annotated_call,
"/*#__PURE__*/ pure(...iterable);"
);
side_effects!(
test_unknown_iterable_in_constructor,
"new Array(...iterable);"
);
side_effects!(
test_unknown_iterable_in_annotated_constructor,
"/*#__PURE__*/ new Pure(...iterable);"
);
side_effects!(test_nested_unknown_spread, "Math.max(...[...iterable]);");
side_effects!(
test_spread_with_side_effect,
"const result = Math.max(...getArray());"
);
side_effects!(
test_spread_array_with_side_effect,
"Math.max(...[sideEffect()]);"
);
side_effects!(
test_spread_array_element_side_effect,
"const arr = [...[sideEffect()]];"
);
no_side_effects!(
test_spread_pure_in_call,
"const result = Math.max(...[1, 2, 3]);"
);
no_side_effects!(test_spread_parenthesized_array, "Math.max(...([1, 2, 3]));");
no_side_effects!(test_spread_string, "Math.max(...'123');");
no_side_effects!(test_spread_parenthesized_string, "Math.max(...('123'));");
no_side_effects!(test_spread_pure_in_constructor, "new Array(...[1, 2, 3]);");
no_side_effects!(test_nested_pure_spread, "const arr = [...[...[1, 2, 3]]];");
no_side_effects!(test_non_spread_argument, "Math.max(iterable);");
}
mod additional_edge_cases_tests { mod additional_edge_cases_tests {
use super::*; use super::*;
...@@ -2613,18 +2701,6 @@ mod tests { ...@@ -2613,18 +2701,6 @@ mod tests {
"# "#
); );
// Spread in call arguments (with pure expression)
no_side_effects!(
test_spread_pure_in_call,
"const result = Math.max(...[1, 2, 3]);"
);
// Spread in call arguments (with side effect)
side_effects!(
test_spread_with_side_effect,
"const result = Math.max(...getArray());"
);
// Complex super expression // Complex super expression
no_side_effects!( no_side_effects!(
test_super_complex_access, test_super_complex_access,
......
const iterable = {
[Symbol.iterator]() {
;(globalThis.__spread_iterator_events ??= []).push('array:iterator')
return {
next() {
globalThis.__spread_iterator_events.push('array:next')
return { done: true }
},
}
},
}
const array = [...iterable]
export {}
const iterable = {
[Symbol.iterator]() {
;(globalThis.__spread_iterator_events ??= []).push('call:iterator')
return {
next() {
globalThis.__spread_iterator_events.push('call:next')
return { done: true }
},
}
},
}
Math.max(...iterable)
export {}
const iterable = {
[Symbol.iterator]() {
;(globalThis.__spread_iterator_events ??= []).push('constructor:iterator')
return {
next() {
globalThis.__spread_iterator_events.push('constructor:next')
return { done: true }
},
}
},
}
new Array(...iterable)
export {}
import './call.js'
import './constructor.js'
import './array.js'
it('should retain effects from iterating spread operands', () => {
expect(globalThis.__spread_iterator_events?.sort()).toEqual([
'array:iterator',
'array:next',
'call:iterator',
'call:next',
'constructor:iterator',
'constructor:next',
])
})
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