From ca5fef0f4a7b5dfc48fe560206f7b1d3621966ac Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 23 Jul 2024 10:07:50 +0900 Subject: [PATCH] [compiler] Flatten scopes based on fallthrough, not scope range Once we create scopes, we should prefer to use the block structure to identify active scope ranges rather than the scope range. They _should_ always be in sync, but ultimately the block structure determine the active range (ie the id of the 'scope' terminal and the terminal's fallthrough block). ghstack-source-id: 730b6d1cfaf0eb689d71057c78a48045ac4fb11c Pull Request resolved: https://github.com/facebook/react/pull/30398 --- .../src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts index fd2f652075..103923a2e4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts @@ -11,7 +11,6 @@ import { HIRFunction, LabelTerminal, PrunedScopeTerminal, - ReactiveScope, getHookKind, isUseOperator, } from '../HIR'; @@ -39,12 +38,11 @@ import {retainWhere} from '../Utils/utils'; * to ensure the hook call does not inadvertently become conditional. */ export function flattenScopesWithHooksOrUseHIR(fn: HIRFunction): void { - const activeScopes: Array<{block: BlockId; scope: ReactiveScope}> = []; + const activeScopes: Array<{block: BlockId; fallthrough: BlockId}> = []; const prune: Array = []; for (const [, block] of fn.body.blocks) { - const firstId = block.instructions[0]?.id ?? block.terminal.id; - retainWhere(activeScopes, current => current.scope.range.end > firstId); + retainWhere(activeScopes, current => current.fallthrough !== block.id); for (const instr of block.instructions) { const {value} = instr; @@ -66,7 +64,7 @@ export function flattenScopesWithHooksOrUseHIR(fn: HIRFunction): void { if (block.terminal.kind === 'scope') { activeScopes.push({ block: block.id, - scope: block.terminal.scope, + fallthrough: block.terminal.fallthrough, }); } }