Skip to content

Commit c828e91

Browse files
committed
[compiler] Do not inline IIFEs in value blocks
As discussed in chat, this is a simple fix to stop introducing labels inside expressions. The useMemo-with-optional test was added in d70b2c2 and crashes for the same reason- an unexpected label as a value block terminal.
1 parent 299762d commit c828e91

8 files changed

+111
-72
lines changed

compiler/packages/babel-plugin-react-compiler/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ export function inlineImmediatelyInvokedFunctionExpressions(
9090
*/
9191
const queue = Array.from(fn.body.blocks.values());
9292
queue: for (const block of queue) {
93+
/* We can't handle labels inside expressions yet, so we don't inline IIFEs if they are in a
94+
* value block.
95+
*/
96+
if (block.kind === 'value') {
97+
continue queue;
98+
}
9399
for (let ii = 0; ii < block.instructions.length; ii++) {
94100
const instr = block.instructions[ii]!;
95101
switch (instr.value.kind) {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-iife-inline-ternary.expect.md

Lines changed: 0 additions & 33 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-useMemo-with-optional.expect.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-useMemo-with-optional.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
## Input
3+
4+
```javascript
5+
function Component(props) {
6+
const x = props.foo
7+
? 1
8+
: (() => {
9+
throw new Error('Did not receive 1');
10+
})();
11+
return items;
12+
}
13+
14+
export const FIXTURE_ENTRYPOINT = {
15+
fn: Component,
16+
params: [{foo: true}],
17+
};
18+
19+
```
20+
21+
## Code
22+
23+
```javascript
24+
function Component(props) {
25+
props.foo ? 1 : _temp();
26+
return items;
27+
}
28+
function _temp() {
29+
throw new Error("Did not receive 1");
30+
}
31+
32+
export const FIXTURE_ENTRYPOINT = {
33+
fn: Component,
34+
params: [{ foo: true }],
35+
};
36+
37+
```
38+
39+
### Eval output
40+
(kind: exception) items is not defined
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ function Component(props) {
66
})();
77
return items;
88
}
9+
10+
export const FIXTURE_ENTRYPOINT = {
11+
fn: Component,
12+
params: [{foo: true}],
13+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {useMemo} from 'react';
6+
function Component(props) {
7+
return (
8+
useMemo(() => {
9+
return [props.value];
10+
}) || []
11+
);
12+
}
13+
14+
export const FIXTURE_ENTRYPOINT = {
15+
fn: Component,
16+
params: [{value: 1}],
17+
};
18+
19+
```
20+
21+
## Code
22+
23+
```javascript
24+
import { c as _c } from "react/compiler-runtime";
25+
import { useMemo } from "react";
26+
function Component(props) {
27+
const $ = _c(2);
28+
let t0;
29+
if ($[0] !== props.value) {
30+
t0 = (() => [props.value])() || [];
31+
$[0] = props.value;
32+
$[1] = t0;
33+
} else {
34+
t0 = $[1];
35+
}
36+
return t0;
37+
}
38+
39+
export const FIXTURE_ENTRYPOINT = {
40+
fn: Component,
41+
params: [{ value: 1 }],
42+
};
43+
44+
```
45+
46+
### Eval output
47+
(kind: ok) [1]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {useMemo} from 'react';
2+
function Component(props) {
3+
return (
4+
useMemo(() => {
5+
return [props.value];
6+
}) || []
7+
);
8+
}
9+
10+
export const FIXTURE_ENTRYPOINT = {
11+
fn: Component,
12+
params: [{value: 1}],
13+
};

0 commit comments

Comments
 (0)