Skip to content

Commit 9067d90

Browse files
committed
[compiler][bugfix] Don't insert hook guards in retry pipeline
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent c2a1961 commit 9067d90

File tree

6 files changed

+119
-9
lines changed

6 files changed

+119
-9
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,12 @@ export function compileProgram(
451451
pass.code,
452452
),
453453
};
454+
if (
455+
!compileResult.compiledFn.hasFireRewrite &&
456+
!compileResult.compiledFn.hasLoweredContextAccess
457+
) {
458+
return null;
459+
}
454460
} catch (err) {
455461
// TODO: we might want to log error here, but this will also result in duplicate logging
456462
if (err instanceof CompilerError) {

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
renameVariables,
1515
} from '.';
1616
import {CompilerError, ErrorSeverity} from '../CompilerError';
17-
import {Environment, EnvironmentConfig, ExternalFunction} from '../HIR';
17+
import {Environment, ExternalFunction} from '../HIR';
1818
import {
1919
ArrayPattern,
2020
BlockId,
@@ -156,7 +156,7 @@ export function codegenFunction(
156156
const compiled = compileResult.unwrap();
157157

158158
const hookGuard = fn.env.config.enableEmitHookGuards;
159-
if (hookGuard != null) {
159+
if (hookGuard != null && fn.env.compilerMode === 'all_features') {
160160
compiled.body = t.blockStatement([
161161
createHookGuard(
162162
hookGuard,
@@ -250,7 +250,11 @@ export function codegenFunction(
250250
}
251251

252252
const emitInstrumentForget = fn.env.config.enableEmitInstrumentForget;
253-
if (emitInstrumentForget != null && fn.id != null) {
253+
if (
254+
emitInstrumentForget != null &&
255+
fn.id != null &&
256+
fn.env.compilerMode === 'all_features'
257+
) {
254258
/*
255259
* Technically, this is a conditional hook call. However, we expect
256260
* __DEV__ and gating identifier to be runtime constants
@@ -548,7 +552,10 @@ function codegenBlockNoReset(
548552
}
549553

550554
function wrapCacheDep(cx: Context, value: t.Expression): t.Expression {
551-
if (cx.env.config.enableEmitFreeze != null) {
555+
if (
556+
cx.env.config.enableEmitFreeze != null &&
557+
cx.env.compilerMode === 'all_features'
558+
) {
552559
// The import declaration for emitFreeze is inserted in the Babel plugin
553560
return t.conditionalExpression(
554561
t.identifier('__DEV__'),
@@ -1553,7 +1560,7 @@ function createHookGuard(
15531560
* ```
15541561
*/
15551562
function createCallExpression(
1556-
config: EnvironmentConfig,
1563+
env: Environment,
15571564
callee: t.Expression,
15581565
args: Array<t.Expression | t.SpreadElement>,
15591566
loc: SourceLocation | null,
@@ -1564,8 +1571,8 @@ function createCallExpression(
15641571
callExpr.loc = loc;
15651572
}
15661573

1567-
const hookGuard = config.enableEmitHookGuards;
1568-
if (hookGuard != null && isHook) {
1574+
const hookGuard = env.config.enableEmitHookGuards;
1575+
if (hookGuard != null && isHook && env.compilerMode === 'all_features') {
15691576
const iife = t.functionExpression(
15701577
null,
15711578
[],
@@ -1701,7 +1708,7 @@ function codegenInstructionValue(
17011708
const callee = codegenPlaceToExpression(cx, instrValue.callee);
17021709
const args = instrValue.args.map(arg => codegenArgument(cx, arg));
17031710
value = createCallExpression(
1704-
cx.env.config,
1711+
cx.env,
17051712
callee,
17061713
args,
17071714
instrValue.loc,
@@ -1791,7 +1798,7 @@ function codegenInstructionValue(
17911798
);
17921799
const args = instrValue.args.map(arg => codegenArgument(cx, arg));
17931800
value = createCallExpression(
1794-
cx.env.config,
1801+
cx.env,
17951802
memberExpr,
17961803
args,
17971804
instrValue.loc,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @flow @enableEmitHookGuards @panicThreshold(none) @enableFire
6+
7+
component Foo(useDynamicHook) {
8+
useDynamicHook();
9+
return <div>hello world</div>;
10+
}
11+
12+
```
13+
14+
## Code
15+
16+
```javascript
17+
function Foo({
18+
useDynamicHook,
19+
}: $ReadOnly<{ useDynamicHook: any }>): React.Node {
20+
useDynamicHook();
21+
return <div>hello world</div>;
22+
}
23+
24+
```
25+
26+
### Eval output
27+
(kind: exception) Fixture not implemented
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @flow @enableEmitHookGuards @panicThreshold(none) @enableFire
2+
3+
component Foo(useDynamicHook) {
4+
useDynamicHook();
5+
return <div>hello world</div>;
6+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @flow @enableEmitHookGuards @panicThreshold(none) @enableFire
6+
import {useEffect, fire} from 'react';
7+
8+
function Component(props, useDynamicHook) {
9+
'use memo';
10+
useDynamicHook();
11+
const foo = props => {
12+
console.log(props);
13+
};
14+
useEffect(() => {
15+
fire(foo(props));
16+
});
17+
18+
return <div>hello world</div>;
19+
}
20+
21+
```
22+
23+
## Code
24+
25+
```javascript
26+
import { $dispatcherGuard } from "react-compiler-runtime";
27+
import { useFire } from "react/compiler-runtime";
28+
import { useEffect, fire } from "react";
29+
30+
function Component(props, useDynamicHook) {
31+
"use memo";
32+
33+
useDynamicHook();
34+
const foo = _temp;
35+
const t0 = useFire(foo);
36+
37+
useEffect(() => {
38+
t0(props);
39+
});
40+
return <div>hello world</div>;
41+
}
42+
function _temp(props_0) {
43+
console.log(props_0);
44+
}
45+
46+
```
47+
48+
### Eval output
49+
(kind: exception) Fixture not implemented
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @flow @enableEmitHookGuards @panicThreshold(none) @enableFire
2+
import {useEffect, fire} from 'react';
3+
4+
function Component(props, useDynamicHook) {
5+
'use memo';
6+
useDynamicHook();
7+
const foo = props => {
8+
console.log(props);
9+
};
10+
useEffect(() => {
11+
fire(foo(props));
12+
});
13+
14+
return <div>hello world</div>;
15+
}

0 commit comments

Comments
 (0)