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).
This commit is contained in:
Joe Savona
2023-03-15 09:39:23 -07:00
parent 9200ad027d
commit b69afe0506
2 changed files with 13 additions and 1 deletions
+12
View File
@@ -137,6 +137,12 @@ export function leaveSSA(fn: HIRFunction): void {
originalLVal === undefined ||
originalLVal.lvalue === value.lvalue // in case this was pre-declared for the `for` initializer
) {
if (originalLVal === undefined && block.kind !== "block") {
CompilerError.invariant(
`TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE)`,
value.lvalue.place.loc
);
}
declarations.set(value.lvalue.place.identifier.name, {
lvalue: value.lvalue,
place: value.lvalue.place,
@@ -170,6 +176,12 @@ export function leaveSSA(fn: HIRFunction): void {
originalLVal === undefined ||
originalLVal.lvalue === value.lvalue
) {
if (originalLVal === undefined && block.kind !== "block") {
CompilerError.invariant(
`TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE)`,
place.loc
);
}
declarations.set(place.identifier.name, {
lvalue: value.lvalue,
place,
@@ -19,7 +19,7 @@ function f(reader) {
## Error
```
[ReactForget] Todo: (CodegenReactiveFunction::codegenInstructionValue) Cannot declare variables in a value block, tried to declare 'value'
[ReactForget] Invariant: TODO: Handle reassignment in a value block where the original declaration was removed by dead code elimination (DCE) (6:6)
```