-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] More complete validation against locals being reassigned a…
…fter render Summary: This diff extends the existing work on validating against locals being reassigned after render, by propagating the reassignment "effect" into the lvalues of instructions when the rvalue operands include values known to cause reassignments. In particular, this "closes the loop" for function definitions and function calls: a function that returns a function that reassigns will be considered to also perform reassignments, but previous to this we didn't consider the result of a `Call` of a function that reassigns to itself be a value that reassigns. This causes a number of new bailouts in test cases, all of which appear to me to be legit. ghstack-source-id: 770bf02d079ea2480be243a49caa6f69573d8092 Pull Request resolved: #30540
- Loading branch information
Showing
13 changed files
with
157 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
.../src/__tests__/fixtures/compiler/context-variable-only-chained-assign.expect.md
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
..._/fixtures/compiler/declare-reassign-variable-in-function-declaration.expect.md
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
..._tests__/fixtures/compiler/error.context-variable-only-chained-assign.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {identity, invoke} from 'shared-runtime'; | ||
|
||
function foo() { | ||
let x = 2; | ||
const fn1 = () => { | ||
const copy1 = (x = 3); | ||
return identity(copy1); | ||
}; | ||
const fn2 = () => { | ||
const copy2 = (x = 4); | ||
return [invoke(fn1), copy2, identity(copy2)]; | ||
}; | ||
return invoke(fn2); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
8 | }; | ||
9 | const fn2 = () => { | ||
> 10 | const copy2 = (x = 4); | ||
| ^ InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `x` cannot be reassigned after render (10:10) | ||
11 | return [invoke(fn1), copy2, identity(copy2)]; | ||
12 | }; | ||
13 | return invoke(fn2); | ||
``` | ||
File renamed without changes.
29 changes: 29 additions & 0 deletions
29
...ures/compiler/error.declare-reassign-variable-in-function-declaration.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
function Component() { | ||
let x = null; | ||
function foo() { | ||
x = 9; | ||
} | ||
const y = bar(foo); | ||
return <Child y={y} />; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
2 | let x = null; | ||
3 | function foo() { | ||
> 4 | x = 9; | ||
| ^ InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `x` cannot be reassigned after render (4:4) | ||
5 | } | ||
6 | const y = bar(foo); | ||
7 | return <Child y={y} />; | ||
``` | ||
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
44 changes: 44 additions & 0 deletions
44
...s__/fixtures/compiler/error.mutable-range-shared-inner-outer-function.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions | ||
let cond = true; | ||
function Component(props) { | ||
let a; | ||
let b; | ||
const f = () => { | ||
if (cond) { | ||
a = {}; | ||
b = []; | ||
} else { | ||
a = {}; | ||
b = []; | ||
} | ||
a.property = true; | ||
b.push(false); | ||
}; | ||
return <div onClick={f()} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
6 | const f = () => { | ||
7 | if (cond) { | ||
> 8 | a = {}; | ||
| ^ InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `a` cannot be reassigned after render (8:8) | ||
9 | b = []; | ||
10 | } else { | ||
11 | a = {}; | ||
``` | ||
File renamed without changes.
Oops, something went wrong.