-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Special-case phi inference for mixed readonly type
This allows us to handle common operations such as `useFragment(...).edges.nodes ?? []` where we have a `Phi(MixedReadonly, Array)`. The underlying pattern remains general-purpose and not Relay-specific, and any API that returns transitively "mixed" data (primitives, arrays, plain objects) can benefit from the same type refinement. ghstack-source-id: 51283108942002a14d032613a9d0b8b665ee3a94 Pull Request resolved: #30797
- Loading branch information
1 parent
4f54674
commit 1b74782
Showing
4 changed files
with
149 additions
and
44 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
42 changes: 0 additions & 42 deletions
42
...tures/compiler/error.todo-repro-missing-memoization-lack-of-phi-types.expect.md
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
...tests__/fixtures/compiler/repro-missing-memoization-lack-of-phi-types.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,105 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @flow @validatePreserveExistingMemoizationGuarantees | ||
import {useMemo} from 'react'; | ||
import {useFragment} from 'shared-runtime'; | ||
|
||
function Component() { | ||
const data = useFragment(); | ||
const nodes = data.nodes ?? []; | ||
const flatMap = nodes.flatMap(node => node.items); | ||
const filtered = flatMap.filter(item => item != null); | ||
const map = useMemo(() => filtered.map(), [filtered]); | ||
const index = filtered.findIndex(x => x === null); | ||
|
||
return ( | ||
<div> | ||
{map} | ||
{index} | ||
</div> | ||
); | ||
} | ||
|
||
``` | ||
## Code | ||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useMemo } from "react"; | ||
import { useFragment } from "shared-runtime"; | ||
|
||
function Component() { | ||
const $ = _c(11); | ||
const data = useFragment(); | ||
let t0; | ||
if ($[0] !== data.nodes) { | ||
t0 = data.nodes ?? []; | ||
$[0] = data.nodes; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
const nodes = t0; | ||
let t1; | ||
if ($[2] !== nodes) { | ||
t1 = nodes.flatMap(_temp); | ||
$[2] = nodes; | ||
$[3] = t1; | ||
} else { | ||
t1 = $[3]; | ||
} | ||
const flatMap = t1; | ||
let t2; | ||
if ($[4] !== flatMap) { | ||
t2 = flatMap.filter(_temp2); | ||
$[4] = flatMap; | ||
$[5] = t2; | ||
} else { | ||
t2 = $[5]; | ||
} | ||
const filtered = t2; | ||
let t3; | ||
let t4; | ||
if ($[6] !== filtered) { | ||
t4 = filtered.map(); | ||
$[6] = filtered; | ||
$[7] = t4; | ||
} else { | ||
t4 = $[7]; | ||
} | ||
t3 = t4; | ||
const map = t3; | ||
const index = filtered.findIndex(_temp3); | ||
let t5; | ||
if ($[8] !== map || $[9] !== index) { | ||
t5 = ( | ||
<div> | ||
{map} | ||
{index} | ||
</div> | ||
); | ||
$[8] = map; | ||
$[9] = index; | ||
$[10] = t5; | ||
} else { | ||
t5 = $[10]; | ||
} | ||
return t5; | ||
} | ||
function _temp3(x) { | ||
return x === null; | ||
} | ||
function _temp2(item) { | ||
return item != null; | ||
} | ||
function _temp(node) { | ||
return node.items; | ||
} | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) Fixture not implemented |
File renamed without changes.