-
Couldn't load subscription status.
- Fork 49.7k
[Compiler] Fix incorrect closure hoisting causing runtime errors (#34901 #34909
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
Closed
kuomars110
wants to merge
3
commits into
facebook:main
from
kuomars110:fix-closure-hoisting-bug-34901
+156
−15
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
86 changes: 86 additions & 0 deletions
86
...n-react-compiler/src/__tests__/fixtures/compiler/closure-hoisting-bug.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,86 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| // @compilationMode(infer) | ||
| // Regression test for https://github.com/facebook/react/issues/34901 | ||
| // The compiler should NOT outline functions that capture variables from their closure. | ||
| // In this case, `() => store` captures `store` from the outer scope and should not | ||
| // be hoisted to a top-level function because `store` would be undefined. | ||
|
|
||
| class SomeStore { | ||
| test = 'hello'; | ||
| } | ||
|
|
||
| function useLocalObservable(fn) { | ||
| return fn(); | ||
| } | ||
|
|
||
| export function createSomething() { | ||
| const store = new SomeStore(); | ||
|
|
||
| const Cmp = () => { | ||
| const observedStore = useLocalObservable(() => store); | ||
| return <div>{observedStore.test}</div>; | ||
| }; | ||
|
|
||
| return Cmp; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: createSomething, | ||
| params: [], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; | ||
| class SomeStore { | ||
| test = "hello"; | ||
| } | ||
|
|
||
| function useLocalObservable(fn) { | ||
| return fn(); | ||
| } | ||
|
|
||
| export function createSomething() { | ||
| const $ = _c(2); | ||
| let t0; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| const store = new SomeStore(); | ||
|
|
||
| const Cmp = () => { | ||
| const $ = _c(2); | ||
| const observedStore = useLocalObservable(() => store); | ||
| let t0; | ||
| if ($[0] !== observedStore.test) { | ||
| t0 = <div>{observedStore.test}</div>; | ||
| $[0] = observedStore.test; | ||
| $[1] = t0; | ||
| } else { | ||
| t0 = $[1]; | ||
| } | ||
| return t0; | ||
| }; | ||
|
|
||
| t0 = Cmp; | ||
| $[0] = t0; | ||
| $[1] = store; | ||
| } else { | ||
| t0 = $[0]; | ||
| } | ||
| return t0; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: createSomething, | ||
| params: [], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: ok) <div>hello</div> |
29 changes: 29 additions & 0 deletions
29
...kages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/closure-hoisting-bug.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,29 @@ | ||
| // @compilationMode(infer) | ||
| // Regression test for https://github.com/facebook/react/issues/34901 | ||
| // The compiler should NOT outline functions that capture variables from their closure. | ||
| // In this case, `() => store` captures `store` from the outer scope and should not | ||
| // be hoisted to a top-level function because `store` would be undefined. | ||
|
|
||
| class SomeStore { | ||
| test = 'hello'; | ||
| } | ||
|
|
||
| function useLocalObservable(fn) { | ||
| return fn(); | ||
| } | ||
|
|
||
| export function createSomething() { | ||
| const store = new SomeStore(); | ||
|
|
||
| const Cmp = () => { | ||
| const observedStore = useLocalObservable(() => store); | ||
| return <div>{observedStore.test}</div>; | ||
| }; | ||
|
|
||
| return Cmp; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: createSomething, | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| // TODO: this is special because it gets imported during build. | ||
| // | ||
| // It exists as a placeholder so that DevTools can support work tag changes between releases. | ||
| // When we next publish a release, update the matching TODO in backend/renderer.js | ||
| // TODO: This module is used both by the release scripts and to expose a version | ||
| // at runtime. We should instead inject the version number as part of the build | ||
| // process, and use the ReactVersions.js module as the single source of truth. | ||
| export default '19.3.0'; |
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.