From 91f41f5ee321fe4cebb5f33bf524bbe52fe9b24b Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Wed, 31 May 2023 13:54:59 +0100 Subject: [PATCH] [hir] Don't track ref value as a valid dep --- .../PropagateScopeDependencies.ts | 6 +++ ...-current-aliased-no-added-to-dep.expect.md | 48 +++++++++++++++++++ .../ref-current-aliased-no-added-to-dep.js | 9 ++++ ...rrent-aliased-not-added-to-dep-2.expect.md | 47 ++++++++++++++++++ .../ref-current-aliased-not-added-to-dep-2.js | 7 +++ 5 files changed, 117 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.js create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.js diff --git a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts index 460d630d2c..842fd68101 100644 --- a/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts +++ b/compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts @@ -11,6 +11,7 @@ import { IdentifierId, InstructionId, InstructionKind, + isRefValueType, isUseRefType, makeInstructionId, Place, @@ -323,6 +324,11 @@ class Context { return false; } + // ref value is not a valid dep + if (isRefValueType(maybeDependency.identifier)) { + return false; + } + const identifier = maybeDependency.identifier; // If this operand is used in a scope, has a dynamic value, and was defined // before this scope, then its a dependency of the scope. diff --git a/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.expect.md new file mode 100644 index 0000000000..a68e34a230 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.expect.md @@ -0,0 +1,48 @@ + +## Input + +```javascript +function VideoTab() { + const ref = useRef(); + const t = ref.current; + let x = () => { + t; + }; + + return ; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function VideoTab() { + const $ = useMemoCache(3); + const ref = useRef(); + const t = ref.current; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = () => { + t; + }; + $[0] = t0; + } else { + t0 = $[0]; + } + const x = t0; + const c_1 = $[1] !== x; + let t1; + if (c_1) { + t1 = ; + $[1] = x; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.js b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.js new file mode 100644 index 0000000000..4ee957e987 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.js @@ -0,0 +1,9 @@ +function VideoTab() { + const ref = useRef(); + const t = ref.current; + let x = () => { + t; + }; + + return ; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.expect.md new file mode 100644 index 0000000000..b5ec176278 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +function Foo({ a }) { + const ref = useRef(); + const val = ref.current; + const x = { a, val }; + + return ; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Foo(t21) { + const $ = useMemoCache(4); + const { a } = t21; + const ref = useRef(); + const val = ref.current; + const c_0 = $[0] !== a; + let t0; + if (c_0) { + t0 = { a, val }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + const c_2 = $[2] !== x; + let t1; + if (c_2) { + t1 = ; + $[2] = x; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.js b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.js new file mode 100644 index 0000000000..1bdd040c67 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.js @@ -0,0 +1,7 @@ +function Foo({ a }) { + const ref = useRef(); + const val = ref.current; + const x = { a, val }; + + return ; +}