Skip to content

Commit 0da1f32

Browse files
committed
[compiler] Fix merging of queues states in InferReferenceEffects
Fixes a bug found by @mofeiZ in #29878. When we merge queues states, if the new state does not introduce changes relative to the queued state we should use the queued state, not the new state. [ghstack-poisoned]
1 parent 195d5bb commit 0da1f32

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "../HIR/HIR";
3535
import { FunctionSignature } from "../HIR/ObjectShape";
3636
import {
37+
printFunction,
3738
printIdentifier,
3839
printMixedHIR,
3940
printPlace,
@@ -201,7 +202,7 @@ export default function inferReferenceEffects(
201202
let queuedState = queuedStates.get(blockId);
202203
if (queuedState != null) {
203204
// merge the queued states for this block
204-
state = queuedState.merge(state) ?? state;
205+
state = queuedState.merge(state) ?? queuedState;
205206
queuedStates.set(blockId, state);
206207
} else {
207208
/*
@@ -765,7 +766,7 @@ class InferenceState {
765766
result.values[id] = { kind, value: printMixedHIR(value) };
766767
}
767768
for (const [variable, values] of this.#variables) {
768-
result.variables[variable] = [...values].map(identify);
769+
result.variables[`$${variable}`] = [...values].map(identify);
769770
}
770771
return result;
771772
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
## Input
3+
4+
```javascript
5+
import { arrayPush } from "shared-runtime";
6+
7+
function Foo(cond) {
8+
let x = null;
9+
if (cond) {
10+
x = [];
11+
} else {
12+
}
13+
// Here, x = phi(x$null, x$[]) does not receive the correct ValueKind
14+
arrayPush(x, 2);
15+
16+
return x;
17+
}
18+
19+
export const FIXTURE_ENTRYPOINT = {
20+
fn: Foo,
21+
params: [{ cond: true }],
22+
sequentialRenders: [{ cond: true }, { cond: true }],
23+
};
24+
25+
```
26+
27+
## Code
28+
29+
```javascript
30+
import { c as _c } from "react/compiler-runtime";
31+
import { arrayPush } from "shared-runtime";
32+
33+
function Foo(cond) {
34+
const $ = _c(2);
35+
let x;
36+
if ($[0] !== cond) {
37+
x = null;
38+
if (cond) {
39+
x = [];
40+
}
41+
42+
arrayPush(x, 2);
43+
$[0] = cond;
44+
$[1] = x;
45+
} else {
46+
x = $[1];
47+
}
48+
return x;
49+
}
50+
51+
export const FIXTURE_ENTRYPOINT = {
52+
fn: Foo,
53+
params: [{ cond: true }],
54+
sequentialRenders: [{ cond: true }, { cond: true }],
55+
};
56+
57+
```
58+
59+
### Eval output
60+
(kind: ok) [2]
61+
[2]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { arrayPush } from "shared-runtime";
2+
3+
function Foo(cond) {
4+
let x = null;
5+
if (cond) {
6+
x = [];
7+
} else {
8+
}
9+
// Here, x = phi(x$null, x$[]) does not receive the correct ValueKind
10+
arrayPush(x, 2);
11+
12+
return x;
13+
}
14+
15+
export const FIXTURE_ENTRYPOINT = {
16+
fn: Foo,
17+
params: [{ cond: true }],
18+
sequentialRenders: [{ cond: true }, { cond: true }],
19+
};

0 commit comments

Comments
 (0)