@samitouri / QOS-React-2 / commits / f7ca10a0c7

Repro for "no value for temporary"

I addressed some of the cases that lead to this invariant but there were still more. In this case, we have scopes like this: ``` scope @1 declarations=[t$0] { let t$0 = ArrayExpression [] if (...) { return null; } } scope @2 deps=[t$0] declarations=[t$1] { let t$1 = Jsx children=[t$0] ... } ``` Because scope 1 has an early return, PropagateEarlyReturns wraps its contents in a label and converts the returns to breaks: ``` scope @1 declarations=[t$0] earlyReturn={t$2} { let t$2 bb0: { let t$0 = ArrayExpression [] if (...) { t$2 = null; break bb0; } } } scope @2 deps=[t$0] declarations=[t$1] { let t$1 = Jsx children=[t$0] ... } ``` But then MergeReactiveScopesThatInvalidateTogether smushes them together: ``` scope @1 declarations=[t$1] earlyReturn={t$2} { let t$2 bb0: { let t$0 = ArrayExpression [] // <--- Oops! We're inside a block now if (...) { t$2 = null; break bb0; } } let t$1 = Jsx children=[t$0] ... } ``` Note that the `t$0` binding is now created inside the labeled block, so it's no longer accessible to the Jsx instruction which follows the labeled block. This isn't an issue with promoting temporaries or propagating outputs, but a simple issue of the labeled block (used for early return) introducing a new block scope. The solution (in the next PR) is to simply reorder the passes so that we transform for early returns after other optimizations. This means the jsx element will basically move inside the labeled block, solving the scoping issue: ``` scope @1 declarations=[t$1] earlyReturn={t$2} { let t$2 bb0: { let t$0 = ArrayExpression [] // ok, same block scope as its use if (...) { t$2 = null; break bb0; } let t$1 = Jsx children=[t$0] // note this moved inside the labeled block } } ```

Joe Savona committed Mar 13, 2024 at 21:29 UTC f7ca10a0c7085002076bf85a24a9a4b07598e5f7
2 files changed +59
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-repro-no-value-for-temporary-reactive-scope-with-early-return.expect.md new
+40
@@ -0,0 +1,40 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
6 +import { identity, makeObject_Primitives } from "shared-runtime";
7 +
8 +function Component(props) {
9 + const object = makeObject_Primitives();
10 + const cond = makeObject_Primitives();
11 + if (!cond) {
12 + return null;
13 + }
14 +
15 + return (
16 + <div className="foo">
17 + {fbt(
18 + "Lorum ipsum" + fbt.param("thing", object.b) + " blah blah blah",
19 + "More text"
20 + )}
21 + </div>
22 + );
23 +}
24 +
25 +```
26 +
27 +
28 +## Error
29 +
30 +```
31 + 10 |
32 + 11 | return (
33 +> 12 | <div className="foo">
34 + | ^^^^^ [ReactForget] Invariant: [Codegen] No value found for temporary. Value for 'read $40:TPrimitive' was not set in the codegen context (12:12)
35 + 13 | {fbt(
36 + 14 | "Lorum ipsum" + fbt.param("thing", object.b) + " blah blah blah",
37 + 15 | "More text"
38 +```
39 +
40 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-repro-no-value-for-temporary-reactive-scope-with-early-return.js new
+19
@@ -0,0 +1,19 @@
1 +// @flow @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
2 +import { identity, makeObject_Primitives } from "shared-runtime";
3 +
4 +function Component(props) {
5 + const object = makeObject_Primitives();
6 + const cond = makeObject_Primitives();
7 + if (!cond) {
8 + return null;
9 + }
10 +
11 + return (
12 + <div className="foo">
13 + {fbt(
14 + "Lorum ipsum" + fbt.param("thing", object.b) + " blah blah blah",
15 + "More text"
16 + )}
17 + </div>
18 + );
19 +}