-
Notifications
You must be signed in to change notification settings - Fork 50.4k
[compiler] Patch array and argument spread mutability #32521
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
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
62 changes: 62 additions & 0 deletions
62
...t-compiler/src/__tests__/fixtures/compiler/array-spread-later-mutated.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 | ||
| function useBar({arg}) { | ||
| /** | ||
| * Note that mutableIterator is mutated by the later object spread. Therefore, | ||
| * `s.values()` should be memoized within the same block as the object spread. | ||
| * In terms of compiler internals, they should have the same reactive scope. | ||
| */ | ||
| const obj = {}; | ||
| const s = new Set([obj, 5, 4]); | ||
| const mutableIterator = s.values(); | ||
| const arr = [...mutableIterator]; | ||
|
|
||
| obj.x = arg; | ||
| return arr; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: useBar, | ||
| params: [{arg: 3}], | ||
| sequentialRenders: [{arg: 3}, {arg: 3}, {arg: 4}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; | ||
| function useBar(t0) { | ||
| const $ = _c(2); | ||
| const { arg } = t0; | ||
| let arr; | ||
| if ($[0] !== arg) { | ||
| const obj = {}; | ||
| const s = new Set([obj, 5, 4]); | ||
| const mutableIterator = s.values(); | ||
| arr = [...mutableIterator]; | ||
|
|
||
| obj.x = arg; | ||
| $[0] = arg; | ||
| $[1] = arr; | ||
| } else { | ||
| arr = $[1]; | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: useBar, | ||
| params: [{ arg: 3 }], | ||
| sequentialRenders: [{ arg: 3 }, { arg: 3 }, { arg: 4 }], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: ok) [{"x":3},5,4] | ||
| [{"x":3},5,4] | ||
| [{"x":4},5,4] |
20 changes: 20 additions & 0 deletions
20
...babel-plugin-react-compiler/src/__tests__/fixtures/compiler/array-spread-later-mutated.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,20 @@ | ||
| function useBar({arg}) { | ||
| /** | ||
| * Note that mutableIterator is mutated by the later object spread. Therefore, | ||
| * `s.values()` should be memoized within the same block as the object spread. | ||
| * In terms of compiler internals, they should have the same reactive scope. | ||
| */ | ||
| const obj = {}; | ||
| const s = new Set([obj, 5, 4]); | ||
| const mutableIterator = s.values(); | ||
| const arr = [...mutableIterator]; | ||
|
|
||
| obj.x = arg; | ||
| return arr; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: useBar, | ||
| params: [{arg: 3}], | ||
| sequentialRenders: [{arg: 3}, {arg: 3}, {arg: 4}], | ||
| }; |
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
File renamed without changes.
42 changes: 42 additions & 0 deletions
42
...src/__tests__/fixtures/compiler/call-spread-argument-mutable-iterator.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,42 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {useIdentity} from 'shared-runtime'; | ||
|
|
||
| function useFoo() { | ||
| const it = new Set([1, 2]).values(); | ||
| useIdentity(); | ||
| return Math.max(...it); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: useFoo, | ||
| params: [{}], | ||
| sequentialRenders: [{}, {}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { useIdentity } from "shared-runtime"; | ||
|
|
||
| function useFoo() { | ||
| const it = new Set([1, 2]).values(); | ||
| useIdentity(); | ||
| return Math.max(...it); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: useFoo, | ||
| params: [{}], | ||
| sequentialRenders: [{}, {}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: ok) 2 | ||
| 2 |
13 changes: 13 additions & 0 deletions
13
...n-react-compiler/src/__tests__/fixtures/compiler/call-spread-argument-mutable-iterator.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,13 @@ | ||
| import {useIdentity} from 'shared-runtime'; | ||
|
|
||
| function useFoo() { | ||
| const it = new Set([1, 2]).values(); | ||
| useIdentity(); | ||
| return Math.max(...it); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: useFoo, | ||
| params: [{}], | ||
| sequentialRenders: [{}, {}], | ||
| }; |
33 changes: 33 additions & 0 deletions
33
...sts__/fixtures/compiler/error.todo-hook-call-spreads-mutable-iterator.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,33 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {useIdentity} from 'shared-runtime'; | ||
|
|
||
| function Component() { | ||
| const items = makeArray(0, 1, 2, null, 4, false, 6); | ||
| return useIdentity(...items.values()); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Component, | ||
| params: [], | ||
| sequentialRenders: [{}, {}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| 3 | function Component() { | ||
| 4 | const items = makeArray(0, 1, 2, null, 4, false, 6); | ||
| > 5 | return useIdentity(...items.values()); | ||
| | ^^^^^^^^^^^^^^ Todo: Support spread syntax for hook arguments (5:5) | ||
| 6 | } | ||
| 7 | | ||
| 8 | export const FIXTURE_ENTRYPOINT = { | ||
| ``` | ||
|
|
||
|
|
||
12 changes: 12 additions & 0 deletions
12
...compiler/src/__tests__/fixtures/compiler/error.todo-hook-call-spreads-mutable-iterator.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,12 @@ | ||
| import {useIdentity} from 'shared-runtime'; | ||
|
|
||
| function Component() { | ||
| const items = makeArray(0, 1, 2, null, 4, false, 6); | ||
| return useIdentity(...items.values()); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Component, | ||
| params: [], | ||
| sequentialRenders: [{}, {}], | ||
| }; |
Oops, something went wrong.
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 hook call bailout case. This new bailout is good (better correctness, as we were previously memoizing the
items.values()iterable)Ideally we can rewrite this to 1) create an intermediate array to consume the iterable and (2) capture and spread the array at the callsite.