mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Retain minimal variable declarations in DCE
Currently DCE can remove variable declarations that are unused, ie where all control-flow paths to usage of the variable are overwritten by a reassignment. We then have to reconstruct the original variable declaration at the appropriate block scope during LeaveSSA, which is complex and can actually be incorrect in some cases. This PR updates to ensure that DCE will not remove the original variable declaration for any variable that is used (even in the case of always being reassigned before use). The main changes are: * DCE retains variable declarations, but if a variable declaration is always shadowed by reassignments then DCE will rewrite StoreLocal -> DeclareLocal so that it can DCE the unused initial value. * BuildHIR now has to change its handling for reassignment destructure instructions with nesting. Nesting uses a temporary which would appear as a declaration of a new variable, which is incompatible with other reassignments. See comments in the file. * LeaveSSA is quite a bit simpler now, since we never need to reconstruct a declaration.
This commit is contained in:
1 parent
84da8994bd
commit
f8cee28f1d
54 files changed
+484
-279
No files matched your search
@@ -3204,11 +3204,19 @@ function lowerAssignment(
|
||||
}
|
||||
}
|
||||
case "ArrayPattern": {
|
||||
// TODO
|
||||
const lvalue = lvaluePath as NodePath<t.ArrayPattern>;
|
||||
const elements = lvalue.get("elements");
|
||||
const items: ArrayPattern["items"] = [];
|
||||
const followups: Array<{ place: Place; path: NodePath<t.LVal> }> = [];
|
||||
// A given destructuring statement must contain all declarations or all
|
||||
// reassignments. This is enforced by the parser, but we rewrite nested
|
||||
// destructuring into assignment to a temporary. Therefore, if we see
|
||||
// any reassignments that are nested destructuring we fall back to
|
||||
// using temporaries for all variables, and emitting the actual reassignments
|
||||
// in follow-up statements
|
||||
const forceTemporaries =
|
||||
kind === InstructionKind.Reassign &&
|
||||
elements.some((element) => !element.isIdentifier());
|
||||
for (let i = 0; i < elements.length; i++) {
|
||||
const element = elements[i];
|
||||
if (element.node == null) {
|
||||
@@ -3228,7 +3236,7 @@ function lowerAssignment(
|
||||
}
|
||||
if (element.isRestElement()) {
|
||||
const argument = element.get("argument");
|
||||
if (argument.isIdentifier()) {
|
||||
if (argument.isIdentifier() && !forceTemporaries) {
|
||||
const identifier = lowerIdentifierForAssignment(
|
||||
builder,
|
||||
element.node.loc ?? GeneratedSource,
|
||||
@@ -3253,7 +3261,7 @@ function lowerAssignment(
|
||||
});
|
||||
followups.push({ place: temp, path: argument as NodePath<t.LVal> }); // TODO remove type cast
|
||||
}
|
||||
} else if (element.isIdentifier()) {
|
||||
} else if (element.isIdentifier() && !forceTemporaries) {
|
||||
const identifier = lowerIdentifierForAssignment(
|
||||
builder,
|
||||
element.node.loc ?? GeneratedSource,
|
||||
@@ -3295,6 +3303,20 @@ function lowerAssignment(
|
||||
const propertiesPaths = lvalue.get("properties");
|
||||
const properties: ObjectPattern["properties"] = [];
|
||||
const followups: Array<{ place: Place; path: NodePath<t.LVal> }> = [];
|
||||
// A given destructuring statement must contain all declarations or all
|
||||
// reassignments. This is enforced by the parser, but we rewrite nested
|
||||
// destructuring into assignment to a temporary. Therefore, if we see
|
||||
// any reassignments that are nested destructuring we fall back to
|
||||
// using temporaries for all variables, and emitting the actual reassignments
|
||||
// in follow-up statements
|
||||
const forceTemporaries =
|
||||
kind === InstructionKind.Reassign &&
|
||||
propertiesPaths.some(
|
||||
(property) =>
|
||||
property.isRestElement() ||
|
||||
(property.isObjectProperty() &&
|
||||
!property.get("value").isIdentifier())
|
||||
);
|
||||
for (let i = 0; i < propertiesPaths.length; i++) {
|
||||
const property = propertiesPaths[i];
|
||||
if (property.isRestElement()) {
|
||||
@@ -3308,19 +3330,31 @@ function lowerAssignment(
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const identifier = lowerIdentifierForAssignment(
|
||||
builder,
|
||||
property.node.loc ?? GeneratedSource,
|
||||
kind,
|
||||
argument
|
||||
);
|
||||
if (identifier === null) {
|
||||
continue;
|
||||
if (forceTemporaries) {
|
||||
const temp = buildTemporaryPlace(
|
||||
builder,
|
||||
property.node.loc ?? GeneratedSource
|
||||
);
|
||||
properties.push({
|
||||
kind: "Spread",
|
||||
place: { ...temp },
|
||||
});
|
||||
followups.push({ place: temp, path: argument as NodePath<t.LVal> }); // TODO remove type cast
|
||||
} else {
|
||||
const identifier = lowerIdentifierForAssignment(
|
||||
builder,
|
||||
property.node.loc ?? GeneratedSource,
|
||||
kind,
|
||||
argument
|
||||
);
|
||||
if (identifier === null) {
|
||||
continue;
|
||||
}
|
||||
properties.push({
|
||||
kind: "Spread",
|
||||
place: identifier,
|
||||
});
|
||||
}
|
||||
properties.push({
|
||||
kind: "Spread",
|
||||
place: identifier,
|
||||
});
|
||||
} else {
|
||||
// TODO: this should always be true given the if/else
|
||||
if (!property.isObjectProperty()) {
|
||||
@@ -3355,7 +3389,7 @@ function lowerAssignment(
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (element.isIdentifier()) {
|
||||
if (element.isIdentifier() && !forceTemporaries) {
|
||||
const identifier = lowerIdentifierForAssignment(
|
||||
builder,
|
||||
element.node.loc ?? GeneratedSource,
|
||||
|
||||
+84
-22
@@ -10,7 +10,9 @@ import {
|
||||
BlockId,
|
||||
HIRFunction,
|
||||
Identifier,
|
||||
IdentifierId,
|
||||
Instruction,
|
||||
InstructionKind,
|
||||
InstructionValue,
|
||||
ObjectPattern,
|
||||
} from "../HIR";
|
||||
@@ -48,7 +50,7 @@ export function deadCodeElimination(fn: HIRFunction): void {
|
||||
for (let i = block.instructions.length - 1; i >= 0; i--) {
|
||||
const instr = block.instructions[i]!;
|
||||
if (
|
||||
!state.used(instr.lvalue.identifier) &&
|
||||
!state.isIdOrNameUsed(instr.lvalue.identifier) &&
|
||||
pruneableValue(instr.value, state) &&
|
||||
// Can't prune the last value of a value block, that's its value!
|
||||
!(block.kind !== "block" && i === block.instructions.length - 1)
|
||||
@@ -56,10 +58,20 @@ export function deadCodeElimination(fn: HIRFunction): void {
|
||||
continue;
|
||||
}
|
||||
state.reference(instr.lvalue.identifier);
|
||||
|
||||
// For the last value of a value block, if it's not pruneable we can't
|
||||
// rewrite it. This is necessary to preserve unused value blocks
|
||||
if (block.kind !== "block" && i === block.instructions.length - 1) {
|
||||
for (const place of eachInstructionValueOperand(instr.value)) {
|
||||
state.reference(place.identifier);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// Otherwise rewrite instructions to remove unused parts of them
|
||||
visitInstruction(instr, state);
|
||||
}
|
||||
for (const phi of block.phis) {
|
||||
if (state.used(phi.id)) {
|
||||
if (state.isIdOrNameUsed(phi.id)) {
|
||||
for (const [_pred, operand] of phi.operands) {
|
||||
state.reference(operand);
|
||||
}
|
||||
@@ -69,25 +81,42 @@ export function deadCodeElimination(fn: HIRFunction): void {
|
||||
} while (state.count > size && hasLoop);
|
||||
for (const [, block] of fn.body.blocks) {
|
||||
for (const phi of block.phis) {
|
||||
if (!state.used(phi.id)) {
|
||||
if (!state.isIdOrNameUsed(phi.id)) {
|
||||
block.phis.delete(phi);
|
||||
}
|
||||
}
|
||||
retainWhere(block.instructions, (instr) =>
|
||||
state.used(instr.lvalue.identifier)
|
||||
state.isIdOrNameUsed(instr.lvalue.identifier)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class State {
|
||||
identifiers: Set<Identifier> = new Set();
|
||||
named: Set<string> = new Set();
|
||||
identifiers: Set<IdentifierId> = new Set();
|
||||
|
||||
// Mark the identifier as being referenced (not dead code)
|
||||
reference(identifier: Identifier): void {
|
||||
this.identifiers.add(identifier);
|
||||
this.identifiers.add(identifier.id);
|
||||
if (identifier.name !== null) {
|
||||
this.named.add(identifier.name);
|
||||
}
|
||||
}
|
||||
|
||||
used(identifier: Identifier): boolean {
|
||||
return this.identifiers.has(identifier);
|
||||
// Check if any version of the given identifier is used somewhere.
|
||||
// This checks both for usage of this specific identifer id (ssa id)
|
||||
// and (for named identifiers) for any usages of that identifier name.
|
||||
isIdOrNameUsed(identifier: Identifier): boolean {
|
||||
return (
|
||||
this.identifiers.has(identifier.id) ||
|
||||
(identifier.name !== null && this.named.has(identifier.name))
|
||||
);
|
||||
}
|
||||
|
||||
// Like `used()`, but only checks for usages of this specific identifier id
|
||||
// (ssa id).
|
||||
isIdUsed(identifier: Identifier): boolean {
|
||||
return this.identifiers.has(identifier.id);
|
||||
}
|
||||
|
||||
get count(): number {
|
||||
@@ -110,12 +139,12 @@ function visitInstruction(instr: Instruction, state: State): void {
|
||||
for (let i = originalItems.length - 1; i >= 0; i--) {
|
||||
const item = originalItems[i];
|
||||
if (item.kind === "Identifier") {
|
||||
if (state.used(item.identifier)) {
|
||||
if (state.isIdOrNameUsed(item.identifier)) {
|
||||
nextItems = originalItems.slice(0, i + 1);
|
||||
break;
|
||||
}
|
||||
} else if (item.kind === "Spread") {
|
||||
if (state.used(item.place.identifier)) {
|
||||
if (state.isIdOrNameUsed(item.place.identifier)) {
|
||||
nextItems = originalItems.slice(0, i + 1);
|
||||
break;
|
||||
}
|
||||
@@ -135,12 +164,12 @@ function visitInstruction(instr: Instruction, state: State): void {
|
||||
let nextProperties: ObjectPattern["properties"] | null = null;
|
||||
for (const property of instr.value.lvalue.pattern.properties) {
|
||||
if (property.kind === "ObjectProperty") {
|
||||
if (state.used(property.place.identifier)) {
|
||||
if (state.isIdOrNameUsed(property.place.identifier)) {
|
||||
nextProperties ??= [];
|
||||
nextProperties.push(property);
|
||||
}
|
||||
} else {
|
||||
if (state.used(property.place.identifier)) {
|
||||
if (state.isIdOrNameUsed(property.place.identifier)) {
|
||||
nextProperties = null;
|
||||
break;
|
||||
}
|
||||
@@ -160,6 +189,25 @@ function visitInstruction(instr: Instruction, state: State): void {
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if (instr.value.kind === "StoreLocal") {
|
||||
if (
|
||||
instr.value.lvalue.kind !== InstructionKind.Reassign &&
|
||||
!state.isIdUsed(instr.value.lvalue.place.identifier)
|
||||
) {
|
||||
// This is a const/let declaration where the variable is accessed later,
|
||||
// but where the value is always overwritten before being read. Ie the
|
||||
// initializer value is never read. We rewrite to a DeclareLocal so
|
||||
// that the initializer value can be DCE'd
|
||||
instr.value = {
|
||||
kind: "DeclareLocal",
|
||||
lvalue: instr.value.lvalue,
|
||||
loc: instr.value.loc,
|
||||
};
|
||||
} else {
|
||||
// Else we mark the initializer as referenced, since the variable itself is
|
||||
// referenced
|
||||
state.reference(instr.value.value.identifier);
|
||||
}
|
||||
} else {
|
||||
for (const operand of eachInstructionValueOperand(instr.value)) {
|
||||
state.reference(operand.identifier);
|
||||
@@ -174,26 +222,40 @@ function visitInstruction(instr: Instruction, state: State): void {
|
||||
function pruneableValue(value: InstructionValue, state: State): boolean {
|
||||
switch (value.kind) {
|
||||
case "DeclareLocal": {
|
||||
return !state.used(value.lvalue.place.identifier);
|
||||
// Declarations are pruneable only if the named variable is never read later
|
||||
return !state.isIdOrNameUsed(value.lvalue.place.identifier);
|
||||
}
|
||||
case "StoreLocal": {
|
||||
// Stores are pruneable only if the identifier being stored to is never read later
|
||||
return !state.used(value.lvalue.place.identifier);
|
||||
if (value.lvalue.kind === InstructionKind.Reassign) {
|
||||
// Reassignments can be pruned if the specific instance being assigned is never read
|
||||
return !state.isIdUsed(value.lvalue.place.identifier);
|
||||
}
|
||||
// Declarations are pruneable only if the named variable is never read later
|
||||
return !state.isIdOrNameUsed(value.lvalue.place.identifier);
|
||||
}
|
||||
case "Destructure": {
|
||||
// Destructure is pruneable only if none of the identifiers are read from later
|
||||
// TODO: as an optimization, prune unused properties where safe
|
||||
let isIdOrNameUsed = false;
|
||||
let isIdUsed = false;
|
||||
for (const place of eachPatternOperand(value.lvalue.pattern)) {
|
||||
if (state.used(place.identifier)) {
|
||||
return false;
|
||||
if (state.isIdUsed(place.identifier)) {
|
||||
isIdOrNameUsed = true;
|
||||
isIdUsed = true;
|
||||
} else if (state.isIdOrNameUsed(place.identifier)) {
|
||||
isIdOrNameUsed = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
if (value.lvalue.kind === InstructionKind.Reassign) {
|
||||
// Reassignments can be pruned if the specific instance being assigned is never read
|
||||
return !isIdUsed;
|
||||
} else {
|
||||
// Otherwise pruneable only if none of the identifiers are read from later
|
||||
return !isIdOrNameUsed;
|
||||
}
|
||||
}
|
||||
case "PostfixUpdate":
|
||||
case "PrefixUpdate": {
|
||||
// Updates are pruneable only if the identifier being stored to is never read later
|
||||
return !state.used(value.lvalue.identifier);
|
||||
// Updates are pruneable if the specific instance instance being assigned is never read
|
||||
return !state.isIdUsed(value.lvalue.identifier);
|
||||
}
|
||||
case "Debugger": {
|
||||
// explicitly retain debugger statements to not break debugging workflows
|
||||
|
||||
@@ -9,11 +9,8 @@ import { CompilerError } from "../CompilerError";
|
||||
import {
|
||||
BasicBlock,
|
||||
BlockId,
|
||||
Effect,
|
||||
GeneratedSource,
|
||||
HIRFunction,
|
||||
Identifier,
|
||||
Instruction,
|
||||
InstructionKind,
|
||||
LValue,
|
||||
LValuePattern,
|
||||
@@ -420,88 +417,13 @@ export function leaveSSA(fn: HIRFunction): void {
|
||||
suggestions: null,
|
||||
});
|
||||
const declaration = declarations.get(phi.id.name);
|
||||
if (declaration === undefined) {
|
||||
let initValue: Place;
|
||||
if (initOperand === null) {
|
||||
initValue = {
|
||||
effect: Effect.Read,
|
||||
kind: "Identifier",
|
||||
loc: GeneratedSource,
|
||||
identifier: {
|
||||
id: fn.env.nextIdentifierId,
|
||||
name: null,
|
||||
mutableRange: {
|
||||
// TODO: this is technically the wrong start range; we do this because the instruction to create the
|
||||
// undefined and the instruction to store it to the identifier share an InstructionId, which makes
|
||||
// this value otherwise appear mutable when stored. All that matters is that the range end prior
|
||||
// to the StoreLocal's instruction id, so we decrement by one.
|
||||
start: makeInstructionId(block.terminal.id - 1),
|
||||
end: makeInstructionId(block.terminal.id),
|
||||
},
|
||||
scope: null,
|
||||
type: { kind: "Primitive" },
|
||||
},
|
||||
};
|
||||
block.instructions.push({
|
||||
id: block.terminal.id,
|
||||
lvalue: { ...initValue, effect: Effect.ConditionallyMutate },
|
||||
value: {
|
||||
kind: "Primitive",
|
||||
// TODO: consider leaving the variable uninitialized rather than explicitly undefined.
|
||||
value: undefined,
|
||||
loc: GeneratedSource,
|
||||
},
|
||||
loc: GeneratedSource,
|
||||
});
|
||||
} else {
|
||||
initValue = {
|
||||
kind: "Identifier",
|
||||
identifier: initOperand,
|
||||
effect: Effect.Capture,
|
||||
loc: GeneratedSource,
|
||||
};
|
||||
}
|
||||
const lvalue: LValue = {
|
||||
place: {
|
||||
kind: "Identifier",
|
||||
identifier: phi.id,
|
||||
effect: Effect.ConditionallyMutate,
|
||||
loc: GeneratedSource,
|
||||
},
|
||||
kind: InstructionKind.Let,
|
||||
};
|
||||
const instr: Instruction = {
|
||||
// NOTE: reuse the terminal id since these lets must be scoped with the terminal anyway.
|
||||
// the mutable range of this canonical id must by definition span from the binding (before
|
||||
// the if) to the phi, so it's safe to reuse the terminal's id.
|
||||
id: block.terminal.id,
|
||||
lvalue: {
|
||||
kind: "Identifier",
|
||||
identifier: {
|
||||
id: fn.env.nextIdentifierId,
|
||||
mutableRange: {
|
||||
start: block.terminal.id,
|
||||
end: makeInstructionId(block.terminal.id + 1),
|
||||
},
|
||||
name: null,
|
||||
scope: null,
|
||||
type: phi.id.type,
|
||||
},
|
||||
effect: Effect.ConditionallyMutate,
|
||||
loc: GeneratedSource,
|
||||
},
|
||||
value: {
|
||||
kind: "StoreLocal",
|
||||
lvalue,
|
||||
value: initValue,
|
||||
loc: GeneratedSource,
|
||||
},
|
||||
loc: GeneratedSource,
|
||||
};
|
||||
block.instructions.push(instr);
|
||||
declarations.set(phi.id.name, { lvalue, place: lvalue.place });
|
||||
phi.id.mutableRange.start = terminal.id;
|
||||
} else if (isPhiMutatedAfterCreation) {
|
||||
CompilerError.invariant(declaration != null, {
|
||||
loc: null,
|
||||
reason: "Expected a declaration for all variables",
|
||||
description: null,
|
||||
suggestions: null,
|
||||
});
|
||||
if (isPhiMutatedAfterCreation) {
|
||||
// The declaration is not guaranteed to flow into the phi, for example in the case of a variable
|
||||
// that is reassigned in all control flow paths to a given phi. The original declaration's range
|
||||
// has to be extended in this case (if the phi is later mutated) since we are reusing the original
|
||||
@@ -511,6 +433,7 @@ export function leaveSSA(fn: HIRFunction): void {
|
||||
// not prune. Otherwise, the declaration would have been pruned and we'd synthesize a new one.
|
||||
declaration.place.identifier.mutableRange.end = phi.id.mutableRange.end;
|
||||
}
|
||||
rewrites.set(phi.id, declaration.place.identifier);
|
||||
}
|
||||
|
||||
// Similar logic for rewrite phis that occur in loops, except that instead of a new let binding
|
||||
|
||||
+3
-1
@@ -22,7 +22,9 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function f() {
|
||||
const x = 3 >>> 1;
|
||||
let x;
|
||||
|
||||
x = 3 >>> 1;
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -33,10 +33,9 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(1);
|
||||
|
||||
let x;
|
||||
let i = 0;
|
||||
do {
|
||||
let x = undefined;
|
||||
if (i > 10) {
|
||||
x = 10;
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(1);
|
||||
let x = undefined;
|
||||
let x;
|
||||
if (props.cond) {
|
||||
x = 1;
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(1);
|
||||
let x = undefined;
|
||||
let x;
|
||||
bb1: switch (props.cond) {
|
||||
case true: {
|
||||
x = 1;
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ const GLOBAL = 42;
|
||||
function Component(t14) {
|
||||
const $ = useMemoCache(1);
|
||||
const { value } = t14;
|
||||
let x = undefined;
|
||||
let x;
|
||||
bb1: switch (GLOBAL) {
|
||||
case value: {
|
||||
x = 1;
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ function getNativeLogFunction(level) {
|
||||
let t0;
|
||||
if ($[0] !== level) {
|
||||
t0 = function () {
|
||||
let str = undefined;
|
||||
let str;
|
||||
if (arguments.length === 1 && typeof arguments[0] === "string") {
|
||||
str = arguments[0];
|
||||
} else {
|
||||
|
||||
+2
-1
@@ -36,6 +36,7 @@ function component(a) {
|
||||
t0 = $[1];
|
||||
}
|
||||
const z = t0;
|
||||
let x;
|
||||
let t1;
|
||||
if ($[2] !== z) {
|
||||
t1 = function () {
|
||||
@@ -46,7 +47,7 @@ function component(a) {
|
||||
} else {
|
||||
t1 = $[3];
|
||||
}
|
||||
const x = t1;
|
||||
x = t1;
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function useFoo(setOne: boolean) {
|
||||
let x;
|
||||
let y;
|
||||
let z;
|
||||
if (setOne) {
|
||||
x = y = z = 1;
|
||||
} else {
|
||||
x = 2;
|
||||
y = 3;
|
||||
z = 5;
|
||||
}
|
||||
return { x, y, z };
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: useFoo,
|
||||
params: [true],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function useFoo(setOne) {
|
||||
const $ = useMemoCache(1);
|
||||
let x;
|
||||
let y;
|
||||
let z;
|
||||
if (setOne) {
|
||||
x = y = z = 1;
|
||||
} else {
|
||||
x = 2;
|
||||
y = 3;
|
||||
z = 5;
|
||||
}
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = { x, y, z };
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: useFoo,
|
||||
params: [true],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ function useFoo(setOne: boolean) {
|
||||
return { x, y, z };
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPONT = {
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: useFoo,
|
||||
params: [true],
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let i = 0;
|
||||
i++;
|
||||
i = props.i;
|
||||
return i;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ i: 42 }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let i;
|
||||
|
||||
i = props.i;
|
||||
return i;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ i: 42 }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
function Component(props) {
|
||||
let i = 0;
|
||||
i++;
|
||||
i = props.i;
|
||||
return i;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ i: 42 }],
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let i = 0;
|
||||
--i;
|
||||
i = props.i;
|
||||
return i;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ i: 42 }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let i;
|
||||
|
||||
i = props.i;
|
||||
return i;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ i: 42 }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
function Component(props) {
|
||||
let i = 0;
|
||||
--i;
|
||||
i = props.i;
|
||||
return i;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{ i: 42 }],
|
||||
};
|
||||
+3
-1
@@ -22,7 +22,9 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function foo(props) {
|
||||
let { x, y } = { x: props.a, y: props.b };
|
||||
let x;
|
||||
let y;
|
||||
({ x, y } = { x: props.a, y: props.b });
|
||||
console.log(x);
|
||||
x = props.c;
|
||||
return x + y;
|
||||
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function useFoo(props: {
|
||||
x?: string;
|
||||
y?: string;
|
||||
z?: string;
|
||||
doDestructure: boolean;
|
||||
}) {
|
||||
let x = null;
|
||||
let y = null;
|
||||
let z = null;
|
||||
const myList = [];
|
||||
if (props.doDestructure) {
|
||||
({ x, y, z } = props);
|
||||
|
||||
myList.push(z);
|
||||
}
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
myList,
|
||||
};
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: useFoo,
|
||||
params: [{ x: "hello", y: "world", doDestructure: true }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function useFoo(props) {
|
||||
const $ = useMemoCache(9);
|
||||
|
||||
let x = null;
|
||||
let y = null;
|
||||
let z;
|
||||
let myList;
|
||||
if ($[0] !== props) {
|
||||
myList = [];
|
||||
if (props.doDestructure) {
|
||||
({ x, y, z } = props);
|
||||
|
||||
myList.push(z);
|
||||
}
|
||||
$[0] = props;
|
||||
$[1] = myList;
|
||||
$[2] = x;
|
||||
$[3] = y;
|
||||
$[4] = z;
|
||||
} else {
|
||||
myList = $[1];
|
||||
x = $[2];
|
||||
y = $[3];
|
||||
z = $[4];
|
||||
}
|
||||
let t0;
|
||||
if ($[5] !== x || $[6] !== y || $[7] !== myList) {
|
||||
t0 = { x, y, myList };
|
||||
$[5] = x;
|
||||
$[6] = y;
|
||||
$[7] = myList;
|
||||
$[8] = t0;
|
||||
} else {
|
||||
t0 = $[8];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: useFoo,
|
||||
params: [{ x: "hello", y: "world", doDestructure: true }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ function useFoo(props: {
|
||||
};
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPONT = {
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: useFoo,
|
||||
params: [{ x: "hello", y: "world", doDestructure: true }],
|
||||
};
|
||||
+1
-1
@@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(2);
|
||||
let x = undefined;
|
||||
let x;
|
||||
if (props.cond) {
|
||||
const [t0] = props.y;
|
||||
let t1;
|
||||
|
||||
+14
-9
@@ -35,15 +35,20 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function foo(a, b, c) {
|
||||
const $ = useMemoCache(5);
|
||||
|
||||
const [d, t46] = a;
|
||||
const [t48] = t46;
|
||||
const { e: t50 } = t48;
|
||||
const { f: g } = t50;
|
||||
const { l: t55, o } = b;
|
||||
const { m: t58 } = t55;
|
||||
const [t60] = t58;
|
||||
const [n] = t60;
|
||||
let d;
|
||||
let g;
|
||||
let n;
|
||||
let o;
|
||||
const [t49, t50] = a;
|
||||
d = t49;
|
||||
const [t54] = t50;
|
||||
const { e: t56 } = t54;
|
||||
({ f: g } = t56);
|
||||
const { l: t61, o: t62 } = b;
|
||||
const { m: t64 } = t61;
|
||||
const [t66] = t64;
|
||||
[n] = t66;
|
||||
o = t62;
|
||||
let t0;
|
||||
if ($[0] !== d || $[1] !== g || $[2] !== n || $[3] !== o) {
|
||||
t0 = { d, g, n, o };
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function useFoo(setOne: boolean) {
|
||||
let x;
|
||||
let y;
|
||||
let z;
|
||||
if (setOne) {
|
||||
x = y = z = 1;
|
||||
} else {
|
||||
x = 2;
|
||||
y = 3;
|
||||
z = 5;
|
||||
}
|
||||
return { x, y, z };
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPONT = {
|
||||
fn: useFoo,
|
||||
params: [true],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Error
|
||||
|
||||
```
|
||||
[ReactForget] Invariant: Const declaration cannot be referenced as an expression (6:6)
|
||||
```
|
||||
|
||||
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function useFoo(props: {
|
||||
x?: string;
|
||||
y?: string;
|
||||
z?: string;
|
||||
doDestructure: boolean;
|
||||
}) {
|
||||
let x = null;
|
||||
let y = null;
|
||||
let z = null;
|
||||
const myList = [];
|
||||
if (props.doDestructure) {
|
||||
({ x, y, z } = props);
|
||||
|
||||
myList.push(z);
|
||||
}
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
myList,
|
||||
};
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPONT = {
|
||||
fn: useFoo,
|
||||
params: [{ x: "hello", y: "world", doDestructure: true }],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Error
|
||||
|
||||
```
|
||||
[ReactForget] Invariant: Expected consistent kind for destructuring. Other places were 'Reassign' but 'store z$44' is const (12:12)
|
||||
```
|
||||
|
||||
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function f(reader) {
|
||||
const queue = [1, 2, 3];
|
||||
let value = 0;
|
||||
let sum = 0;
|
||||
// BUG: we need to codegen the complex test expression
|
||||
while ((value = queue.pop()) != null) {
|
||||
sum += value;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Error
|
||||
|
||||
```
|
||||
[ReactForget] Invariant: TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE) (6:6)
|
||||
```
|
||||
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
```javascript
|
||||
function Component(props) {
|
||||
const x = [props.a];
|
||||
let y = undefined;
|
||||
let y;
|
||||
if (x) {
|
||||
y = props.b;
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
```javascript
|
||||
function useFoo() {
|
||||
for (let i = 0; i <= 5; i++) {
|
||||
let color = undefined;
|
||||
let color;
|
||||
if (isSelected) {
|
||||
color = isCurrent ? "#FFCC22" : "#FF5050";
|
||||
} else {
|
||||
|
||||
+2
-1
@@ -24,6 +24,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function component() {
|
||||
const $ = useMemoCache(1);
|
||||
let x;
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = function x() {};
|
||||
@@ -31,7 +32,7 @@ function component() {
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
const x = t0;
|
||||
x = t0;
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -35,7 +35,9 @@ function hoisting() {
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
const qux = () => {
|
||||
const result = foo();
|
||||
let result;
|
||||
|
||||
result = foo();
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ function Component(props) {
|
||||
const $ = useMemoCache(1);
|
||||
const cond = props.cond;
|
||||
const x = props.x;
|
||||
let a = undefined;
|
||||
let a;
|
||||
if (cond) {
|
||||
a = x;
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function foo(a, b) {
|
||||
let x = undefined;
|
||||
let x;
|
||||
if (a) {
|
||||
x = 1;
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function foo(a, b, c) {
|
||||
let x = undefined;
|
||||
let x;
|
||||
bb1: {
|
||||
if (a) {
|
||||
x = b;
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ function foo(a, b, c, d) {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function foo(a, b, c, d) {
|
||||
const $ = useMemoCache(4);
|
||||
let x = undefined;
|
||||
let x;
|
||||
if (someVal) {
|
||||
let t0;
|
||||
if ($[0] !== b) {
|
||||
|
||||
-1
@@ -24,7 +24,6 @@ function foo(a, b, c, d) {
|
||||
const $ = useMemoCache(3);
|
||||
let x;
|
||||
if ($[0] !== b || $[1] !== c) {
|
||||
x = undefined;
|
||||
if (someVal) {
|
||||
x = { b };
|
||||
} else {
|
||||
|
||||
-1
@@ -27,7 +27,6 @@ function foo(a, b, c, d) {
|
||||
someObj();
|
||||
let x;
|
||||
if ($[0] !== a) {
|
||||
x = undefined;
|
||||
if (a) {
|
||||
const y = someObj();
|
||||
const z = y;
|
||||
|
||||
-1
@@ -25,7 +25,6 @@ function foo(a, b, c, d) {
|
||||
someObj();
|
||||
let x;
|
||||
if ($[0] !== a) {
|
||||
x = undefined;
|
||||
if (a) {
|
||||
x = someObj();
|
||||
} else {
|
||||
|
||||
+1
-2
@@ -33,9 +33,8 @@ function foo(a, b, c, d) {
|
||||
someObj();
|
||||
let x;
|
||||
if ($[0] !== a || $[1] !== b) {
|
||||
x = undefined;
|
||||
if (a) {
|
||||
let z = undefined;
|
||||
let z;
|
||||
if (b) {
|
||||
const w = someObj();
|
||||
z = w;
|
||||
|
||||
+1
-3
@@ -2,7 +2,6 @@
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
// @debug
|
||||
function Component(props) {
|
||||
const x = {};
|
||||
let y;
|
||||
@@ -30,14 +29,13 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { unstable_useMemoCache as useMemoCache } from "react"; // @debug
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(6);
|
||||
let x;
|
||||
let y;
|
||||
if ($[0] !== props) {
|
||||
x = {};
|
||||
y = undefined;
|
||||
if (props.cond) {
|
||||
y = [props.value];
|
||||
} else {
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
// @debug
|
||||
function Component(props) {
|
||||
const x = {};
|
||||
let y;
|
||||
|
||||
-1
@@ -43,7 +43,6 @@ function Component(props) {
|
||||
const x = t0;
|
||||
let y;
|
||||
if ($[1] !== props || $[2] !== x) {
|
||||
y = undefined;
|
||||
if (props.cond) {
|
||||
y = {};
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ function component(a) {
|
||||
|
||||
```javascript
|
||||
function component(a) {
|
||||
let x = undefined;
|
||||
let x;
|
||||
if (a) {
|
||||
x = "bar";
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ function Component(props) {
|
||||
let t0;
|
||||
if ($[0] !== props.str) {
|
||||
t0 = () => {
|
||||
let str = undefined;
|
||||
let str;
|
||||
if (arguments.length) {
|
||||
str = arguments[0];
|
||||
} else {
|
||||
|
||||
+5
-2
@@ -22,18 +22,21 @@ function foo() {
|
||||
import { unstable_useMemoCache as useMemoCache } from "react";
|
||||
function mutate() {}
|
||||
function foo() {
|
||||
const $ = useMemoCache(1);
|
||||
const $ = useMemoCache(2);
|
||||
let a;
|
||||
let c;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
let b = {};
|
||||
c = {};
|
||||
const a = b;
|
||||
a = b;
|
||||
b = c;
|
||||
c = a;
|
||||
mutate(a, b);
|
||||
$[0] = c;
|
||||
$[1] = a;
|
||||
} else {
|
||||
c = $[0];
|
||||
a = $[1];
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
+3
-1
@@ -34,7 +34,9 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function foo(a, b, c, d) {
|
||||
const x = a;
|
||||
let x;
|
||||
|
||||
x = a;
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -22,7 +22,9 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function foo(a, b, c) {
|
||||
const x = c;
|
||||
let x;
|
||||
|
||||
x = c;
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -20,8 +20,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function ternary(props) {
|
||||
let x = undefined;
|
||||
|
||||
let x;
|
||||
const y = props.a ? (x = 1) : (x = 2);
|
||||
return x + y;
|
||||
}
|
||||
|
||||
+1
@@ -26,6 +26,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let x;
|
||||
return 42;
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -26,6 +26,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let x;
|
||||
return 42;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ const { throwErrorWithMessage } = require("shared-runtime");
|
||||
|
||||
function Component(props) {
|
||||
const $ = useMemoCache(1);
|
||||
let x = undefined;
|
||||
let x;
|
||||
try {
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
|
||||
+1
-2
@@ -20,8 +20,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let x = undefined;
|
||||
|
||||
let x;
|
||||
((x = 1), 1) && (x = 2);
|
||||
return x;
|
||||
}
|
||||
|
||||
+1
-2
@@ -20,8 +20,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
let x = undefined;
|
||||
|
||||
let x;
|
||||
props.cond ? (x = 1) : (x = 2);
|
||||
return x;
|
||||
}
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ export const FIXTURE_ENTRYPOINT = {
|
||||
function Component(props) {
|
||||
let t49;
|
||||
bb11: {
|
||||
let y = undefined;
|
||||
let y;
|
||||
bb2: switch (props.switch) {
|
||||
case "foo": {
|
||||
t49 = "foo";
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function Component() {
|
||||
const queue = [1, 2, 3];
|
||||
let value = 0;
|
||||
let sum = 0;
|
||||
while ((value = queue.pop()) != null) {
|
||||
sum += value;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Component() {
|
||||
const queue = [1, 2, 3];
|
||||
let value;
|
||||
let sum = 0;
|
||||
while ((value = queue.pop()) != null) {
|
||||
sum = sum + value;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+6
-2
@@ -1,10 +1,14 @@
|
||||
function f(reader) {
|
||||
function Component() {
|
||||
const queue = [1, 2, 3];
|
||||
let value = 0;
|
||||
let sum = 0;
|
||||
// BUG: we need to codegen the complex test expression
|
||||
while ((value = queue.pop()) != null) {
|
||||
sum += value;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [],
|
||||
};
|
||||
@@ -416,8 +416,6 @@ const skipFilter = new Set([
|
||||
"readonly-object-method-calls",
|
||||
"readonly-object-method-calls-mutable-lambda",
|
||||
|
||||
"bug.reactive-control-dependency-do-while-test",
|
||||
|
||||
// TODO: 🌲
|
||||
"forest-basic",
|
||||
|
||||
|
||||
Reference in new issue
Block a user