-
Notifications
You must be signed in to change notification settings - Fork 49k
[compiler][bugfix] Fix hoisting of let declarations #32724
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
59 changes: 59 additions & 0 deletions
59
...act-compiler/src/__tests__/fixtures/compiler/hoisting-invalid-tdz-let.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,59 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Foo() { | ||
const getX = () => x; | ||
console.log(getX()); | ||
|
||
let x = 4; | ||
x += 5; | ||
|
||
return <Stringify getX={getX} shouldInvokeFns={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
function Foo() { | ||
const $ = _c(2); | ||
let getX; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
getX = () => x; | ||
console.log(getX()); | ||
|
||
let x; | ||
x = 4; | ||
x = x + 5; | ||
$[0] = getX; | ||
} else { | ||
getX = $[0]; | ||
} | ||
x; | ||
let t0; | ||
if ($[1] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = <Stringify getX={getX} shouldInvokeFns={true} />; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: exception) Cannot access 'x' before initialization |
14 changes: 14 additions & 0 deletions
14
...s/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-invalid-tdz-let.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 @@ | ||
function Foo() { | ||
const getX = () => x; | ||
console.log(getX()); | ||
|
||
let x = 4; | ||
x += 5; | ||
|
||
return <Stringify getX={getX} shouldInvokeFns={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; |
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
67 changes: 67 additions & 0 deletions
67
...r/src/__tests__/fixtures/compiler/hoisting-reassigned-let-declaration.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,67 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {CONST_NUMBER0, CONST_NUMBER1, Stringify} from 'shared-runtime'; | ||
|
||
function useHook({cond}) { | ||
'use memo'; | ||
const getX = () => x; | ||
|
||
let x = CONST_NUMBER0; | ||
if (cond) { | ||
x += CONST_NUMBER1; | ||
} | ||
return <Stringify getX={getX} shouldInvokeFns={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useHook, | ||
params: [{cond: true}], | ||
sequentialRenders: [{cond: true}, {cond: true}, {cond: false}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { CONST_NUMBER0, CONST_NUMBER1, Stringify } from "shared-runtime"; | ||
|
||
function useHook(t0) { | ||
"use memo"; | ||
const $ = _c(2); | ||
const { cond } = t0; | ||
let t1; | ||
if ($[0] !== cond) { | ||
const getX = () => x; | ||
|
||
let x; | ||
x = CONST_NUMBER0; | ||
if (cond) { | ||
x = x + CONST_NUMBER1; | ||
x; | ||
} | ||
|
||
t1 = <Stringify getX={getX} shouldInvokeFns={true} />; | ||
$[0] = cond; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
return t1; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useHook, | ||
params: [{ cond: true }], | ||
sequentialRenders: [{ cond: true }, { cond: true }, { cond: false }], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) <div>{"getX":{"kind":"Function","result":1},"shouldInvokeFns":true}</div> | ||
<div>{"getX":{"kind":"Function","result":1},"shouldInvokeFns":true}</div> | ||
<div>{"getX":{"kind":"Function","result":0},"shouldInvokeFns":true}</div> |
18 changes: 18 additions & 0 deletions
18
...gin-react-compiler/src/__tests__/fixtures/compiler/hoisting-reassigned-let-declaration.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,18 @@ | ||
import {CONST_NUMBER0, CONST_NUMBER1, Stringify} from 'shared-runtime'; | ||
|
||
function useHook({cond}) { | ||
'use memo'; | ||
const getX = () => x; | ||
|
||
let x = CONST_NUMBER0; | ||
if (cond) { | ||
x += CONST_NUMBER1; | ||
} | ||
return <Stringify getX={getX} shouldInvokeFns={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useHook, | ||
params: [{cond: true}], | ||
sequentialRenders: [{cond: true}, {cond: true}, {cond: false}], | ||
}; |
69 changes: 69 additions & 0 deletions
69
...__tests__/fixtures/compiler/hoisting-reassigned-twice-let-declaration.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 {CONST_NUMBER0, CONST_NUMBER1, Stringify} from 'shared-runtime'; | ||
|
||
function useHook({cond}) { | ||
'use memo'; | ||
const getX = () => x; | ||
|
||
let x = CONST_NUMBER0; | ||
if (cond) { | ||
x += CONST_NUMBER1; | ||
x = Math.min(x, 100); | ||
} | ||
return <Stringify getX={getX} shouldInvokeFns={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useHook, | ||
params: [{cond: true}], | ||
sequentialRenders: [{cond: true}, {cond: true}, {cond: false}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { CONST_NUMBER0, CONST_NUMBER1, Stringify } from "shared-runtime"; | ||
|
||
function useHook(t0) { | ||
"use memo"; | ||
const $ = _c(2); | ||
const { cond } = t0; | ||
let t1; | ||
if ($[0] !== cond) { | ||
const getX = () => x; | ||
|
||
let x; | ||
x = CONST_NUMBER0; | ||
if (cond) { | ||
x = x + CONST_NUMBER1; | ||
x; | ||
x = Math.min(x, 100); | ||
} | ||
|
||
t1 = <Stringify getX={getX} shouldInvokeFns={true} />; | ||
$[0] = cond; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
return t1; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useHook, | ||
params: [{ cond: true }], | ||
sequentialRenders: [{ cond: true }, { cond: true }, { cond: false }], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) <div>{"getX":{"kind":"Function","result":1},"shouldInvokeFns":true}</div> | ||
<div>{"getX":{"kind":"Function","result":1},"shouldInvokeFns":true}</div> | ||
<div>{"getX":{"kind":"Function","result":0},"shouldInvokeFns":true}</div> |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
We could rewrite these to be a
StoreLocal LValue: {kind: "Let"}
which would result in cleaner output. Note that we currently produce aDeclareContext
followed by aStoreContext
for all non-hoisted context variables.I implemented this with a
DeclareContext
followed byStoreContext
to better preserve instruction semantics (aStoreLocal
followed byStoreContext
to the same variable feels a bit nonsensical). Since this is currently the last pass before codegen, this isn't a problem at the moment.