Bailout on assigning to globals

This commit is contained in:
Joe Savona
2023-02-17 10:02:56 -08:00
parent ac9212f24e
commit a2ebea4cde
3 changed files with 44 additions and 1 deletions
+30 -1
View File
@@ -1790,7 +1790,36 @@ function lowerAssignment(
switch (lvalueNode.type) {
case "Identifier": {
const lvalue = lvaluePath as NodePath<t.Identifier>;
const place = lowerIdentifier(builder, lvalue);
const identifier = builder.resolveIdentifier(lvalue);
if (identifier == null) {
if (kind === InstructionKind.Reassign) {
// Trying to reassign a global is not allowed
builder.errors.push({
reason: `(BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported.`,
severity: ErrorSeverity.InvalidInput,
nodePath: lvalue,
});
} else {
// Else its an internal error bc we couldn't find the binding
builder.errors.push({
reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`,
severity: ErrorSeverity.Invariant,
nodePath: lvalue,
});
}
return {
kind: "UnsupportedNode",
loc: lvalue.node.loc ?? GeneratedSource,
node: lvalue.node,
};
}
const place: Place = {
kind: "Identifier",
identifier: identifier,
effect: Effect.Unknown,
loc: lvalue.node.loc ?? GeneratedSource,
};
builder.push({
id: makeInstructionId(0),
lvalue: { place: { ...place }, kind },
@@ -68,6 +68,9 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
default: {
}
}
// Cannot assign to globals
someUnknownGlobal = true;
}
```
@@ -400,6 +403,14 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
59 | }
60 | case foo(): {
61 | }
[ReactForget] InvalidInputError: (BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported.
67 |
68 | // Cannot assign to globals
> 69 | someUnknownGlobal = true;
| ^^^^^^^^^^^^^^^^^
70 | }
71 |
```
@@ -64,4 +64,7 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
default: {
}
}
// Cannot assign to globals
someUnknownGlobal = true;
}