-
Notifications
You must be signed in to change notification settings - Fork 49.7k
[compiler] Fix codegen for nested method calls with memoized properties #34100
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
Draft
poteto
wants to merge
1
commit into
main
Choose a base branch
from
pr34100
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−110
Draft
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
69 changes: 69 additions & 0 deletions
69
.../src/__tests__/fixtures/compiler/codegen-methodcall-readonly-receiver.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,69 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {PrimitiveBox} from 'shared-runtime'; | ||
|
|
||
| function Component({value, realmax}) { | ||
| const box = new PrimitiveBox(value); | ||
| const maxValue = Math.max(box.get(), realmax); | ||
| // ^^^^^^^^^ should not be separated into static call | ||
| return <div>{maxValue}</div>; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Component, | ||
| params: [{value: 42, realmax: 100}], | ||
| isComponent: true, | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; | ||
| import { PrimitiveBox } from "shared-runtime"; | ||
|
|
||
| function Component(t0) { | ||
| const $ = _c(6); | ||
| const { value, realmax } = t0; | ||
| let t1; | ||
| let t2; | ||
| let t3; | ||
| if ($[0] !== value) { | ||
| const box = new PrimitiveBox(value); | ||
| t1 = Math; | ||
| t2 = t1.max; | ||
| t3 = box.get(); | ||
| $[0] = value; | ||
| $[1] = t1; | ||
| $[2] = t2; | ||
| $[3] = t3; | ||
| } else { | ||
| t1 = $[1]; | ||
| t2 = $[2]; | ||
| t3 = $[3]; | ||
| } | ||
| const maxValue = t2(t3, realmax); | ||
| let t4; | ||
| if ($[4] !== maxValue) { | ||
| t4 = <div>{maxValue}</div>; | ||
| $[4] = maxValue; | ||
| $[5] = t4; | ||
| } else { | ||
| t4 = $[5]; | ||
| } | ||
| return t4; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Component, | ||
| params: [{ value: 42, realmax: 100 }], | ||
| isComponent: true, | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: ok) <div>100</div> |
14 changes: 14 additions & 0 deletions
14
...in-react-compiler/src/__tests__/fixtures/compiler/codegen-methodcall-readonly-receiver.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 {PrimitiveBox} from 'shared-runtime'; | ||
|
|
||
| function Component({value, realmax}) { | ||
| const box = new PrimitiveBox(value); | ||
| const maxValue = Math.max(box.get(), realmax); | ||
| // ^^^^^^^^^ should not be separated into static call | ||
| return <div>{maxValue}</div>; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Component, | ||
| params: [{value: 42, realmax: 100}], | ||
| isComponent: true, | ||
| }; |
85 changes: 85 additions & 0 deletions
85
...gin-react-compiler/src/__tests__/fixtures/compiler/codegen-methodcall.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,85 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| function foo() { | ||
| return { | ||
| bar() { | ||
| return 3.14; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const YearsAndMonthsSince = () => { | ||
| const diff = foo(); | ||
| const months = Math.floor(diff.bar()); | ||
| return <>{months}</>; | ||
| }; | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: YearsAndMonthsSince, | ||
| params: [], | ||
| isComponent: true, | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; | ||
| function foo() { | ||
| const $ = _c(1); | ||
| let t0; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| t0 = { | ||
| bar() { | ||
| return 3.14; | ||
| }, | ||
| }; | ||
| $[0] = t0; | ||
| } else { | ||
| t0 = $[0]; | ||
| } | ||
| return t0; | ||
| } | ||
|
|
||
| const YearsAndMonthsSince = () => { | ||
| const $ = _c(4); | ||
| let t0; | ||
| let t1; | ||
| let t2; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| const diff = foo(); | ||
| t0 = Math; | ||
| t1 = t0.floor; | ||
| t2 = diff.bar(); | ||
| $[0] = t0; | ||
| $[1] = t1; | ||
| $[2] = t2; | ||
| } else { | ||
| t0 = $[0]; | ||
| t1 = $[1]; | ||
| t2 = $[2]; | ||
| } | ||
| const months = t1(t2); | ||
| let t3; | ||
| if ($[3] === Symbol.for("react.memo_cache_sentinel")) { | ||
| t3 = <>{months}</>; | ||
| $[3] = t3; | ||
| } else { | ||
| t3 = $[3]; | ||
| } | ||
| return t3; | ||
| }; | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: YearsAndMonthsSince, | ||
| params: [], | ||
| isComponent: true, | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: ok) 3 |
19 changes: 19 additions & 0 deletions
19
...ackages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-methodcall.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,19 @@ | ||
| function foo() { | ||
| return { | ||
| bar() { | ||
| return 3.14; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const YearsAndMonthsSince = () => { | ||
| const diff = foo(); | ||
| const months = Math.floor(diff.bar()); | ||
| return <>{months}</>; | ||
| }; | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: YearsAndMonthsSince, | ||
| params: [], | ||
| isComponent: true, | ||
| }; |
31 changes: 0 additions & 31 deletions
31
...rc/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.expect.md
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
...-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.js
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
...ler/error.todo-nested-method-calls-lower-property-load-into-temporary.expect.md
This file was deleted.
Oops, something went wrong.
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.
Hmmm, the invariant was there to make sure that we don't rewrite a method invocation, which may read from its receiver. In the
Math.*cases they are static functions and that isn't a problem, but i'm wondering if the wrong signature could break compilation w the change as implemented.I think it would be something that a) has the receiver as readonly and b) returns a primitive. Can you try constructing a method like this in shared-runtime and type it (shared-runtime-type-provider) to check that it works correctly?