[hir][patch] Fix small bug in duplicate scope merging logic

ghstack-source-id: 98966dd4c2b264329acad077427febc2a34a5edc
Pull Request resolved: https://github.com/facebook/react-forget/pull/2906
This commit is contained in:
Mofei Zhang committed 2024-04-25 11:07:22 -04:00
1 parent f22dd62ab8
commit f196e1f703
3 files changed
+114 -10

No files matched your search

@@ -241,16 +241,6 @@ export function alignReactiveScopesToBlockScopesHIR(fn: HIRFunction): void {
const joinedScopes: DisjointSet<ReactiveScope> =
mergeOverlappingScopes(rootNode);
joinedScopes.forEach((scope, groupScope) => {
if (scope !== groupScope) {
groupScope.range.start = makeInstructionId(
Math.min(groupScope.range.start, scope.range.start)
);
groupScope.range.end = makeInstructionId(
Math.max(groupScope.range.end, scope.range.end)
);
}
});
/**
* Join scopes that begin and end at the same instructions
*/
@@ -269,6 +259,18 @@ export function alignReactiveScopesToBlockScopesHIR(fn: HIRFunction): void {
}
}
}
joinedScopes.forEach((scope, groupScope) => {
if (scope !== groupScope) {
groupScope.range.start = makeInstructionId(
Math.min(groupScope.range.start, scope.range.start)
);
groupScope.range.end = makeInstructionId(
Math.max(groupScope.range.end, scope.range.end)
);
}
});
for (const [place, originalScope] of placeScopes) {
const nextScope = joinedScopes.find(originalScope);
if (nextScope !== null && nextScope !== originalScope) {
@@ -0,0 +1,76 @@
## Input
```javascript
import { makeArray, useHook } from "shared-runtime";
/**
* Here, the cond ? [...] : defaultList value block produces two
* new values (each with its own scope):
* $0 = ["text"]
* $1 = { text: $0 }
* The same value block also mutates customList, so it must be
* merged with the scope producing customList
*/
function Foo({ defaultList, cond }) {
const comparator = (a, b) => a - b;
useHook();
const customList = makeArray(1, 5, 2);
useHook();
const result = cond
? [...customList.sort(comparator), { text: ["text"] }]
: defaultList;
return result;
}
export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [{ defaultList: [2, 4], cond: true }],
};
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import { makeArray, useHook } from "shared-runtime";
/**
* Here, the cond ? [...] : defaultList value block produces two
* new values (each with its own scope):
* $0 = ["text"]
* $1 = { text: $0 }
* The same value block also mutates customList, so it must be
* merged with the scope producing customList
*/
function Foo(t0) {
const $ = useMemoCache(1);
const { defaultList, cond } = t0;
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t1 = (a, b) => a - b;
$[0] = t1;
} else {
t1 = $[0];
}
const comparator = t1;
useHook();
const customList = makeArray(1, 5, 2);
useHook();
const result = cond
? [...customList.sort(comparator), { text: ["text"] }]
: defaultList;
return result;
}
export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [{ defaultList: [2, 4], cond: true }],
};
```
### Eval output
(kind: ok) [1,2,5,{"text":["text"]}]
@@ -0,0 +1,26 @@
import { makeArray, useHook } from "shared-runtime";
/**
* Here, the cond ? [...] : defaultList value block produces two
* new values (each with its own scope):
* $0 = ["text"]
* $1 = { text: $0 }
* The same value block also mutates customList, so it must be
* merged with the scope producing customList
*/
function Foo({ defaultList, cond }) {
const comparator = (a, b) => a - b;
useHook();
const customList = makeArray(1, 5, 2);
useHook();
const result = cond
? [...customList.sort(comparator), { text: ["text"] }]
: defaultList;
return result;
}
export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [{ defaultList: [2, 4], cond: true }],
};