Skip to content

Commit

Permalink
[compiler] patch: retain UpdateExpression for globals
Browse files Browse the repository at this point in the history
ghstack-source-id: aff6606f85698ccda13d1deffb6faa9d91f9821a
Pull Request resolved: #30471
  • Loading branch information
mofeiZ committed Jul 26, 2024
1 parent d27617f commit 5d4a27e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 56 deletions.
53 changes: 32 additions & 21 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,6 @@ function lowerExpression(
switch (leftNode.type) {
case 'Identifier': {
const leftExpr = left as NodePath<t.Identifier>;
const identifier = lowerIdentifier(builder, leftExpr);
const leftPlace = lowerExpressionToTemporary(builder, leftExpr);
const right = lowerExpressionToTemporary(builder, expr.get('right'));
const binaryPlace = lowerValueToTemporary(builder, {
Expand All @@ -1944,30 +1943,42 @@ function lowerExpression(
right,
loc: exprLoc,
});
const kind = getStoreKind(builder, leftExpr);
if (kind === 'StoreLocal') {
lowerValueToTemporary(builder, {
kind: 'StoreLocal',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
value: {...binaryPlace},
type: null,
loc: exprLoc,
});
return {kind: 'LoadLocal', place: identifier, loc: exprLoc};
const binding = builder.resolveIdentifier(leftExpr);
if (binding.kind === 'Identifier') {
const identifier = lowerIdentifier(builder, leftExpr);
const kind = getStoreKind(builder, leftExpr);
if (kind === 'StoreLocal') {
lowerValueToTemporary(builder, {
kind: 'StoreLocal',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
value: {...binaryPlace},
type: null,
loc: exprLoc,
});
return {kind: 'LoadLocal', place: identifier, loc: exprLoc};
} else {
lowerValueToTemporary(builder, {
kind: 'StoreContext',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
value: {...binaryPlace},
loc: exprLoc,
});
return {kind: 'LoadContext', place: identifier, loc: exprLoc};
}
} else {
lowerValueToTemporary(builder, {
kind: 'StoreContext',
lvalue: {
place: {...identifier},
kind: InstructionKind.Reassign,
},
const temporary = lowerValueToTemporary(builder, {
kind: 'StoreGlobal',
name: leftExpr.node.name,
value: {...binaryPlace},
loc: exprLoc,
});
return {kind: 'LoadContext', place: identifier, loc: exprLoc};
return {kind: 'LoadLocal', place: temporary, loc: temporary.loc};
}
}
case 'MemberExpression': {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

## Input

```javascript
let renderCount = 0;
function useFoo() {
renderCount += 1;
return renderCount;
}

export const FIXTURE_ENTRYPOINT = {
fn: useFoo,
params: [],
};

```


## Error

```
1 | let renderCount = 0;
2 | function useFoo() {
> 3 | renderCount += 1;
| ^^^^^^^^^^^^^^^^ InvalidReact: Unexpected reassignment of a variable which was defined outside of the component. Components and hooks should be pure and side-effect free, but variable reassignment is a form of side-effect. If this variable is used in rendering, use useState instead. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render) (3:3)
4 | return renderCount;
5 | }
6 |
```
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function Foo() {
return t0;
}
function _temp() {
renderCount = renderCount + 1;
return renderCount;
}

Expand All @@ -49,4 +50,6 @@ export const FIXTURE_ENTRYPOINT = {
};

```
### Eval output
(kind: ok) <div>{"cb":{"kind":"Function","result":1},"shouldInvokeFns":true}</div>
2 changes: 0 additions & 2 deletions compiler/packages/snap/src/SproutTodoFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,6 @@ const skipFilter = new Set([
'rules-of-hooks/rules-of-hooks-69521d94fa03',

// bugs
'bug-invalid-update-global-should-bailout',
'bug-update-global-in-callback',
'bug-invalid-hoisting-functionexpr',
'original-reactive-scopes-fork/bug-nonmutating-capture-in-unsplittable-memo-block',
'original-reactive-scopes-fork/bug-hoisted-declaration-with-scope',
Expand Down

0 comments on commit 5d4a27e

Please sign in to comment.