[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: a9e348181d
Pull Request resolved: https://github.com/facebook/react/pull/30333
This commit is contained in:
Joe Savona
2024-07-15 17:32:04 +09:00
parent 2683c0a18c
commit fee423e083
2 changed files with 14 additions and 4 deletions

View File

@@ -24,8 +24,6 @@ import { EARLY_RETURN_SENTINEL } from "./CodegenReactiveFunction";
import { ReactiveFunctionTransform, Transformed } from "./visitors";
/**
* TODO: Actualy propagate early return information, for now we throw a Todo bailout.
*
* This pass ensures that reactive blocks honor the control flow behavior of the
* original code including early return semantics. Specifically, if a reactive
* scope early returned during the previous execution and the inputs to that block
@@ -135,6 +133,14 @@ class Transform extends ReactiveFunctionTransform<State> {
scopeBlock: ReactiveScopeBlock,
parentState: State
): void {
/**
* Exit early if an earlier pass has already created an early return,
* which may happen in alternate compiler configurations.
*/
if (scopeBlock.scope.earlyReturnValue !== null) {
return;
}
const innerState: State = {
withinReactiveScope: true,
earlyReturnValue: parentState.earlyReturnValue,

View File

@@ -932,10 +932,14 @@ class PruneScopesTransform extends ReactiveFunctionTransform<
* is early-returned from within the scope. For now we intentionaly keep
* these scopes, and let them get pruned later by PruneUnusedScopes
* _after_ handling the early-return case in PropagateEarlyReturns.
*
* Also keep the scope if an early return was created by some earlier pass,
* which may happen in alternate compiler configurations.
*/
if (
scopeBlock.scope.declarations.size === 0 &&
scopeBlock.scope.reassignments.size === 0
(scopeBlock.scope.declarations.size === 0 &&
scopeBlock.scope.reassignments.size === 0) ||
scopeBlock.scope.earlyReturnValue !== null
) {
return { kind: "keep" };
}