@samitouri / QOS-React / commits / ca5fef0f4a

[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

Joe Savona committed Jul 23, 2024 at 10:07 UTC ca5fef0f4a7b5dfc48fe560206f7b1d3621966ac
1 file changed +3 -5
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/FlattenScopesWithHooksOrUseHIR.ts
+3 -5
@@ -11,7 +11,6 @@ import {
11 HIRFunction,
12 LabelTerminal,
13 PrunedScopeTerminal,
14 - ReactiveScope,
14 getHookKind,
15 isUseOperator,
16 } from '../HIR';
@@ -39,12 +38,11 @@ import {retainWhere} from '../Utils/utils';
38 * to ensure the hook call does not inadvertently become conditional.
39 */
40 export function flattenScopesWithHooksOrUseHIR(fn: HIRFunction): void {
42 - const activeScopes: Array<{block: BlockId; scope: ReactiveScope}> = [];
41 + const activeScopes: Array<{block: BlockId; fallthrough: BlockId}> = [];
42 const prune: Array<BlockId> = [];
43
44 for (const [, block] of fn.body.blocks) {
46 - const firstId = block.instructions[0]?.id ?? block.terminal.id;
47 - retainWhere(activeScopes, current => current.scope.range.end > firstId);
45 + retainWhere(activeScopes, current => current.fallthrough !== block.id);
46
47 for (const instr of block.instructions) {
48 const {value} = instr;
@@ -66,7 +64,7 @@ export function flattenScopesWithHooksOrUseHIR(fn: HIRFunction): void {
64 if (block.terminal.kind === 'scope') {
65 activeScopes.push({
66 block: block.id,
69 - scope: block.terminal.scope,
67 + fallthrough: block.terminal.fallthrough,
68 });
69 }
70 }