Unverified Commit 68f20d1c authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: Honor PURE annotations for callback arguments (#100577)

## Summary

Honor PURE annotations without treating function arguments as invoked
when checking call and constructor side effects. Continue checking
argument evaluation, while retaining conservative callback handling for
unannotated known builtins.

## Verification

- Not run: tests (split existing commits into a PR stack; no source
changes).

<!-- NEXT_JS_LLM -->
parent cc02d668
...@@ -1018,7 +1018,8 @@ impl<'a> Visit for SideEffectVisitor<'a> { ...@@ -1018,7 +1018,8 @@ impl<'a> Visit for SideEffectVisitor<'a> {
// Impure expressions (conservative) // Impure expressions (conservative)
Expr::Call(call) => { Expr::Call(call) => {
// Check for /*#__PURE__*/ annotation or for a well known function // Check for /*#__PURE__*/ annotation or for a well known function
if self.is_pure_annotated(call.span) || self.is_known_pure_builtin(&call.callee) { let is_pure_annotated = self.is_pure_annotated(call.span);
if is_pure_annotated || self.is_known_pure_builtin(&call.callee) {
// For known pure builtins, we need to check both: // For known pure builtins, we need to check both:
// 1. The receiver (e.g., the array in [foo(), 2, 3].map(...)) // 1. The receiver (e.g., the array in [foo(), 2, 3].map(...))
// 2. The arguments // 2. The arguments
...@@ -1027,8 +1028,9 @@ impl<'a> Visit for SideEffectVisitor<'a> { ...@@ -1027,8 +1028,9 @@ impl<'a> Visit for SideEffectVisitor<'a> {
call.callee.visit_with(self); call.callee.visit_with(self);
// Check all arguments // Check all arguments
// Assume that any function expressions in the arguments will be invoked. // Known builtins may invoke callbacks, but a PURE annotation covers
self.with_will_invoke_fn_exprs(true, |this| { // those effects too. Argument evaluation must still be checked.
self.with_will_invoke_fn_exprs(!is_pure_annotated, |this| {
call.args.visit_children_with(this); call.args.visit_children_with(this);
}); });
} else if self.is_require_or_import(&call.callee) { } else if self.is_require_or_import(&call.callee) {
...@@ -1045,9 +1047,10 @@ impl<'a> Visit for SideEffectVisitor<'a> { ...@@ -1045,9 +1047,10 @@ impl<'a> Visit for SideEffectVisitor<'a> {
} }
Expr::New(new) => { Expr::New(new) => {
// Check for /*#__PURE__*/ annotation or known pure constructor // Check for /*#__PURE__*/ annotation or known pure constructor
if self.is_pure_annotated(new.span) || self.is_known_pure_constructor(&new.callee) { let is_pure_annotated = self.is_pure_annotated(new.span);
if is_pure_annotated || self.is_known_pure_constructor(&new.callee) {
// Pure constructor, but still need to check arguments // Pure constructor, but still need to check arguments
self.with_will_invoke_fn_exprs(true, |this| { self.with_will_invoke_fn_exprs(!is_pure_annotated, |this| {
new.args.visit_children_with(this); new.args.visit_children_with(this);
}); });
} else { } else {
...@@ -1573,7 +1576,22 @@ mod tests { ...@@ -1573,7 +1576,22 @@ mod tests {
no_side_effects!( no_side_effects!(
test_pure_annotation_with_pure_args, test_pure_annotation_with_pure_args,
"/*#__PURE__*/ foo(1, 2, 3);" "/*#__PURE__*/ foo(1, 2, 3, function(){ console.log(1); });"
);
no_side_effects!(
test_pure_annotation_with_arrow_callback,
"/*#__PURE__*/ foo(() => console.log(1));"
);
no_side_effects!(
test_pure_annotation_builtin_with_callback,
"/*#__PURE__*/ [1].map(() => console.log(1));"
);
no_side_effects!(
test_pure_annotation_constructor_with_callback,
"/*#__PURE__*/ new Promise(() => console.log(1));"
); );
// Even with PURE annotation, impure arguments make it impure // Even with PURE annotation, impure arguments make it impure
......
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