-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Stop relying on identifier mutable ranges after constructi…
…ng scopes Addresses discussion at #30399 (comment). Once we've constructed scopes it's invalid to use identifier mutable ranges. The only places we can do this which i can find are ValidateMemoizedEffectDeps (which is already flawed and disabled by default) and ValidatePreservedManualMemoization. I added a todo to the former, and fixed up the latter. The idea of the fix is that for StartMemo dependencies, if they needed to be memoized (identifier.scope != null) then that scope should exist and should have already completed. If they didn't need a scope or can't have one created (eg their range spans a hook), then their scope would be pruned. So if the scope is set, not pruned, and not completed, then it's an error. For declarations (FinishMemo) the existing logic applies unchanged. ghstack-source-id: af5bfd88553de3e30621695f9d139c4dc5efb997 Pull Request resolved: #30428
- Loading branch information
1 parent
c08b516
commit b943feb
Showing
7 changed files
with
76 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
.../preserve-memo-validation/error.false-positive-useMemo-with-refs.flow.expect.md
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
...s__/fixtures/compiler/preserve-memo-validation/useMemo-with-refs.flow.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @flow @validatePreserveExistingMemoizationGuarantees | ||
import { identity } from "shared-runtime"; | ||
|
||
component Component( | ||
disableLocalRef, | ||
ref, | ||
) { | ||
const localRef = useFooRef(); | ||
const mergedRef = useMemo(() => { | ||
return disableLocalRef ? ref : identity(ref, localRef); | ||
}, [disableLocalRef, ref, localRef]); | ||
return <div ref={mergedRef} />; | ||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { identity } from "shared-runtime"; | ||
|
||
const Component = React.forwardRef(Component_withRef); | ||
function Component_withRef(t0, ref) { | ||
const $ = _c(6); | ||
const { disableLocalRef } = t0; | ||
const localRef = useFooRef(); | ||
let t1; | ||
let t2; | ||
if ($[0] !== disableLocalRef || $[1] !== ref || $[2] !== localRef) { | ||
t2 = disableLocalRef ? ref : identity(ref, localRef); | ||
$[0] = disableLocalRef; | ||
$[1] = ref; | ||
$[2] = localRef; | ||
$[3] = t2; | ||
} else { | ||
t2 = $[3]; | ||
} | ||
t1 = t2; | ||
const mergedRef = t1; | ||
let t3; | ||
if ($[4] !== mergedRef) { | ||
t3 = <div ref={mergedRef} />; | ||
$[4] = mergedRef; | ||
$[5] = t3; | ||
} else { | ||
t3 = $[5]; | ||
} | ||
return t3; | ||
} | ||
|
||
``` | ||
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters