-
Notifications
You must be signed in to change notification settings - Fork 49.9k
[compiler] Fix for edge cases of mutation of potentially frozen values #33984
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
+483
−106
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
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
39 changes: 39 additions & 0 deletions
39
...ts__/fixtures/compiler/error.invalid-mutate-phi-which-could-be-frozen.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,39 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {useHook} from 'shared-runtime'; | ||
|
|
||
| function Component(props) { | ||
| const frozen = useHook(); | ||
| let x; | ||
| if (props.cond) { | ||
| x = frozen; | ||
| } else { | ||
| x = {}; | ||
| } | ||
| x.property = true; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 1 error: | ||
|
|
||
| Error: This value cannot be modified | ||
|
|
||
| Modifying a value returned from a hook is not allowed. Consider moving the modification into the hook where the value is constructed. | ||
|
|
||
| error.invalid-mutate-phi-which-could-be-frozen.ts:11:2 | ||
| 9 | x = {}; | ||
| 10 | } | ||
| > 11 | x.property = true; | ||
| | ^ value cannot be modified | ||
| 12 | } | ||
| 13 | | ||
| ``` | ||
|
|
||
|
|
||
12 changes: 12 additions & 0 deletions
12
...ompiler/src/__tests__/fixtures/compiler/error.invalid-mutate-phi-which-could-be-frozen.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 {useHook} from 'shared-runtime'; | ||
|
|
||
| function Component(props) { | ||
| const frozen = useHook(); | ||
| let x; | ||
| if (props.cond) { | ||
| x = frozen; | ||
| } else { | ||
| x = {}; | ||
| } | ||
| x.property = true; | ||
| } |
61 changes: 61 additions & 0 deletions
61
.../fixtures/compiler/error.todo-for-loop-with-context-variable-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,61 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {Stringify, useIdentity} from 'shared-runtime'; | ||
|
|
||
| function Component() { | ||
| const data = useIdentity( | ||
| new Map([ | ||
| [0, 'value0'], | ||
| [1, 'value1'], | ||
| ]) | ||
| ); | ||
| const items = []; | ||
| // NOTE: `i` is a context variable because it's reassigned and also referenced | ||
| // within a closure, the `onClick` handler of each item | ||
| // TODO: for loops create a unique environment on each iteration, which means | ||
| // that if the iteration variable is only updated in the updater, the variable | ||
| // is effectively const within the body and the "update" acts more like | ||
| // a re-initialization than a reassignment. | ||
| // Until we model this "new environment" semantic, we allow this case to error | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense! |
||
| for (let i = MIN; i <= MAX; i += INCREMENT) { | ||
| items.push( | ||
| <Stringify key={i} onClick={() => data.get(i)} shouldInvokeFns={true} /> | ||
| ); | ||
| } | ||
| return <>{items}</>; | ||
| } | ||
|
|
||
| const MIN = 0; | ||
| const MAX = 3; | ||
| const INCREMENT = 1; | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| params: [], | ||
| fn: Component, | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 1 error: | ||
|
|
||
| Error: This value cannot be modified | ||
|
|
||
| Modifying a value used previously in JSX is not allowed. Consider moving the modification before the JSX. | ||
|
|
||
| error.todo-for-loop-with-context-variable-iterator.ts:18:30 | ||
| 16 | // a re-initialization than a reassignment. | ||
| 17 | // Until we model this "new environment" semantic, we allow this case to error | ||
| > 18 | for (let i = MIN; i <= MAX; i += INCREMENT) { | ||
| | ^ `i` cannot be modified | ||
| 19 | items.push( | ||
| 20 | <Stringify key={i} onClick={() => data.get(i)} shouldInvokeFns={true} /> | ||
| 21 | ); | ||
| ``` | ||
|
|
||
|
|
||
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.
yesss