Skip to content

Commit fee423e

Browse files
committed
[compiler] Handle earlier creation of early return on scopes
I'm experimenting with a new pass that sometimes creates scopes with early returns earlier in the pipeline, but there are a few passes that assume that can't happen. This PR is updating those passes just to be more resilient to help unblock experimentation. ghstack-source-id: a9e3481 Pull Request resolved: #30333
1 parent 2683c0a commit fee423e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PropagateEarlyReturns.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import { EARLY_RETURN_SENTINEL } from "./CodegenReactiveFunction";
2424
import { ReactiveFunctionTransform, Transformed } from "./visitors";
2525

2626
/**
27-
* TODO: Actualy propagate early return information, for now we throw a Todo bailout.
28-
*
2927
* This pass ensures that reactive blocks honor the control flow behavior of the
3028
* original code including early return semantics. Specifically, if a reactive
3129
* scope early returned during the previous execution and the inputs to that block
@@ -135,6 +133,14 @@ class Transform extends ReactiveFunctionTransform<State> {
135133
scopeBlock: ReactiveScopeBlock,
136134
parentState: State
137135
): void {
136+
/**
137+
* Exit early if an earlier pass has already created an early return,
138+
* which may happen in alternate compiler configurations.
139+
*/
140+
if (scopeBlock.scope.earlyReturnValue !== null) {
141+
return;
142+
}
143+
138144
const innerState: State = {
139145
withinReactiveScope: true,
140146
earlyReturnValue: parentState.earlyReturnValue,

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonEscapingScopes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,10 +932,14 @@ class PruneScopesTransform extends ReactiveFunctionTransform<
932932
* is early-returned from within the scope. For now we intentionaly keep
933933
* these scopes, and let them get pruned later by PruneUnusedScopes
934934
* _after_ handling the early-return case in PropagateEarlyReturns.
935+
*
936+
* Also keep the scope if an early return was created by some earlier pass,
937+
* which may happen in alternate compiler configurations.
935938
*/
936939
if (
937-
scopeBlock.scope.declarations.size === 0 &&
938-
scopeBlock.scope.reassignments.size === 0
940+
(scopeBlock.scope.declarations.size === 0 &&
941+
scopeBlock.scope.reassignments.size === 0) ||
942+
scopeBlock.scope.earlyReturnValue !== null
939943
) {
940944
return { kind: "keep" };
941945
}

0 commit comments

Comments
 (0)