-
Notifications
You must be signed in to change notification settings - Fork 49.2k
[compiler] Fix for false positive mutation of destructured spread object #33786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
...er/src/__tests__/fixtures/compiler/array-pattern-spread-creates-array.expect.md
This file contains hidden or 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,104 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
import {useMemo} from 'react'; | ||
import {makeObject_Primitives, ValidateMemoization} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
// Should memoize independently | ||
const x = useMemo(() => makeObject_Primitives(), []); | ||
|
||
const rest = useMemo(() => { | ||
const [_, ...rest] = props.array; | ||
|
||
// Should be inferred as Array.proto.push which doesn't mutate input | ||
rest.push(x); | ||
return rest; | ||
}); | ||
|
||
return ( | ||
<> | ||
<ValidateMemoization inputs={[]} output={x} /> | ||
<ValidateMemoization inputs={[props.array]} output={rest} /> | ||
</> | ||
); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{array: [0, 1, 2]}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees | ||
import { useMemo } from "react"; | ||
import { makeObject_Primitives, ValidateMemoization } from "shared-runtime"; | ||
|
||
function Component(props) { | ||
const $ = _c(9); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = makeObject_Primitives(); | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
const x = t0; | ||
let rest; | ||
if ($[1] !== props.array) { | ||
[, ...rest] = props.array; | ||
|
||
rest.push(x); | ||
$[1] = props.array; | ||
$[2] = rest; | ||
} else { | ||
rest = $[2]; | ||
} | ||
const rest_0 = rest; | ||
let t1; | ||
if ($[3] === Symbol.for("react.memo_cache_sentinel")) { | ||
t1 = <ValidateMemoization inputs={[]} output={x} />; | ||
$[3] = t1; | ||
} else { | ||
t1 = $[3]; | ||
} | ||
let t2; | ||
if ($[4] !== props.array) { | ||
t2 = [props.array]; | ||
$[4] = props.array; | ||
$[5] = t2; | ||
} else { | ||
t2 = $[5]; | ||
} | ||
let t3; | ||
if ($[6] !== rest_0 || $[7] !== t2) { | ||
t3 = ( | ||
<> | ||
{t1} | ||
<ValidateMemoization inputs={t2} output={rest_0} /> | ||
</> | ||
); | ||
$[6] = rest_0; | ||
$[7] = t2; | ||
$[8] = t3; | ||
} else { | ||
t3 = $[8]; | ||
} | ||
return t3; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ array: [0, 1, 2] }], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) <div>{"inputs":[],"output":{"a":0,"b":"value1","c":true}}</div><div>{"inputs":[[0,1,2]],"output":[1,2,{"a":0,"b":"value1","c":true}]}</div> |
28 changes: 28 additions & 0 deletions
28
...ugin-react-compiler/src/__tests__/fixtures/compiler/array-pattern-spread-creates-array.js
This file contains hidden or 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,28 @@ | ||
// @validatePreserveExistingMemoizationGuarantees | ||
import {useMemo} from 'react'; | ||
import {makeObject_Primitives, ValidateMemoization} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
// Should memoize independently | ||
const x = useMemo(() => makeObject_Primitives(), []); | ||
|
||
const rest = useMemo(() => { | ||
const [_, ...rest] = props.array; | ||
|
||
// Should be inferred as Array.proto.push which doesn't mutate input | ||
rest.push(x); | ||
return rest; | ||
}); | ||
|
||
return ( | ||
<> | ||
<ValidateMemoization inputs={[]} output={x} /> | ||
<ValidateMemoization inputs={[props.array]} output={rest} /> | ||
</> | ||
); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{array: [0, 1, 2]}], | ||
}; |
62 changes: 62 additions & 0 deletions
62
...es/compiler/repro-local-mutation-of-new-object-from-destructured-prop.expect.md
This file contains hidden or 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,62 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {Stringify} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const {a} = props; | ||
const {b, ...rest} = a; | ||
// Local mutation of `rest` is allowed since it is a newly allocated object | ||
rest.value = props.value; | ||
return <Stringify rest={rest} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{a: {b: 0, other: 'other'}, value: 42}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { Stringify } from "shared-runtime"; | ||
|
||
function Component(props) { | ||
const $ = _c(5); | ||
const { a } = props; | ||
let rest; | ||
if ($[0] !== a || $[1] !== props.value) { | ||
const { b, ...t0 } = a; | ||
rest = t0; | ||
|
||
rest.value = props.value; | ||
$[0] = a; | ||
$[1] = props.value; | ||
$[2] = rest; | ||
} else { | ||
rest = $[2]; | ||
} | ||
let t0; | ||
if ($[3] !== rest) { | ||
t0 = <Stringify rest={rest} />; | ||
$[3] = rest; | ||
$[4] = t0; | ||
} else { | ||
t0 = $[4]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ a: { b: 0, other: "other" }, value: 42 }], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) <div>{"rest":{"other":"other","value":42}}</div> |
14 changes: 14 additions & 0 deletions
14
.../__tests__/fixtures/compiler/repro-local-mutation-of-new-object-from-destructured-prop.js
This file contains hidden or 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,14 @@ | ||
import {Stringify} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const {a} = props; | ||
const {b, ...rest} = a; | ||
// Local mutation of `rest` is allowed since it is a newly allocated object | ||
rest.value = props.value; | ||
return <Stringify rest={rest} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{a: {b: 0, other: 'other'}, value: 42}], | ||
}; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the example where i realized we weren't inferring array pattern spread as an array type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh makes sense.
MergeReactiveScopesThatInvalidateTogether
uses type info as a proxy for whether something was newly created, which currently works due to how we infer types