@samitouri / QOS-React / commits / 41f52b73c2

Change reference effects for hooks

I realized we hadn't updated InferReferenceEffects to match our latest thinking on hooks. Specifically, we will default to assuming that hooks can mutate their arguments and return mutable values — this works with our model since we don't treat hooks specially for reactive scope construction. Ie, first we figure out what variables construct together, then we create scopes, then we prune scopes that contain hooks. So changing the reference effects for hooks "just works". Note that it is helpful for our unit tests to have an example hook that we know _does_ freeze its input and return a frozen value, so i've temporarily added `useFreeze()` to the list of defined hooks. That is meant as a stopgap: the right solution is to allow some way to tell the compiler about specific custom hooks and their semantics.

Joe Savona committed Feb 8, 2023 at 14:24 UTC 41f52b73c2da70dbd0fa70d26b95ae1fcfcd6693
6 files changed +110 -13
compiler/forget/src/Inference/InferReferenceEffects.ts
+11 -3
@@ -792,10 +792,18 @@ const HOOKS: Map<string, Hook> = new Map([
792 "useRef",
793 {
794 kind: "Ref",
795 - effectKind: Effect.Read,
795 + effectKind: Effect.Capture,
796 valueKind: ValueKind.Mutable,
797 },
798 ],
799 + [
800 + "useFreeze",
801 + {
802 + kind: "Ref",
803 + effectKind: Effect.Freeze,
804 + valueKind: ValueKind.Frozen,
805 + },
806 + ],
807 ]);
808
809 type HookKind = { kind: "State" } | { kind: "Ref" } | { kind: "Custom" };
@@ -812,7 +820,7 @@ export function parseHookCall(place: Place): Hook | null {
820 }
821 return {
822 kind: "Custom",
815 - effectKind: Effect.Freeze,
816 - valueKind: ValueKind.Frozen,
823 + effectKind: Effect.Mutate,
824 + valueKind: ValueKind.Mutable,
825 };
826 }
compiler/forget/src/__tests__/fixtures/hir/optional-member-expression.expect.md
+1 -10
@@ -16,16 +16,7 @@ function Foo(props) {
16
17 ```javascript
18 function Foo(props) {
19 - const $ = React.unstable_useMemoCache();
20 - const c_0 = $[0] !== props.a;
21 - let x;
22 - if (c_0) {
23 - x = bar(props.a);
24 - $[0] = props.a;
25 - $[1] = x;
26 - } else {
27 - x = $[1];
28 - }
19 + const x = bar(props.a);
20 const y = x?.b;
21
22 const z = useBar(y);
compiler/forget/src/__tests__/fixtures/hir/timers.expect.md new
+60
@@ -0,0 +1,60 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const start = performance.now();
7 + const now = Date.now();
8 + const time = performance.now() - start;
9 + return (
10 + <div>
11 + rendering took {time} at {now}
12 + </div>
13 + );
14 +}
15 +
16 +```
17 +
18 +## Code
19 +
20 +```javascript
21 +function Component(props) {
22 + const $ = React.unstable_useMemoCache();
23 + let start;
24 + if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
25 + start = performance.now();
26 + $[0] = start;
27 + } else {
28 + start = $[0];
29 + }
30 + let now;
31 + if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
32 + now = Date.now();
33 + $[1] = now;
34 + } else {
35 + now = $[1];
36 + }
37 + let t0;
38 + if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
39 + t0 = performance.now();
40 + $[2] = t0;
41 + } else {
42 + t0 = $[2];
43 + }
44 + const time = t0 - start;
45 + let t1;
46 + if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
47 + t1 = (
48 + <div>
49 + rendering took {time} at {now}
50 + </div>
51 + );
52 + $[3] = t1;
53 + } else {
54 + t1 = $[3];
55 + }
56 + return t1;
57 +}
58 +
59 +```
60 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/timers.js new
+10
@@ -0,0 +1,10 @@
1 +function Component(props) {
2 + const start = performance.now();
3 + const now = Date.now();
4 + const time = performance.now() - start;
5 + return (
6 + <div>
7 + rendering took {time} at {now}
8 + </div>
9 + );
10 +}
compiler/forget/src/__tests__/fixtures/hir/useRef-mutable.expect.md new
+23
@@ -0,0 +1,23 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const ref = useRef(null);
7 + ref.current = props.value;
8 + return ref.current;
9 +}
10 +
11 +```
12 +
13 +## Code
14 +
15 +```javascript
16 +function Component(props) {
17 + const ref = useRef(null);
18 + ref.current = props.value;
19 + return ref.current;
20 +}
21 +
22 +```
23 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/useRef-mutable.js new
+5
@@ -0,0 +1,5 @@
1 +function Component(props) {
2 + const ref = useRef(null);
3 + ref.current = props.value;
4 + return ref.current;
5 +}