More precise error for canonical reassignment in a value block
These examples previously errored all the way in codegen, when we detected that a value block (eg a `while` test expression) was declaring a new variable. We now detect this in LeaveSSA and error. The actual fix is a bit tricky, we'd need to add a new declaration in the nearest block scope (or selectively not DCE the declaration if its reassigned in just this way).
Joe Savona committed
Mar 15, 2023 at 09:39 UTC
b69afe0506488d325ce4b2f6f4da91f7aa7f18cb
2 files changed
+13
-1
compiler/forget/src/SSA/LeaveSSA.ts
+12
@@ -137,6 +137,12 @@ export function leaveSSA(fn: HIRFunction): void {
137
originalLVal === undefined ||
138
originalLVal.lvalue === value.lvalue // in case this was pre-declared for the `for` initializer
139
) {
140
+ if (originalLVal === undefined && block.kind !== "block") {
141
+ CompilerError.invariant(
142
+ `TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE)`,
143
+ value.lvalue.place.loc
144
+ );
145
+ }
146
declarations.set(value.lvalue.place.identifier.name, {
147
lvalue: value.lvalue,
148
place: value.lvalue.place,
@@ -170,6 +176,12 @@ export function leaveSSA(fn: HIRFunction): void {
176
originalLVal === undefined ||
177
originalLVal.lvalue === value.lvalue
178
) {
179
+ if (originalLVal === undefined && block.kind !== "block") {
180
+ CompilerError.invariant(
181
+ `TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE)`,
182
+ place.loc
183
+ );
184
+ }
185
declarations.set(place.identifier.name, {
186
lvalue: value.lvalue,
187
place,
compiler/forget/src/__tests__/fixtures/compiler/error.while-with-assignment-in-test.expect.md
+1
-1
@@ -19,7 +19,7 @@ function f(reader) {
19
## Error
20
21
```
22
-[ReactForget] Todo: (CodegenReactiveFunction::codegenInstructionValue) Cannot declare variables in a value block, tried to declare 'value'
22
+[ReactForget] Invariant: TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE) (6:6)
23
```
24
25
\ No newline at end of file