Skip to content
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

[compiler] Fix assignment within for update expression #30067

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,26 @@ class Driver {
});
} else if (defaultBlock.instructions.length === 1) {
const instr = defaultBlock.instructions[0]!;
let place: Place = instr.lvalue!;
let place: Place = instr.lvalue;
let value: ReactiveValue = instr.value;
if (instr.value.kind === "StoreLocal") {
place = instr.value.lvalue.place;
if (
/*
* Value blocks generally end in a StoreLocal to assign the value of the
* expression for this branch. These StoreLocal instructions can be pruned,
* since we represent the value blocks as a compund value in ReactiveFunction
* (no phis). However, it's also possible to have a value block that ends in
* an AssignmentExpression, which we need to keep. So we only prune
* StoreLocal for temporaries — any named/promoted values must be used
* elsewhere and aren't safe to prune.
*/
value.kind === "StoreLocal" &&
value.lvalue.place.identifier.name === null
) {
place = value.lvalue.place;
value = {
kind: "LoadLocal",
place: instr.value.value,
loc: instr.value.value.loc,
place: value.value,
loc: value.value.loc,
};
}
return {
Expand All @@ -939,14 +951,26 @@ class Driver {
};
} else {
const instr = defaultBlock.instructions.at(-1)!;
let place: Place = instr.lvalue!;
let place: Place = instr.lvalue;
let value: ReactiveValue = instr.value;
if (instr.value.kind === "StoreLocal") {
place = instr.value.lvalue.place;
if (
/*
* Value blocks generally end in a StoreLocal to assign the value of the
* expression for this branch. These StoreLocal instructions can be pruned,
* since we represent the value blocks as a compund value in ReactiveFunction
* (no phis). However, it's also possible to have a value block that ends in
* an AssignmentExpression, which we need to keep. So we only prune
* StoreLocal for temporaries — any named/promoted values must be used
* elsewhere and aren't safe to prune.
*/
value.kind === "StoreLocal" &&
value.lvalue.place.identifier.name === null
) {
place = value.lvalue.place;
value = {
kind: "LoadLocal",
place: instr.value.value,
loc: instr.value.value.loc,
place: value.value,
loc: value.value.loc,
};
}
const sequence: ReactiveSequenceValue = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

## Input

```javascript
function Component(props) {
let x = props.init;
for (let i = 0; i < 100; i = i + 1) {
x += i;
}
return [x];
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ init: 0 }],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
function Component(props) {
const $ = _c(2);
let x = props.init;
for (let i = 0; i < 100; i = i + 1) {
x = x + i;
}
let t0;
if ($[0] !== x) {
t0 = [x];
$[0] = x;
$[1] = t0;
} else {
t0 = $[1];
}
return t0;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ init: 0 }],
};

```

### Eval output
(kind: ok) [4950]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function Component(props) {
let x = props.init;
for (let i = 0; i < 100; i = i + 1) {
x += i;
}
return [x];
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ init: 0 }],
};