-
Notifications
You must be signed in to change notification settings - Fork 50.4k
[compiler] Avoid bailouts when inserting gating #32598
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
|
|
||
|
Contributor
Author
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. This fixture no longer bails out (component syntax refs produce a hoisted |
||
| ## Input | ||
|
|
||
| ```javascript | ||
| // @flow @gating | ||
| import {Stringify} from 'shared-runtime'; | ||
| import * as React from 'react'; | ||
|
|
||
| component Foo(ref: React.RefSetter<Controls>) { | ||
| return <Stringify ref={ref} />; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: eval('(...args) => React.createElement(Foo, args)'), | ||
| params: [{ref: React.createRef()}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { isForgetEnabled_Fixtures } from "ReactForgetFeatureFlag"; | ||
| import { c as _c } from "react/compiler-runtime"; | ||
| import { Stringify } from "shared-runtime"; | ||
| import * as React from "react"; | ||
|
|
||
| const Foo = React.forwardRef(Foo_withRef); | ||
| const _isForgetEnabled_Fixtures_result = isForgetEnabled_Fixtures(); | ||
| function _Foo_withRef_optimized(_$$empty_props_placeholder$$, ref) { | ||
| const $ = _c(2); | ||
| let t0; | ||
| if ($[0] !== ref) { | ||
| t0 = <Stringify ref={ref} />; | ||
| $[0] = ref; | ||
| $[1] = t0; | ||
| } else { | ||
| t0 = $[1]; | ||
| } | ||
| return t0; | ||
| } | ||
| function _Foo_withRef_unoptimized( | ||
| _$$empty_props_placeholder$$: $ReadOnly<{}>, | ||
| ref: React.RefSetter<Controls>, | ||
| ): React.Node { | ||
| return <Stringify ref={ref} />; | ||
| } | ||
| function Foo_withRef(arg0, arg1) { | ||
| if (_isForgetEnabled_Fixtures_result) | ||
| return _Foo_withRef_optimized(arg0, arg1); | ||
| else return _Foo_withRef_unoptimized(arg0, arg1); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: eval("(...args) => React.createElement(Foo, args)"), | ||
| params: [{ ref: React.createRef() }], | ||
| }; | ||
|
|
||
| ``` | ||
| ### Eval output | ||
| (kind: ok) <div>{"ref":null}</div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // @flow @gating | ||
| import {Stringify} from 'shared-runtime'; | ||
| import * as React from 'react'; | ||
|
|
||
| component Foo(ref: React.RefSetter<Controls>) { | ||
| return <Stringify ref={ref} />; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: eval('(...args) => React.createElement(Foo, args)'), | ||
| params: [{ref: React.createRef()}], | ||
| }; |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
|
|
||
|
Contributor
Author
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. This fixture also no longer bails out (renamed from |
||
| ## Input | ||
|
|
||
| ```javascript | ||
| // @gating | ||
| import {createRef, forwardRef} from 'react'; | ||
| import {Stringify} from 'shared-runtime'; | ||
|
|
||
| const Foo = forwardRef(Foo_withRef); | ||
| function Foo_withRef(props, ref) { | ||
| return <Stringify ref={ref} {...props} />; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: eval('(...args) => React.createElement(Foo, args)'), | ||
| params: [{prop1: 1, prop2: 2, ref: createRef()}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { isForgetEnabled_Fixtures } from "ReactForgetFeatureFlag"; | ||
| import { c as _c } from "react/compiler-runtime"; // @gating | ||
| import { createRef, forwardRef } from "react"; | ||
| import { Stringify } from "shared-runtime"; | ||
|
|
||
| const Foo = forwardRef(Foo_withRef); | ||
| const _isForgetEnabled_Fixtures_result = isForgetEnabled_Fixtures(); | ||
| function _Foo_withRef_optimized(props, ref) { | ||
| const $ = _c(3); | ||
| let t0; | ||
| if ($[0] !== props || $[1] !== ref) { | ||
| t0 = <Stringify ref={ref} {...props} />; | ||
| $[0] = props; | ||
| $[1] = ref; | ||
| $[2] = t0; | ||
| } else { | ||
| t0 = $[2]; | ||
| } | ||
| return t0; | ||
| } | ||
| function _Foo_withRef_unoptimized(props, ref) { | ||
| return <Stringify ref={ref} {...props} />; | ||
| } | ||
| function Foo_withRef(arg0, arg1) { | ||
| if (_isForgetEnabled_Fixtures_result) | ||
| return _Foo_withRef_optimized(arg0, arg1); | ||
| else return _Foo_withRef_unoptimized(arg0, arg1); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: eval("(...args) => React.createElement(Foo, args)"), | ||
| params: [{ prop1: 1, prop2: 2, ref: createRef() }], | ||
| }; | ||
|
|
||
| ``` | ||
| ### Eval output | ||
| (kind: ok) <div>{"0":{"prop1":1,"prop2":2,"ref":{"current":null}},"ref":"[[ cyclic ref *3 ]]"}</div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // @gating | ||
| import {createRef, forwardRef} from 'react'; | ||
| import {Stringify} from 'shared-runtime'; | ||
|
|
||
| const Foo = forwardRef(Foo_withRef); | ||
| function Foo_withRef(props, ref) { | ||
| return <Stringify ref={ref} {...props} />; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: eval('(...args) => React.createElement(Foo, args)'), | ||
| params: [{prop1: 1, prop2: 2, ref: createRef()}], | ||
| }; |
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.
Note that this changes us to only bail out for function declarations which are referenced before declaration.
See
invalid-fnexpr-referenceandreassigned-fnexpr-variablefixtures for examples why we don't need special handling for other function kinds.This should be correct as other referenced-before-declaration identifiers are handled by directly replacing ast nodes. This may error at runtime, but still preserves source semantics (e.g. compiler-optimized code errors if and only if source code errors)