Bailout on assigning to globals
Joe Savona committed
Feb 17, 2023 at 10:02 UTC
a2ebea4cde7bded4dfe73922b4f1b7435c7702db
3 files changed
+44
-1
compiler/forget/src/HIR/BuildHIR.ts
+30
-1
@@ -1790,7 +1790,36 @@ function lowerAssignment(
1790
switch (lvalueNode.type) {
1791
case "Identifier": {
1792
const lvalue = lvaluePath as NodePath<t.Identifier>;
1793
- const place = lowerIdentifier(builder, lvalue);
1793
+ const identifier = builder.resolveIdentifier(lvalue);
1794
+ if (identifier == null) {
1795
+ if (kind === InstructionKind.Reassign) {
1796
+ // Trying to reassign a global is not allowed
1797
+ builder.errors.push({
1798
+ reason: `(BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported.`,
1799
+ severity: ErrorSeverity.InvalidInput,
1800
+ nodePath: lvalue,
1801
+ });
1802
+ } else {
1803
+ // Else its an internal error bc we couldn't find the binding
1804
+ builder.errors.push({
1805
+ reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`,
1806
+ severity: ErrorSeverity.Invariant,
1807
+ nodePath: lvalue,
1808
+ });
1809
+ }
1810
+ return {
1811
+ kind: "UnsupportedNode",
1812
+ loc: lvalue.node.loc ?? GeneratedSource,
1813
+ node: lvalue.node,
1814
+ };
1815
+ }
1816
+
1817
+ const place: Place = {
1818
+ kind: "Identifier",
1819
+ identifier: identifier,
1820
+ effect: Effect.Unknown,
1821
+ loc: lvalue.node.loc ?? GeneratedSource,
1822
+ };
1823
builder.push({
1824
id: makeInstructionId(0),
1825
lvalue: { place: { ...place }, kind },
compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md
+11
@@ -68,6 +68,9 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
68
default: {
69
}
70
}
71
+
72
+ // Cannot assign to globals
73
+ someUnknownGlobal = true;
74
}
75
76
```
@@ -400,6 +403,14 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
403
59 | }
404
60 | case foo(): {
405
61 | }
406
+
407
+[ReactForget] InvalidInputError: (BuildHIR::lowerAssignment) Assigning to an identifier defined outside the function scope is not supported.
408
+ 67 |
409
+ 68 | // Cannot assign to globals
410
+> 69 | someUnknownGlobal = true;
411
+ | ^^^^^^^^^^^^^^^^^
412
+ 70 | }
413
+ 71 |
414
```
415
416
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.js
+3
@@ -64,4 +64,7 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
64
default: {
65
}
66
}
67
+
68
+ // Cannot assign to globals
69
+ someUnknownGlobal = true;
70
}