-
Notifications
You must be signed in to change notification settings - Fork 48.3k
/
Copy pathValidateLocalsNotReassignedAfterRender.ts
165 lines (162 loc) · 5.7 KB
/
ValidateLocalsNotReassignedAfterRender.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { CompilerError, Effect } from "..";
import { HIRFunction, IdentifierId, Place } from "../HIR";
import {
eachInstructionValueOperand,
eachTerminalOperand,
} from "../HIR/visitors";
/**
* Validates that local variables cannot be reassigned after render.
* This prevents a category of bugs in which a closure captures a
* binding from one render but does not update
*/
export function validateLocalsNotReassignedAfterRender(fn: HIRFunction): void {
const contextVariables = new Set<IdentifierId>();
const reassignment = getContextReassignment(
fn,
contextVariables,
false,
false
);
if (reassignment !== null) {
CompilerError.throwInvalidReact({
reason:
"Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead",
description:
reassignment.identifier.name !== null &&
reassignment.identifier.name.kind === "named"
? `Variable \`${reassignment.identifier.name.value}\` cannot be reassigned after render`
: "",
loc: reassignment.loc,
});
}
}
function getContextReassignment(
fn: HIRFunction,
contextVariables: Set<IdentifierId>,
isFunctionExpression: boolean,
isAsync: boolean
): Place | null {
const reassigningFunctions = new Map<IdentifierId, Place>();
for (const [, block] of fn.body.blocks) {
for (const instr of block.instructions) {
const { lvalue, value } = instr;
switch (value.kind) {
case "FunctionExpression":
case "ObjectMethod": {
let reassignment = getContextReassignment(
value.loweredFunc.func,
contextVariables,
true,
isAsync || value.loweredFunc.func.async
);
if (reassignment === null) {
// If the function itself doesn't reassign, does one of its dependencies?
for (const operand of eachInstructionValueOperand(value)) {
const reassignmentFromOperand = reassigningFunctions.get(
operand.identifier.id
);
if (reassignmentFromOperand !== undefined) {
reassignment = reassignmentFromOperand;
break;
}
}
}
// if the function or its depends reassign, propagate that fact on the lvalue
if (reassignment !== null) {
if (isAsync || value.loweredFunc.func.async) {
CompilerError.throwInvalidReact({
reason:
"Reassigning a variable in an async function can cause inconsistent behavior on subsequent renders. Consider using state instead",
description:
reassignment.identifier.name !== null &&
reassignment.identifier.name.kind === "named"
? `Variable \`${reassignment.identifier.name.value}\` cannot be reassigned after render`
: "",
loc: reassignment.loc,
});
}
reassigningFunctions.set(lvalue.identifier.id, reassignment);
}
break;
}
case "StoreLocal": {
const reassignment = reassigningFunctions.get(
value.value.identifier.id
);
if (reassignment !== undefined) {
reassigningFunctions.set(
value.lvalue.place.identifier.id,
reassignment
);
reassigningFunctions.set(lvalue.identifier.id, reassignment);
}
break;
}
case "LoadLocal": {
const reassignment = reassigningFunctions.get(
value.place.identifier.id
);
if (reassignment !== undefined) {
reassigningFunctions.set(lvalue.identifier.id, reassignment);
}
break;
}
case "DeclareContext": {
if (!isFunctionExpression) {
contextVariables.add(value.lvalue.place.identifier.id);
}
break;
}
case "StoreContext": {
if (isFunctionExpression) {
if (contextVariables.has(value.lvalue.place.identifier.id)) {
return value.lvalue.place;
}
} else {
/*
* We only track reassignments of variables defined in the outer
* component or hook.
*/
contextVariables.add(value.lvalue.place.identifier.id);
}
break;
}
default: {
for (const operand of eachInstructionValueOperand(value)) {
CompilerError.invariant(operand.effect !== Effect.Unknown, {
reason: `Expected effects to be inferred prior to ValidateLocalsNotReassignedAfterRender`,
loc: operand.loc,
});
const reassignment = reassigningFunctions.get(
operand.identifier.id
);
if (
reassignment !== undefined &&
operand.effect === Effect.Freeze
) {
/*
* Functions that reassign local variables are inherently mutable and are unsafe to pass
* to a place that expects a frozen value. Propagate the reassignment upward.
*/
return reassignment;
}
}
break;
}
}
}
for (const operand of eachTerminalOperand(block.terminal)) {
const reassignment = reassigningFunctions.get(operand.identifier.id);
if (reassignment !== undefined) {
return reassignment;
}
}
}
return null;
}